4

我正在使用 3.1.2 版开发一个 spring 应用程序,使用 tomcat 7 作为 servlet 管理器。我注意到豆子被创建了两次,我不知道如何防止这种情况。我知道问题出在我的 web.xml 或应用程序上下文中,但我一直无法找到重复的来源。

从启动时的 tomcat 日志中,我看到以下内容(解释为空格):

May 24, 2013 9:33:03 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
May 24, 2013 9:33:03 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Fri May 24 09:33:03 CDT 2013]; root of context hierarchy
May 24, 2013 9:33:03 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/kpi-reporter-servlet.xml]
May 24, 2013 9:33:03 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@df9978: defining beans [...]; root of factory hierarchy
May 24, 2013 9:33:04 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 1260 ms
May 24, 2013 9:33:04 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'kpi-reporter': initialization started
May 24, 2013 9:33:04 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'kpi-reporter-servlet': startup date [Fri May 24 09:33:04 CDT 2013]; parent: Root WebApplicationContext
May 24, 2013 9:33:04 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/kpi-reporter-servlet.xml]
May 24, 2013 9:33:04 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2821db: defining beans [...]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@df9978

这是我的 web.xml 和应用程序上下文配置:

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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_3_0.xsd"
     version="3.0">

<display-name>kpi-reporter</display-name>

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

<servlet-mapping>
  <servlet-name>kpi-reporter</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.css</url-pattern>
  <url-pattern>*.gif</url-pattern>
  <url-pattern>*.js</url-pattern>
  <url-pattern>*.png</url-pattern>
</servlet-mapping>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/kpi-reporter-servlet.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

kpi-reporter-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="."/>

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

</beans>

如果我删除 kpi-reporter-servlet.xml 中的语句,则根本不会定义 bean。如果我将上下文语句移动到 web.xml(连同所需的导入),它仍然会被定义两次。我怀疑 web.xml 在启动过程中被调用了两次。

我检查了 webapps 目录,我只有一个文件和目录,用于部署应用程序的 .war 文件,以及它被解压缩到的目录。

我开发的其他 .war 应用程序也有这种行为,尽管它们具有与之关联的类似 xml 文件。

有谁知道为什么会发生这种情况?我被难住了。

在此先感谢,马克斯

4

2 回答 2

5

您可以注释掉 context-param 部分,如下所示:

<!--context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/kpi-reporter-servlet.xml</param-value>
</context-param-->

或者,将 kpi-reporter-servlet.xml 重命名为其他名称,并相应地更新 context-param 的 param-value。

Spring的文档说:

在 DispatcherServlet 初始化时,Spring MVC 会查找名为 [servlet-name]-servlet.xml 的文件...

所以在你的情况下,就像 kpi-reporter-servlet.xml 被声明了两次。

于 2013-05-24T15:17:08.440 回答
0

Sring 在此处初始化两个应用程序上下文:

ContextLoaderListener 使用 context-param 标签引用的文件中包含的 beans 创建应用程序上下文:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/kpi-reporter-servlet.xml</param-value>
</context-param>

这是“根”(或父)应用程序上下文。

然后您的 DispatcherServlet 正在寻找一个名为 SERVLET-NAME-servlet.xml 的文件,并使用该文件中定义的 bean 构建另一个应用程序上下文。此应用程序上下文将“根”应用程序上下文作为父级。这里您的 servlet 的名称是“kpi-reporter”,因此它会加载“kpi-reporter-servlet.xml”中定义的所有 bean。

因此,如果您不需要使用 ContextLoaderListener 则将其删除。如果需要,请更改文件的名称(例如:kpi-reporter-context.xml)并提供一个空的 kpi-reporter-servlet.xml,它将由 DispatcherServlet 加载。

于 2013-05-24T15:18:19.187 回答