0

I am working on a Spring mvc application and have recently noticed that all of the servlets are registered twice upon startup.

Have any of you an idea of why? If so, how I can avoid this while still keeping all of the functionality?

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>

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>

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>
2
  • Don't load your configuration twice. You have a single configuration that is loaded by both the ContextLoaderListener and DispatcherServlet. You should split your configuration. The web related stuff (controllers, view resolvers etc.) should be loaded by the DispatcherServlet the general application stuff (services, repositories, datasources etc) should be loaded by the ContextLoaderListener.
    – M. Deinum
    Jan 26, 2015 at 21:04
  • yeah, you are right! No wonder everything got loaded twice...
    – Eivind
    Mar 12, 2015 at 22:26

1 Answer 1

5

It is because you initialize two spring contexts with the same configuration.

  • you start one spring context via ContextLoaderListener and
  • the second one by DispatcherServlet (diesel)

You should have two different configuation files,

  • one (that is loaded by the ContextLoaderListener) that contains all the Spring beans except: the Controller and Web Related stuff (exclude @Controller from the component scan)
  • one (that is loaded by the DispatcherServlet) that contains mainly the component scan for the Controller and Web Related stuff (component scan should only search for @Controller )

@See: this question "ContextLoaderListener or not" and answer for future details

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.