当server.xml中配置<Context path="" docBase="path"/>时,启动tomcat,调用了onApplicationEvent 4次,
如图配置
启动打印结果:
信息: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/work/bhcoolp/WebRoot/WEB-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/work/bhcoolp/WebRoot/WEB-INF/lib/slf4j-simple-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
----------------------start---------------------------------
1
null
----------------------end---------------------------------
一月 07, 2019 4:43:45 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'spring'
----------------------start---------------------------------
2
[org.along.license.AuthResourceinfoModel@17e33f71, org.along.license.AuthResourceinfoModel@de26124]
----------------------end---------------------------------
一月 07, 2019 4:43:46 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory [E:\Java\tomcat\tomcat8-2\apache-tomcat-8.5.34\webapps\bhcoolp]
一月 07, 2019 4:43:49 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
一月 07, 2019 4:43:49 下午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
一月 07, 2019 4:43:49 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/Java/tomcat/tomcat8-2/apache-tomcat-8.5.34/webapps/bhcoolp/WEB-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/Java/tomcat/tomcat8-2/apache-tomcat-8.5.34/webapps/bhcoolp/WEB-INF/lib/slf4j-simple-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
----------------------start---------------------------------
3
null
----------------------end---------------------------------
一月 07, 2019 4:43:52 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'spring'
----------------------start---------------------------------
4
[org.along.license.AuthResourceinfoModel@5a74da4, org.along.license.AuthResourceinfoModel@364821d3]
----------------------end---------------------------------
其中两次是由于web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个是 projectName-servlet context(作为root application context的子容器),导致nApplicationEvent方法被执行两次.
然后看启动日志root WebApplicationContext初始化了两次,所以导致了onApplicationEvent 被调用了4次,
原因是,由于host节点配置了appBase为webapps,启动tomcat会加载webapps里的所有项目,然后下面又用Context配置了webapps里的项目,导致项目又加载一次,所以发生了项目重复加载问题
已尝试有效的解决方法
1.将项目解压到webapps下的ROOT文件夹下
2.将项目名改为ROOT
3.将server.xml中Host节点下的appBase="" ,不加载webapps下的项目,
修改后host内容
<Host name="localhost" appBase=""
unpackWARs="true" autoDeploy="false">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="path" debug="0" reloadable="false" />
</Host>
4.使用nginx反向代理,注:项目war包不要放在tomcat的webapps文件夹下