java - How to avoid "Root WebApplicationContext: initialization started" loading twice? -
i using spring mvc, spring security , deploying in apache tomcat 1.7x. notice web application context getting loaded twice. please let me know wrong configuration.
i have referred below posts not identify difference why spring context loaded twice?, spring mvc web app: application context starts twice
info contextloader:273 - root webapplicationcontext: initialization started
below web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- definition of root spring container shared servlets , filters --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>web-inf/spring/appservlet/spring-security.xml</param-value> </context-param> <!-- creates spring container shared servlet , filters --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <listener> <listener-class>org.springframework.security.web.session.httpsessioneventpublisher</listener-class> </listener> <session-config> <session-timeout>5</session-timeout> </session-config> <!-- spring security filter --> <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> <!-- processes application requests --> <servlet> <servlet-name>appservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring/appservlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
my spring-security.xml
<?xml version="1.0" encoding="utf-8"?> <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" xsi:schemalocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <beans:bean id="rolevoter" class="org.springframework.security.access.vote.rolevoter"> <beans:property name="roleprefix" value=""></beans:property> </beans:bean> <beans:import resource="servlet-context.xml" /> <beans:bean id="accessdecisionmanager" class="org.springframework.security.access.vote.affirmativebased"> <beans:constructor-arg name="decisionvoters" ref="rolevoter" /> </beans:bean> <http authentication-manager-ref="login-service" access-decision-manager-ref="accessdecisionmanager"> .... </http> <beans:bean id="userloginservice" class="my.test.service.impl.userloginservice"> <beans:property name="userprofileservice" ref="userprofileservice" /> </beans:bean> <!-- login message --> <beans:bean id="messagesource" class="org.springframework.context.support.resourcebundlemessagesource"> <beans:property name="basenames"> <beans:list> <beans:value>mymessages</beans:value> </beans:list> </beans:property> </beans:bean> </beans:beans>
there more way this. way work depends how servlet-context.xml
are.
first, can remove <beans:import resource="servlet-context.xml" />
spring-security.xml
.
other way remove this:
<!-- definition of root spring container shared servlets , filters --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>web-inf/spring/appservlet/spring-security.xml</param-value> </context-param>
and this:
<listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener>
and include import spring-security.xml
in servlet-context.xml
.
a practice separate configuration in multiple application contexts, import in applicationcontext.xml
, have specific application context servlet context, this:
applicationcontext.xml
:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.package" /> <import resource="applicationcontext-security.xml"/> <import resource="applicationcontext-data.xml"/> </beans>
applicationcontext-servlet.xml
:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <mvc:annotation-driven /> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver" p:prefix="/web-inf/views/" p:suffix=".jsp" /> <bean class="org.springframework.web.servlet.handler.simplemappingexceptionresolver" p:order="3" p:defaulterrorview="error" /> </beans>
and in web.xml
:
<!-- spring servlet --> <servlet> <servlet-name>springdispatcherservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>web-inf/applicationcontext-servlet.xml</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springdispatcherservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:meta-inf/applicationcontext.xml</param-value> </context-param>
Comments
Post a Comment