Java 在Tomcat服务器启动后运行一个方法
只有在Tomcat服务器启动并运行之后,我才需要启动/运行方法(类中声明的3个方法)。我浏览了几个建议在Java 在Tomcat服务器启动后运行一个方法,java,spring,multithreading,tomcat,Java,Spring,Multithreading,Tomcat,只有在Tomcat服务器启动并运行之后,我才需要启动/运行方法(类中声明的3个方法)。我浏览了几个建议在web.xml中使用Servlets(侦听器类)的线程。问题是我的类实现了ServletContextListener,它甚至在Tomcat服务器加载完成之前就运行了 注意:我的web.xml已经被标记到DispatcherServlet,以查找任何RESTAPI调用 Ex(以下是一些帮助您了解更多信息的日志): 因为我的方法有线程,所以它甚至在Tomcat完全启动之前就被卡住了(线程应该只在
web.xml
中使用Servlet
s(侦听器类)的线程。问题是我的类实现了ServletContextListene
r,它甚至在Tomcat服务器加载完成之前就运行了
注意:我的web.xml
已经被标记到DispatcherServlet,以查找任何RESTAPI调用
Ex(以下是一些帮助您了解更多信息的日志):
因为我的方法有线程,所以它甚至在Tomcat完全启动之前就被卡住了(线程应该只在显示“INFO:Server startup in 27236 ms”时启动)
注意:如果我遗漏了什么,请告诉我。您可以编写一个,其中提供了一个。一些东西:
还有其他方法可以做到这一点。您可以在此处阅读一些相关文章:
INFO: No Spring WebApplicationInitializer types detected on classpath
Oct 26, 2018 6:20:43 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
.
{{ Connect with MYSQL }}
.
Oct 26, 2018 6:20:50 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
.
18:20:51.394 [localhost-startStop-1] INFO o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/user/someMethod],methods=[POST],produces=[application/xml || application/json]}" onto public java.util.ArrayList<com.x.y.z.User> com.z.y.controllers.UserController.deleteUser(com.x.y.z.User)
18:20:52.090 [localhost-startStop-1] INFO o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Oct 26 18:20:50 IST 2018]; parent: Root WebApplicationContext
18:20:52.181 [localhost-startStop-1] INFO o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Oct 26 18:20:50 IST 2018]; parent: Root WebApplicationContext
18:20:52.394 [localhost-startStop-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization completed in 2041 ms
Oct 26, 2018 6:20:52 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8081"]
Oct 26, 2018 6:20:52 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8010"]
Oct 26, 2018 6:20:52 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 27236 ms
{{ I want to call methods after Tomcat starts }}
INFO: No Spring WebApplicationInitializer types detected on classpath
Oct 26, 2018 6:22:09 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
.
.
{{ Connect with MYSQL }}
.
18:22:16.461 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 6635 ms
**MyClass - myFileWatcher()
The above thread has started**
.
.
@Bean
public CommandLineRunner commandLineRunner(SomeAppService someAppService) {
return strings -> {
// do something with someAppService
// this code will run just after the application has fully started
};
}