ServletContextListener 和 static 块

huangapple go评论68阅读模式
英文:

ServletContextListener and static block

问题

I have created ServletContextListener in below class. Also I have created static block in another class of the same package. which would run first in servlet type application. that static block is not running at all.

@WebListener
public class BaclkgroundJobManager implements ServletContextListener {

private ScheduledExecutorService scheduler;

public void contextInitialized(ServletContextEvent sce) {

System.err.println("inside context initialized");
scheduler=Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new SomeHourlyJob(), 0, 2, TimeUnit.MINUTES);

}

}

Below is the class which contains static block.

public class ConnectionUtil {

public static String baseUrl,tokenUrl,grantType,scope,user,password,skillName, accessToken,filePath;

static
{
try {
ClassLoader classLoader= Thread.currentThread().getContextClassLoader();
InputStream input =classLoader.getResourceAsStream("com/dynamicentity/properties/application.properties");
Properties properties =new Properties();
properties.load(input);
System.out.println("Inside the static block of ConnectionUtil class");
skillName=properties.getProperty("chatbot.skillName");
baseUrl=properties.getProperty("chatbot.baseUrl");

}
catch(Exception e)
{
System.out.println(e.getMessage());
}

}

in entire application only this class has static block. will this static block gets executed as soon as i start server? or i will have to run it somehow?

英文:

I have created ServletContextListener in below class. Also I have created static block in another class of the same package. which would run first in servlet type application. that static block is not running at all.

@WebListener
public class BaclkgroundJobManager implements ServletContextListener {

	 private ScheduledExecutorService scheduler;

    public void contextInitialized(ServletContextEvent sce)  { 

    	System.err.println("inside context initialized");
        scheduler=Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new SomeHourlyJob(), 0, 2, TimeUnit.MINUTES);      
    
    }
	
}

Below is the class which contains static block.

public class ConnectionUtil {
	
	public static String baseUrl,tokenUrl,grantType,scope,user,password,skillName, accessToken,filePath;
	
	static
	{
	   try {
		ClassLoader classLoader= Thread.currentThread().getContextClassLoader();
		InputStream input =classLoader.getResourceAsStream("com/dynamicentity/properties/application.properties");
		Properties properties =new Properties();
		properties.load(input);
		System.out.println("Inside the static block of ConnectionUtil class");
		skillName=properties.getProperty("chatbot.skillName");
		baseUrl=properties.getProperty("chatbot.baseUrl");
	
	   }
	   catch(Exception e)
	   {
		   System.out.println(e.getMessage());
	   }
		
	}

in entire application only this class has static block. will this static block gets executed as soon as i start server? or i will have to run it somehow?

答案1

得分: 1

类初始化块static { ... }在类加载过程中运行。通常情况下,类是按需加载的,当需要时才加载。如果您的代码中没有使用ConnectionUtil类,它将永远不会被加载,初始化块也不会运行。

在ConnectionUtil中添加一个静态方法,并从BaclkgroundJobManager中调用它。该方法不必执行任何操作,但它将确保该类被加载。

另一种可能性是使用反射API加载类:

Class.forName("your.package.name.ConnectionUtil");
英文:

Class initializer blocks static { ...} run as a part of the class loading process. Normally classes are loaded on demand, when they are needed. If nothing in your code uses the ConnectionUtil class, it's never loaded, and the initializer block never runs.

Add a static method to ConnectionUtil and call it from BaclkgroundJobManager. The method doesn't have to do anything, but having it will ensure that the class gets loaded.

Another possibility is to load the class using the reflection API

Class.forName("your.package.name.ConnectionUtil");

</details>



huangapple
  • 本文由 发表于 2020年8月11日 07:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63349191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定