我正在使用Spring MVC,Spring安全性并在Apache Tomcat 1.7x中进行部署。我注意到Web应用程序上下文被加载了两次。请让我知道我的配置有什么问题。
我在下面的帖子中提到过,但无法确定差异 Why Spring Context is loaded twice?, Spring MVC web app: application context starts twice
INFO ContextLoader:273 - Root WebApplicationContext:初始化 开始
以下是我的 web.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>
我的 spring-security.xml
function __construct($bar, $top){
$this->$bar = $bar;
$this->$top = $top;
}
答案 0 :(得分:0)
还有很多方法可以做到这一点。可行的方式取决于servlet-context.xml
的方式。
首先,您只需从<beans:import resource="servlet-context.xml" />
删除spring-security.xml
。
其他方式是删除它:
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/appServlet/spring-security.xml</param-value>
</context-param>
而且:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
并在spring-security.xml
中包含对servlet-context.xml
的导入。
一个好的做法是在多个应用程序上下文中分离您的配置,在applicationContext.xml
中导入所有内容,并为servlet上下文设置特定的应用程序上下文,像这样:
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>
在您的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>