如何避免两次弹簧初始化?

时间:2015-01-26 20:58:18

标签: spring spring-mvc tomcat spring-security web.xml

我正在开发一个Spring mvc应用程序,并且最近注意到所有servlet在启动时都注册了两次。

你们有谁知道为什么?如果是这样,我如何在保持所有功能的同时避免这种情况?

Spring config:

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.1.xsd   
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/cache 
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">  

<!-- SPRING MVC -->  
<mvc:annotation-driven />
<context:annotation-config/> 

<!-- Activates @Scheduled and @Async annotations for scheduling -->
<task:annotation-driven />

<import resource="spring-security.xml"/>

<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" /> 
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/robots.txt" location="robots.txt" />

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"/>
</bean> 

<mvc:interceptors> 
     <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="no"/>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>     
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>  

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

<cache:annotation-driven /> 
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" p:shared="true"/> 
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/> 

 <!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="2000000"/>
</bean>

弹簧security.xml文件

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

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

<!-- SECURITY -->
<global-method-security secured-annotations="enabled" />
<http auto-config='true' use-expressions="true" disable-url-rewriting="true">
    <custom-filter position="CHANNEL_FILTER" ref="channelProcessingFilter"/>
    <intercept-url pattern="/secured/**" access="hasAnyRole('ADMIN')"/>
    <form-login login-page='/login.html' default-target-url='/secured/index.html' always-use-default-target='true'/>
    <logout logout-success-url="/loggedOut.html" delete-cookies="JSESSIONID" />
</http>

<beans:bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"/>

<!--  force https -->
<beans:bean id="channelProcessingFilter" class="org.springframework.security.web.access.channel.ChannelProcessingFilter">
  <beans:property name="channelDecisionManager" ref="channelDecisionManager"/>
  <beans:property name="securityMetadataSource">
    <filter-security-metadata-source path-type="ant">
        <intercept-url pattern="/**" access="REQUIRES_SECURE_CHANNEL" />
    </filter-security-metadata-source>
  </beans:property>
</beans:bean>


<beans:bean id="channelDecisionManager" class="org.springframework.security.web.access.channel.ChannelDecisionManagerImpl">
  <beans:property name="channelProcessors">
    <beans:list>
    <beans:ref bean="secureChannelHeaderProcessor"/>
    <beans:ref bean="insecureChannelHeaderProcessor"/>
    </beans:list>
  </beans:property>
</beans:bean>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Diesel</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/diesel-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
     <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
  </listener>
  <servlet>
    <servlet-name>diesel</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/diesel-servlet.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>diesel</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <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>

   <filter>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <error-page>
        <error-code>404</error-code>
        <location>/redirect.html</location>
    </error-page>

  <servlet>
    <description></description>
    <display-name>dieselInitializer</display-name>
    <servlet-name>dieselInitializer</servlet-name>
    <servlet-class>com.snakeoil.diesel.web.DieselInitializer</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

    <session-config>
        <session-timeout>180</session-timeout>
    </session-config>


</web-app>

1 个答案:

答案 0 :(得分:5)

这是因为您使用相同的配置初始化两个spring上下文。

  • 您可以通过ContextLoaderListener
  • 开始一个春季环境
  • 第二个DispatcherServlet(柴油)

您应该有两个不同的配置文件,

  • 包含所有Spring bean的一个(由ContextLoaderListener加载),除了:Controller和Web相关内容(从组件扫描中排除@Controller
  • 一个(由DispatcherServlet加载),主要包含Controller和Web相关内容的组件扫描(组件扫描只应搜索@Controller

@See:这个问题“ContextLoaderListener or not”并回答未来的细节