在調(diào)試定時(shí)任務(wù)的時(shí)候刀疙,發(fā)現(xiàn)每次定時(shí)任務(wù)舔痕,會(huì)debug兩次魏滚,后來(lái)在定時(shí)任務(wù)類加上了構(gòu)造方法镀首,sysout一句話,這時(shí)候發(fā)現(xiàn)該實(shí)例創(chuàng)建了兩次鼠次,再仔細(xì)一看發(fā)現(xiàn)信息: Initializing Spring root WebApplicationContext
這句話在控制臺(tái)也打印了兩次更哄,也就是說(shuō)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
計(jì)劃任務(wù)被初始化了
三月 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
計(jì)劃任務(wù)被初始化了
三月 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腥寇,到目前為止還沒(méi)發(fā)現(xiàn)對(duì)系統(tǒng)有什么明顯影響成翩,但是性能上肯定是有問(wèn)題的,在完美主義強(qiáng)迫癥的驅(qū)使下我決定解決這個(gè)問(wèn)題赦役。
首先百度搜索了一頓之后發(fā)現(xiàn)有兩種情況會(huì)導(dǎo)致 spring 加載兩次
1) web.xml 配置導(dǎo)致
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
所以導(dǎo)致spring 初始化兩次麻敌,網(wǎng)上有說(shuō)刪掉contextConfigLocation
以及 ContextLoaderListener
這組配置,個(gè)人不推薦這種做法掂摔。
解決方案:
<!-- 加載mvc相關(guān)配置术羔,攔截器,視圖解析器等 -->
<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>
<!-- 加載關(guān)于spring bean的配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml,
classpath*:/applicationContext-shiro.xml
</param-value>
</context-param>
參考了之間的springmvc項(xiàng)目乙漓,簡(jiǎn)單的來(lái)說(shuō)就是這兩處指定聂示,分別加載不同的配置文件,這樣就不會(huì)出現(xiàn)bean重復(fù)加載的問(wèn)題了4孛搿(小弟對(duì)spring 架構(gòu)知識(shí) 知之深淺鱼喉,個(gè)人薄見(jiàn),僅供參考)
2) tomcat配置導(dǎo)致
我在檢查了我的 web.xml 之后趋观,并沒(méi)有發(fā)現(xiàn)上述問(wèn)題扛禽。百度后結(jié)論,tomcat也會(huì)導(dǎo)致spring加載兩次皱坛。
具體原因有待詳細(xì)考證编曼,有知道朋友可以告訴我一下。
解決方案:
設(shè)置不使用默認(rèn)的 webapps
根據(jù)上述配置后 server.xml 的變化(docBase="絕對(duì)路徑")剩辟,在linux環(huán)境下掐场,不知道會(huì)不會(huì)奏效往扔,windows下實(shí)測(cè)沒(méi)有問(wèn)題!在確認(rèn)之后熊户,我會(huì)給出答案萍膛!
<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>
解決,恢復(fù)正常
三月 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
計(jì)劃任務(wù)被初始化了
三月 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