gpt4 book ai didi

正在复制 Spring 应用程序 bean

转载 作者:行者123 更新时间:2023-11-28 21:49:17 24 4
gpt4 key购买 nike

我正在使用 3.1.2 版开发一个 spring 应用程序,使用 tomcat 7 作为 servlet 管理器。我注意到 bean 被创建了两次,但我不确定如何防止这种情况发生。我知道问题出在我的 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 和应用程序上下文配置:

网络.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 文件。

有谁知道为什么会这样?我在这里被难住了。

提前致谢,最大

最佳答案

你可以注释掉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 重命名为其他名称,并相应地更新上下文参数的参数值。

Spring 的文档说:

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]- servlet.xml...

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

关于正在复制 Spring 应用程序 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16738161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com