Dav*_*ler 5 spring tomcat spring-mvc applicationcontext
我之前在这些论坛上看过这个基本问题,但没有一个答案似乎解决了我的特定问题.无论如何,我有一个小的Spring webapp,包括一个核心Spring业务层,SpringMVC和Spring-Quartz(我也使用MyBatis,虽然我不相信这是相关的).我的所有Spring库都是3.1.3
问题是,当我将我的应用程序部署到Tomcat 6时,通常会将根应用程序上下文和Web应用程序上下文分别初始化两次.通过查看日志可以看出这一点,而且当我的Quartz作业应该触发一次时触发两次这一事实也很明显(后一点是为什么这不仅仅是一个理论问题).
我以为我的应用程序上下文都已整理好了,但显然必定会有一些我不知道的东西.我的一般方法是:
下面是我的配置文件,减去所有的XML文件:
UsMain-servlet.xml中
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/rsc/**" location="/rsc/" cache-period="31556926"/>
<mvc:annotation-driven />
<context:component-scan base-package="com.me.controllers" />
Run Code Online (Sandbox Code Playgroud)
web.xml中
<web-app ... version="2.5">
...
<servlet>
<servlet-name>UsMain</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>UsMain</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml中
<import resource="classes/com/me/config/spring-biz-context.xml" />
<import resource="classes/com/me/config/spring-quartz-context.xml" />
Run Code Online (Sandbox Code Playgroud)
下面是Tomcat启动时我看到两次的日志行示例:
2013-11-07 05:18:27 [main] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
2013-11-07 05:18:27 [main] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Thu Nov 07 05:18:27 UTC 2013]; root of context hierarchy
2013-11-07 05:18:27 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
2013-11-07 05:18:35 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/classes/com/me/config/spring-biz-context.xml]
... bunch of stuff ...
2013-11-07 05:18:38 [main] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'UsMain-servlet': startup date [Thu Nov 07 05:18:38 UTC 2013]; parent: Root WebApplicationContext
2013-11-07 05:18:38 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/UsMain-servlet.xml]
Run Code Online (Sandbox Code Playgroud)
另一件事可能是也可能不是红鲱鱼...如果我以任何方式修改我的代码或配置文件,然后重新打包WAR文件,然后停止Tomcat,将WAR复制到webapps /,并启动Tomcat,应用程序上下文只加载一次(是的!)但是如果我在那之后重新启动Tomcat,则会出现此问题.
编辑
在我不断探索这个问题时,"可能是也可能不是红鲱鱼"似乎是一个重要因素.事实上,这可能是关于Tomcat的一个问题,而不是关于Spring的问题.
如果我在重新启动Tomcat之前从webapps /目录中删除已部署的webapp的子目录,则不会发生此问题.如果我在重新启动Tomcat之前保持子目录不变,那么问题确实发生了.
换句话说,我将webapp打包到MyApp.war中,并将该WAR粘贴到Tomcat的webapps /目录中.当Tomcat启动时,它会解压缩WAR并创建一个webapps/MyApp /子目录.第一次,事情很好.但是,如果我只是重启Tomcat,就会出现问题.如果我删除webapps/MyApp/然后重新启动Tomcat,那么事情就好了.
所以,在某种程度上,问题就解决了.但是关于正在发生的事情的想法仍然会非常有用.据我所知,这不是Tomcat/webapps的运作方式.
我一直在研究这个问题,现在很清楚问题不在于Spring及其应用程序上下文,而是Tomcat正在做的事情.所以我将回答我问过的问题"因为Tomcat在重新启动时正在部署Web应用程序两次".
我的证据是,如果我在重新启动之前从webapps /目录中删除我的Web应用程序的部署子目录,问题永远不会发生,但如果我不删除它,它总是会发生.另外,我添加了一些日志记录到我的"Singleton"Spring bean之一; 当这个问题发生并且bean被创建两次时,每次由不同的类加载器加载.
至于为什么Tomcat这样做,我还不知道,但这是一个完全独立的问题.
编辑
只是补充说我已经弄明白了Tomcat的用处,以防其他人遇到我遇到的同样问题.这与我尝试将WAR文件部署为根Web应用程序有关.请参阅此URL:Apache Tomcat配置参考并搜索此字符串:
If you want to deploy a WAR file or a directory using a context path that is not related to the base file name then one of the following options must be used to prevent double-deployment:
Run Code Online (Sandbox Code Playgroud)
尝试直接在 web.xml contextConfigLocation 中声明 applicationContext.xml 导入作为 applicationContext 的一部分。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
/path/to/your/file1.xml,
/path/to/your/file2.xml
</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1969 次 |
| 最近记录: |