Spring中有两种上下文环境:ApplicationContext和WebApplicationContext,他们分别对应ContextLoaderListener和ServletDispatcher,且都可以用来配置bean的注入,装配,AOP以及事物管理。
1、ContextLoaderListener
ContextLoaderListener通过读取contextConfigLocation参数来读取配置参数,下面是一个典型web.xml中加载ContextLoaderListener的代码片段。
<!-- 定义参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/config/applicationContext.xml</param-value>
</context-param>
<!-- 定义Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
以及 applicationContext.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
<context:component-scan base-package="com.simonefolinoblogspot.dao" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
...
</beans>
可以看到ContextLoaderListener的配置文件都是中间层,数据层,DAO之类的
2. ServletDispatcher
ServletDispatcher通过读取<servlet-name>-servlet.xml文件来读取配置,如果文件不存在,则可以通过servlet标签内的
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/config/dispatcher-servlet.xml</param-value>
</init-param>
来指定配置文件,它配置的是Web层组件的注入,装配和AOP
- Controllers
- ViewResolvers
- LocaleResolvers
- ThemeResolvers
下面是一个典型配置web.xml
<servlet>
<servlet-name>addressbook</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:annotation-config />
<context:component-scan base-package="com.simonefolinoblogspot.controller" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
说一个我的例子,我在Conroller类上加上了@Controller自动注册注释,然后把aop的配置引入放到了applicationContext.xml里,这个配置由ContextLoaderListener读取的
<import resource="classpath*:spring/aop/aopConfig.xml" />
结果AOP方法就不执行了,解决方法就是需要将aopConfig.xml放到ServletDispatcher的配置xml文件里
3、两者混合使用。一般情况下ServletDispatcher即可完成所有功能,但是一旦出现如下情况,修改起来会比较麻烦。但是ServletDispatcher在处理@Controller标注的类时更好,因为它就是为了这类标注的类诞生的,即ServletDispatcher可以搞定@Controller和一般的dao,但是ContextLoaderListener处理不了@Controller,只能处理一般的dao
标准方法是在web.xml中配置两个,然后在spring mvc配置文件中注解只扫controller注解,其余spring不扫controller注解,如上面所述。但是在一些特殊场景中如下:
加入我想引入ehcache的页面缓存。它必须在web.xml里配个SimplePageCachingFilter。由于其加载时机先于servlet,我不得不在web.xml配置ContextLoaderListener。因为ServletDispatcher本质就是个servlet,所以仅仅使用ServletDispatcher是无法完成此需求,故需要ContextLoaderListener来完成。