spring - 如何避免两次进行 spring 初始化?

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

我正在开发一个 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>

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

网络.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>

最佳答案

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

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

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

  • 一个(由 ContextLoaderListener 加载)包含所有 Spring bean,除了:Controller 和 Web 相关的东西(不包括 @Controller 来自组件扫描)
  • 一个(由 DispatcherServlet 加载)主要包含 Controller 和 Web 相关内容的组件扫描(组件扫描应该只搜索 @ Controller )

@查看:这个问题“ContextLoaderListener or not”和 future 细节的答案

关于spring - 如何避免两次进行 spring 初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28158639/

相关文章:

java - TOP [N] 记录 Group By per user query in the best possible way

java - 如何使用 Spring MVC 在 SimpleDateFormat 中指定自定义验证错误消息

spring-mvc - 如何使用 spring mvc controller 发出 post 请求?

java - tomcat 8.0.15/jasper/tiles 3.0.5 的 NoClassDefFoundError

eclipse - 从 Eclipse 3.5 在 Tomcat 6 上运行 Spring + Maven2 项目时出现 ClassNotFoundException

java - 如何在 Spring-mvc 中为请求参数设置别名?

java - 将 lineNumber 存储在 stepExecutionContext 中并访问它

java - 使用 HANA 的 Web 应用程序,获取 SLF4J : Class path contains multiple SLF4J bindings

spring - 在Spring Security中的@PreFilter和@PostFilter中获取Collection/Array对象的句柄

java - 如何在 mvn :package 中包含 GWT 生成的文件