是否有可能在应用程序启动时运行一个类时mvc 我想初始化该类中的线程?在spring-mvc应用程序启动时运行一个类
还是有什么办法..?
public class MyServletContextListener implements Runnable {
public void run() {
while (true) {
try {
System.out.println("Inside run()");
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这种输出增加ApplicationListener接口的
NFO: HHH000206: hibernate.properties not found
Jul 24, 2017 8:42:33 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jul 24, 2017 8:42:35 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Jul 24, 2017 8:42:36 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Thread Started
getApplicationName() : /Hibernate_webservice
getId() : org.springframework.web.context.WebApplicationContext:/Hibernate_webservice
getParent() : null
getDisplayName() : Root WebApplicationContext
Jul 24, 2017 8:42:38 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 8880 ms
Jul 24, 2017 8:42:38 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'appServlet'
Jul 24, 2017 8:42:38 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'appServlet': initialization started
Jul 24, 2017 8:42:38 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Mon Jul 24 20:42:38 IST 2017]; parent: Root WebApplicationContext
Jul 24, 2017 8:42:38 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-config.xml]
Jul 24, 2017 8:42:38 PM org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
INFO: Loading properties file from class path resource [application.properties]
Jul 24, 2017 8:42:38 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Jul 24, 2017 8:42:38 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/form],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView sample.test.TestController.method()
Jul 24, 2017 8:42:39 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Jul 24, 2017 8:42:39 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Thread Started
getApplicationName() : /Hibernate_webservice
getId() : org.springframework.web.context.WebApplicationContext:/Hibernate_webservice/appServlet
getParent() : Root WebApplicationContext: startup date [Mon Jul 24 20:42:30 IST 2017]; root of context hierarchy
getDisplayName() : WebApplicationContext for namespace 'appServlet-servlet'
Thread Started
getApplicationName() : /Hibernate_webservice
getId() : org.springframework.web.context.WebApplicationContext:/Hibernate_webservice/appServlet
getParent() : Root WebApplicationContext: startup date [Mon Jul 24 20:42:30 IST 2017]; root of context hierarchy
getDisplayName() : WebApplicationContext for namespace 'appServlet-servlet'
Jul 24, 2017 8:42:39 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'appServlet': initialization completed in 1365 ms
Jul 24, 2017 8:42:40 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8082"]
Jul 24, 2017 8:42:40 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8011"]
Jul 24, 2017 8:42:40 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 19807 ms
此解决方案正在工作,但它在应用程序加载时调用onApplicationEvent(ContextRefreshedEvent event)3次 –
我已经更新了答案,以描述如何多次触发ContextRefreshedEvent并提供有关如何避免/处理该问题的一些建议。 – glytching
有多个应用程序上下文是很好的...如果我只需要一个上下文,该怎么办? –