【发布时间】:2020-11-30 14:08:02
【问题描述】:
我在下面的类中创建了 ServletContextListener。我还在同一个包的另一个类中创建了静态块。它将首先在 servlet 类型的应用程序中运行。那个静态块根本没有运行。
@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);
}
}
下面是包含static 块的类。
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());
}
}
在整个应用程序中只有这个类有静态块。这个静态块会在我启动服务器后立即执行吗?还是我必须以某种方式运行它?
【问题讨论】:
标签: java servlets web-applications static servletcontextlistener