網上找了一下關於ContextLoaderListener和ServletDispatcher的解釋,這是原文
http://simone-folino.blogspot.com/2012/05/dispatcherservlet-vs.html
總結如下:
Spring中有兩種上下文環境-"Application Context和Web Application Context",他們分別對應ContextLoaderListener和ServletDispatcher,且都可以用來配置bean的注入,裝配,和AOP
1. ContextLoaderListener
ContextLoaderListener通過讀取contextConfigLocation參數來讀取配置參數,一般來說它配置的是Spring項目的中間層,服務層組件的注入,裝配,AOP.
對應到Spring的自動裝配機制<context:component-scan>就是以下幾種注解的裝配
- DAO: such as @Repository bean
- Entity: such as @Entity bean
- Service: such as @Service bean
(以上內容來自引用博文)
下面是一個典型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文件里