0

I am using the @Autowired annotation with spring-mvc and spring-security, it works, but the webapp start very slow, about 1 munute every time, because the spring-mvc and spring-security scan twice all autowired classes and the total count of classes about 500. Are there any suggestions to speed up the scan time? or the static xml configuration is better?

in web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/rest-servlet.xml
    </param-value>
</context-param>
    <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>

    <servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
    <servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
    ....

in rest-sevlet.xml

    <context:component-scan base-package="com.mycomp" />
<mvc:annotation-driven />
<mvc:interceptors>
    ....
    </mvc:interceptors>
<import resource="classes/config/applicationContext-security-base.xml"/>
<import resource="classes/config/applicationContext-security.xml"/>

<import resource="classes/config/spring-aop.xml"/>
<!-- i18n --> 
<import resource="classes/config/spring-locale.xml"/>
Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
  • 2
    Why would the classes be scanned twice?! Spring Security only scans defined beans (i.e. `@Components` in the context). component-scan scans the classpath for beans. If that is wrongly configured you can have duplicate beans. And next to that why is the startup-time a problem? Anyway post some configuration and your web.xml. – M. Deinum Nov 07 '13 at 08:28
  • Use a profiler to see where the time is going. Spring is usually quick to start up, but some of your beans might take time (eg Hibernate?) – artbristol Nov 07 '13 at 10:38
  • Scan twice it coused by spring-mvc sevlet (rest-servlet) and spring-security filter, I think. Both they are need a separated spring-context. – Yu Jiaao Nov 07 '13 at 14:13
  • Just like the question [link](http://stackoverflow.com/questions/19826228/spring-web-xml-bean-is-created-twice-with?rq=1)there – Yu Jiaao Nov 07 '13 at 14:20

1 Answers1

2

First don't load your configuration twice. Currently both the ContextLoaderListener as well as the DispatcherServlet load the same configuration files. Result duplicate bean instances, duplicate scanning (and next to that in the future memory problems, weird transaction problems etc.).

Your configuration needs to be split. Your ContextLoaderListener should only load things that are general to your application (services, repositories, datasources etc.). The DispatcherServlet in turn should contain/load only web related things like (@)Controllers, ViewResolvers, Views, mvc configuration etc.

Also beware of component-scanning and don't fall into the trap everyone does. If you add the same comonent-scan to each configuration file this will result in bean duplication (ever bean is instantiated twice). So make sure that your ContextLoaderListener' scans for everything but controllers and that yourDispatcherServlet` scans only for web related content (controllers for instance).

M. Deinum
  • 115,695
  • 22
  • 220
  • 224