2015-01-26 51 views
0

我正在开发一个Spring MVC应用程序,并且最近注意到所有的servlet在启动时都注册了两次。如何避免两次弹簧初始化?

你有没有想过为什么?如果是这样,我怎样才能避免这一点,同时仍然保持所有的功能?

Spring配置:

<?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> 
+0

不要加载配置两次。你有一个由ContextLoaderListener和DispatcherServlet加载的配置。你应该拆分你的配置。 Web相关的东西(控制器,视图解析器等)应该由'DispatcherServlet'加载,一般的应用程序(服务,存储库,数据源等)应该由'ContextLoaderListener'加载。 – 2015-01-26 21:04:04

+0

是的,你是对的!难怪一切都被装载了两次... – Eivind 2015-03-12 22:26:12

回答

5

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

  • 您通过ContextLoaderListener
  • 启动一个Spring上下文通过DispatcherServlet(柴油)的第二个

你应该有两个不同的configuation文件,

  • 一个(即加载由ContextLoaderListener)包含所有的Spring bean,除了:Controller和Web Related stuff(不包括@Controller)组件扫描)
  • 一个(即由DispatcherServlet装),其中包含主要为Controller和Web相关的东西(组件扫描组件扫描只搜索@Controller

@see:这个问题“ContextLoaderListener or not “并回答以后的详细信息

+0

谢谢!你是对的。这解决了它! – Eivind 2015-03-12 22:24:40