Dears,我在apachetomcat 9.0.46上部署了一个jersey-spring-api。(Jersey用于处理restful服务JAX-RS,Spring用于处理我的所有beans{controllers,DAO,SessionFactory,JPA等等}(。在窗户上的tomcat 9上一切都很好。。。当在ubuntu tomcat 9.0.46中部署完全相同的war时,ContextLoader被触发了3次,我的所有singleton都被实例化了3次。我正在tomcat端口80和443上部署api(https-godady证书(。一旦我启动tomcat,就部署了war,并启动了端口80和443(netstat-tulnp | grep-java(,我在日志中看到了所有实例化的singleton。(池-2(Applicationcontext类我的自定义spring@Configuration类,它被触发,数据库被访问时没有任何问题
2021-06-09 14:41:52,128 1104 [main] INFO o.s.web.context.ContextLoader - Root WebApplicationContext initialized in 905 ms
2021-06-09 14:41:53,124 2100 [pool-2-thread-1] INFO skd.app.core.ApplicationContext - TASK::cleanExpiredStatuses
则服务器需要几分钟(大约10分钟,上面14:41,下面14:51(,并且当端口8005启动时,我再次看到ContextLoader被再次触发2次。
09-Jun-2021 14:51:36.196 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [584,064] milliseconds.
09-Jun-2021 14:51:36.592 INFO [main] org.glassfish.jersey.server.ApplicationHandler.initialize Initiating Jersey application, version Jersey: 2.6 2014-02-18 21:52:53...
09-Jun-2021 14:51:37.042 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/opt/apache-tomcat-9.0.46/webapps/skd-service.war] has finished in [588,185] ms
2021-06-09 14:51:39,388 696 [main] INFO o.s.web.context.ContextLoader - Root WebApplicationContext initialized in 581 ms
09-Jun-2021 14:51:39.632 INFO [main] org.glassfish.jersey.server.ApplicationHandler.initialize Initiating Jersey application, version Jersey: 2.6 2014-02-18 21:52:53...
09-Jun-2021 14:51:40.013 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/opt/apache-tomcat-9.0.46/webapps/skd-service.war]
第三次:
2021-06-09 14:51:41,989 744 [main] INFO o.s.web.context.ContextLoader - Root WebApplicationContext initialized in 605 ms
09-Jun-2021 14:51:42.232 INFO [main] org.glassfish.jersey.server.ApplicationHandler.initialize Initiating Jersey application, version Jersey: 2.6 2014-02-18 21:52:53...
09-Jun-2021 14:51:42.602 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/opt/apache-tomcat-9.0.46/webapps/skd-service.war] has finished in [2,590] ms
在windows中一切都很好,只有当部署到ubuntu tomcat时,我才明白这一点。有人知道为什么对于同一个WAR文件,windows和ubuntu之间的tomcat行为会有这种差异吗?
我已经设法解决了这个问题。该问题与/conf/server.xml中的tomcat配置有关。多个主机将触发每个主机的上下文加载程序。我为所有主机保留了webapps的默认appBase,从而为每个主机触发每个我的战争的ContextLoader。ContextLoader也会多次触发的另一个原因是在内部定义选项,除非您需要加载战争外部的东西。我建议阅读规格:https://tomcat.apache.org/tomcat-4.1-doc/config/host.html