2010-09-09 102 views
2

这里是我的web.xml:弹簧3个MVC多个应用程序上下文实例

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

<!-- Enables clean URLs with JSP views e.g. /welcome instead of /app/welcome --> 
<filter> 
    <filter-name>UrlRewriteFilter</filter-name> 
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>UrlRewriteFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/*.xml 
     </param-value> 
</context-param> 
<!-- Handles all requests into the application --> 
<servlet> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/*.xml 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<!-- Maps all /app requests to the DispatcherServlet for handling --> 
<servlet-mapping> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

为什么有应用程序上下文的两个实例产生的? 当我使用@Scheduled添加一个预定的方法时,会调用两次,因为这两个应用程序上下文。

+1

请参阅http://stackoverflow.com/questions/3652090/difference-between-applicationcontext-and-spring-servlet-xml-in-spring/3652125#3652125和http://stackoverflow.com/questions/3230663/double- spring-beans-in-jetty-7-1-4/3231031#3231031 – axtavt 2010-09-09 19:09:36

+0

我认为它没有真正解释如何解决它,所以我的@Scheduled方法只被调用一次。 – mike27 2010-09-09 19:25:12

回答

3

您正在两次加载相同的spring配置文件。当然,你有两个独立的应用程序上下文。起初我会将DispatcherServerlet的servlet名称重命名为“spring3mvc”。该servlet的定义应该是这样的:

<servlet> 
     <servlet-name>spring3Mvc</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

如果以这种方式拥有它,你应该在你的“WEB-INF”目录下有一个Spring配置文件“spring3Mvc-servlet.xml中”。由于正确的命名约定,Spring会自动找到这个文件。在这个文件中,你应该只有那些对springMVC很重要的bean。它可能看起来像这样:

<context:component-scan base-package="org.company.gui.controller"/> 

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
    <property name="prefix" value="/WEB-INF/jsp/"/> 
    <property name="suffix" value=".view.jsp"/> 
</bean> 

这应该解决您的问题。

3

我注意到,你在下面的块

<servlet> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/*.xml 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

尝试删除具有

<load-on-startup>1</load-on-startup> 

...它为我工作

1

你有任何其他的弹簧过滤器或JSP你的web.xml中的页面没有显示在你的代码片段中?

我问,回答你的问题,因为我相信从Spring documentation这句话能说明什么可能发生的......

“在Web MVC框架中,每个 DispatcherServlet有它自己的 的WebApplicationContext,它继承在 根WebApplicationContext定义 所有豆。定义可以是 重写在特定的servlet- 范围,和新的特定范围-豆 可以定义本地到GIV这些 继承豆en servlet实例“。

如果你对我的web.xml问题回答“yes”,那么我的猜测是Spring创建一个根实例化WebApplicationContext(通过ContextLoaderListener)。因此,这会在发生之前发生......

然后,当创建DispatcherServlet时,“contextConfigLocation”引用相同的文件(即,相同的bean名称),因此新的WebApplicationContext会覆盖本地的bean名称那个servlet!

我想知道,即使你回答“不”,这是否仍然会发生。由于您在DispatcherServlet配置中设置了“contextConfigLocation”(由ContextLoaderListener使用)并“覆盖”它;我假设Spring不检查这些配置是否使用相同的文件集。

您可以通过调试程序运行这些方案,并在WebApplicationContext方法上放置断点以确定。

解决方法:

为了解决这个问题,或者:

1)确保你的2个contextConfigLocations不要在文件重叠,他们使用

或者:

2)在它自己的xml文件中打开Scheduling Bean并确保它仅由2个contextConfigLocations中的一个引用