gpt4 book ai didi

java - 在 spring-mvc 应用程序开始时运行一个类

转载 作者:行者123 更新时间:2023-11-30 06:05:54 24 4
gpt4 key购买 nike

在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 接口(interface)后得到的输出

            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

最佳答案

Spring 提供了 ApplicationListener<ContextRefreshedEvent>界面及其onApplicationEvent(ContextRefreshedEvent event)钩。

例如:

@Component
public class MyServiceCreationListener implements ApplicationListener<ContextRefreshedEvent> {

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// do something on container startup
}
}

无论你在 onApplicationEvent 中写什么方法将在创建 Spring 上下文时执行一次且仅执行一次。

如果您发现此方法被多次调用,那么您必须有多个 ApplicationContext在玩。如果你检查 event.getApplicationContext()您可能会看到它们中的每一个都有不同的 id 和 displayName 并且您可以找出它们的来源。如果您的应用程序是 Spring MVC 应用程序,那么您可能同时拥有 ContextLoaderListenerDispatcherServlet , 这两个都创建了自己的 ApplicationContext每个都会触发 ContextRefreshedEvent .

如果您想确定哪个是父上下文,您可以尝试 event.getApplicationContext().getParent() != null或者您可以在 ApplicationListener 中切换一个类 boolean 值,例如alreadyInitialised .

还有 Spring 4 的 SmartInitializingSingleton这可能允许您在不同级别 Hook 上下文创建。

关于java - 在 spring-mvc 应用程序开始时运行一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45281185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com