[解决] springmvc 初始化两次WebApplicati
2018-03-07 本文已影响353人
殷天文
在调试定时任务的时候,发现每次定时任务,会debug两次,后来在定时任务类加上了构造方法,sysout一句话,这时候发现该实例创建了两次,再仔细一看发现信息: Initializing Spring root WebApplicationContext
这句话在控制台也打印了两次,也就是说spring 被加载了两次。
三月 07, 2018 11:36:09 上午 org.apache.catalina.core.ApplicationContext log
信息: Set web app root system property: 'webapp.root' = [F:\apache-tomcat-8.0.44\webapps\shopxxb2b2c\]
三月 07, 2018 11:36:09 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
计划任务被初始化了
三月 07, 2018 11:36:18 上午 net.shopxx.listener.InitListener handle
信息: Initializing SHOP++ B2B2C 5.0 RELEASE
三月 07, 2018 11:36:19 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'springmvc'
三月 07, 2018 11:36:25 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory F:\apache-tomcat-8.0.44\webapps\ROOT
三月 07, 2018 11:36:26 上午 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, 2018 11:36:26 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory F:\apache-tomcat-8.0.44\webapps\ROOT has finished in 1,517 ms
三月 07, 2018 11:36:26 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory F:\apache-tomcat-8.0.44\webapps\shopxxb2b2c
三月 07, 2018 11:36:33 上午 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, 2018 11:36:33 上午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
三月 07, 2018 11:36:34 上午 org.apache.catalina.core.ApplicationContext log
信息: Set web app root system property: 'webapp.root' = [F:\apache-tomcat-8.0.44\webapps\shopxxb2b2c\]
三月 07, 2018 11:36:34 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
计划任务被初始化了
三月 07, 2018 11:36:42 上午 net.shopxx.listener.InitListener handle
信息: Initializing SHOP++ B2B2C 5.0 RELEASE
三月 07, 2018 11:36:43 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'springmvc'
三月 07, 2018 11:36:45 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory F:\apache-tomcat-8.0.44\webapps\shopxxb2b2c has finished in 18,476 ms
三月 07, 2018 11:36:45 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
三月 07, 2018 11:36:45 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
三月 07, 2018 11:36:45 上午 org.apache.catalina.startup.Catalina start
信息: Server startup in 51220 ms
虽然加载了两次spring,到目前为止还没发现对系统有什么明显影响,但是性能上肯定是有问题的,在完美主义强迫症的驱使下我决定解决这个问题。
首先百度搜索了一顿之后发现有两种情况会导致 spring 加载两次
1) web.xml 配置导致
http://blog.csdn.net/chaijunkun/article/details/6925889
该博主的 web.xml 配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>TaskTest</display-name>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!-- 中间省略 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext*.xml</param-value>
</context-param>
</web-app>
他在这两处都加载了 applicationContext*.xml
所以导致spring 初始化两次,网上有说删掉contextConfigLocation
以及 ContextLoaderListener
这组配置,个人不推荐这种做法。
解决方案:
<!-- 加载mvc相关配置,拦截器,视图解析器等 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 加载关于spring bean的配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml,
classpath*:/applicationContext-shiro.xml
</param-value>
</context-param>
参考了之间的springmvc项目,简单的来说就是这两处指定,分别加载不同的配置文件,这样就不会出现bean重复加载的问题了!(小弟对spring 架构知识 知之深浅,个人薄见,仅供参考)
2) tomcat配置导致
我在检查了我的 web.xml 之后,并没有发现上述问题。百度后结论,tomcat也会导致spring加载两次。
具体原因有待详细考证,有知道朋友可以告诉我一下。
解决方案:
开发环境设置如图设置不使用默认的 webapps
根据上述配置后 server.xml 的变化(docBase="绝对路径"),在linux环境下,不知道会不会奏效,windows下实测没有问题!在确认之后,我会给出答案!
<Engine defaultHost="localhost" name="Catalina">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
<Context docBase="F:\apache-tomcat-8.0.44\extWebapps\shopxxb2b2c" path="/shop" reloadable="true" source="org.eclipse.jst.jee.server:shopxxb2b2c"/></Host>
</Engine>
解决,恢复正常
三月 07, 2018 4:20:34 下午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
三月 07, 2018 4:20:34 下午 org.apache.catalina.core.ApplicationContext log
信息: Set web app root system property: 'webapp.root' = [F:\apache-tomcat-8.0.44\extWebapps\shopxxb2b2c\]
三月 07, 2018 4:20:34 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
计划任务被初始化了
三月 07, 2018 4:20:43 下午 net.shopxx.listener.InitListener handle
信息: Initializing SHOP++ B2B2C 5.0 RELEASE
三月 07, 2018 4:20:44 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'springmvc'
三月 07, 2018 4:20:48 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory F:\apache-tomcat-8.0.44\webapps\ROOT
三月 07, 2018 4:20:50 下午 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, 2018 4:20:50 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory F:\apache-tomcat-8.0.44\webapps\ROOT has finished in 1,820 ms
三月 07, 2018 4:20:50 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
三月 07, 2018 4:20:50 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
三月 07, 2018 4:20:50 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 29063 ms