Java SpringMVC服务bean加载了两次
我的SpringMVC应用程序有一个问题,我的@Service类被创建了两次。我发现很少有线程讨论这个问题,而且大部分时间都与在应用程序和Servlet上下文中定义Java SpringMVC服务bean加载了两次,java,spring,spring-mvc,Java,Spring,Spring Mvc,我的SpringMVC应用程序有一个问题,我的@Service类被创建了两次。我发现很少有线程讨论这个问题,而且大部分时间都与在应用程序和Servlet上下文中定义有关。但在我的例子中,所有配置都在应用程序上下文文件中,Servlet上下文conf文件为空。我包括web.xml和applicationContext.xml文件 applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="
有关。但在我的例子中,所有配置都在应用程序上下文文件中,Servlet上下文conf文件为空。我包括web.xml
和applicationContext.xml
文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<tx:annotation-driven />
<mvc:annotation-driven />
<task:annotation-driven />
<context:component-scan base-package="my.app" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/assets/**" location="/WEB-INF/assets/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="characterEncoding" value="UTF-8" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
</set>
</property>
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="1" />
<property name="viewNames" value="thymeleaf/*" />
</bean>
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g.
/?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="myCookie" />
<property name="defaultLocale" value="sk_SK" />
</bean>
<import resource="spring/spring-security.xml"/>
<import resource="spring/data-source.xml" />
<import resource="spring/lang-source.xml"/>
<import resource="spring/data-properties.xml"/>
<import resource="spring/data-managers.xml" />
</beans>
<?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">
<session-config>
<session-timeout>300</session-timeout>
</session-config>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<tx:annotation-driven />
<mvc:annotation-driven />
<task:annotation-driven />
<context:component-scan base-package="my.app" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/assets/**" location="/WEB-INF/assets/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="characterEncoding" value="UTF-8" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalDialects">
<set>
<bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
</set>
</property>
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="1" />
<property name="viewNames" value="thymeleaf/*" />
</bean>
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g.
/?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="myCookie" />
<property name="defaultLocale" value="sk_SK" />
</bean>
<import resource="spring/spring-security.xml"/>
<import resource="spring/data-source.xml" />
<import resource="spring/lang-source.xml"/>
<import resource="spring/data-properties.xml"/>
<import resource="spring/data-managers.xml" />
</beans>
<?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">
<session-config>
<session-timeout>300</session-timeout>
</session-config>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
300
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
0
org.springframework.web.context.ContextLoaderListener
appServlet
/
SetCharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF-8
强制编码
真的
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
如果您使用spring mvc。您的应用程序中将有两个上下文。基于applicationContext.xml和appServlet servlet.xml(名称基于servlet名称appServlet)的spring上下文
当spring上下文初始化时,它将对applicationContext.xml中定义的包进行组件扫描。这是您第一次创建服务。
然后,当您使用DispatcherServlet时,它将创建一个spring上下文。如果您有appServlet-servlet.xml,它就是基于此。否则,它将对所有类路径进行组件扫描。这是您的服务第二次创建
如果不需要,可以在applicationContext.xml中编写:
<context:component-scan base-package="my.app">
<context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" />
</context:component-scan>
然后您应该创建appServlet-servlet.xml并编写如下组件扫描:
<context:component-scan base-package="my.app" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
因此,您可以在spring上下文中保存所有组件和服务。spring mvc将只保留控制器。它是真的空的还是正在导入配置?还要确保您没有在代码中自己创建上下文实例。只是想知道如何检查复制bean的创建?当使用基于类的代理时,看起来好像创建了重复的实例……是的,servlet conf是空的,没有导入。我正在自动连接代码中的所有服务bean,因此没有额外的实例创建。我使用日志记录服务,这是我所知道的双重性。我确信日志实现不是问题,因为我看到一些业务方法也会执行两次。没有理由当有两个实例时,一个方法也会执行两次(除非您使用的调度程序也会启动两次)。那么loggin的问题不在吗?我也没有提到创建实例,而是创建了一个
ApplicationContext
。你的web.xml
旁边没有WebApplicationInitializer
?如果你的servlet context.xml
是空的,那为什么还要有一个xml文件呢。在此处指定空的
时,它将构造一个空的ApplicationContext
。OP已声明由DispatcherServlet
加载的上下文为空,因此它不加载任何内容。其次,您的代码有缺陷,因为由DispatcherServlet加载的代码应该包含一个使用默认过滤器=“false”
,因为此时它正在扫描所有内容。如果它没有加载任何内容,所有控制器将无法处理该请求。所以我认为是组件扫描它的包来创建控制器。不,不是,它扫描所有东西,不仅仅是控制器。应用程序上下文将使它扫描所有东西,当然包含控制器。但此时DispatcherServlet无法获取控制器。因此,处理请求将不起作用。我认为OP可以让web应用程序正常工作。因此,必须使用mvc sacn包才能获得控制器。如果它也扫描服务呢?OP的问题是加载两次bean。您发布的代码将导致重复加载除@Controller
之外的所有bean。这将造成严重破坏,尤其是如果根目录中有
或类似文件。突然之间没有交易。