web.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app id="WebApp_ID" version="3.0"
-
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_3_0.xsd">
-
-
-
<context-param>
-
<param-name>log4jConfigLocation</param-name>
-
<param-value>classpath:config/properties/log4j.properties</param-value>
-
</context-param>
-
-
<listener>
-
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
-
</listener>
-
-
<!-- context-param中配置的contextConfigLocation只對ContextLoaderListener有效 不會對DispatcherServlet有效 -->
-
<!-- DispatcherServlet用來加載mvc相關的bean ContextLoaderListener用來加載其他bean -->
-
<!-- ContextLoaderListener會生成一個spring的context 注:DispatcherServlet不會識別@controller注解-->
-
<context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>/WEB-INF/config/spring-bean/*.xml</param-value>
-
</context-param>
-
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
-
</listener>
-
-
-
<!-- 配置DispatcherServlet load-on-startup要為1 它會根據contextConfigLocation生成一個spring的context-->
-
<servlet>
-
<servlet-name>spring</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>/WEB-INF/config/spring-servlet.xml</param-value>
-
</init-param>
-
<load-on-startup>1</load-on-startup>
-
</servlet>
-
-
<servlet-mapping>
-
<servlet-name>spring</servlet-name>
-
<url-pattern>/</url-pattern>
-
</servlet-mapping>
-
-
-
-
<filter>
-
<filter-name>encodingFilter</filter-name>
-
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
-
<init-param>
-
<param-name>encoding</param-name>
-
<param-value>UTF-8</param-value>
-
</init-param>
-
<init-param>
-
<param-name>forceEncoding</param-name>
-
<param-value>true</param-value>
-
</init-param>
-
</filter>
-
<filter-mapping>
-
<filter-name>encodingFilter</filter-name>
-
<url-pattern>/*</url-pattern>
-
</filter-mapping>
-
-
</web-app>
ContextLoaderListener中加載的context成功后,spring 將 applicationContext存放在ServletContext中key值為"org.springframework.web.context.WebApplicationContext.ROOT"的attribute中。
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
可以通過WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)或WebApplicationContextUtils.getWebApplicationContext(servletContext)方法來獲取對應的applicationContext。
DispatcherServlet加載的context成功后,如果 publishContext屬性的值設置為true的話(缺省為true) 會將applicationContext存放在org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中。
在每次request請求時,DispatcherServlet會將此applicationContext存放在request中,
可以通過 RequestContextUtils.getWebApplicationContext 或 WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname)方法 來獲取對應的applicationContext。
在配置的過程中盡量讓DispatcherServlet和ContextLoaderListener職責分離,避免一個bean被加載兩次。
或者只使用DispatcherServlet(沒有試過 應該可行)。
從web.xml的配置可知ContextLoaderListener會先於DispatcherServlet創建ApplicationContext,DispatcherServlet在創建ApplicationContext時會先找到由ContextLoaderListener所創建的ApplicationContext,再將后者的ApplicationContext作為參數傳給DispatcherServlet的ApplicationContext的setParent()方法。
當Spring在執行ApplicationContext的getBean時,如果在自己context中找不到對應的bean,則會在父ApplicationContext中去找。這也解釋了為什么我們可以在DispatcherServlet中獲取到由ContextLoaderListener對應的ApplicationContext中的bean。
spring-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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
-
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/mvc
-
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
<!-- 配置掃描的包 這里配置的包和它的下級包都會被掃描 這樣不僅會掃描@controller @service @repository 還會掃描@component-->
-
<context:component-scan base-package="com.zjf" />
-
-
-
<!-- 注冊HandlerMapper、HandlerAdapter兩個映射類 這是spring為@Controller分發請求所必須的 同時也是表單數據轉換為對象(如xml json等)必須的-->
-
<mvc:annotation-driven />
-
-
-
<!-- 訪問靜態資源 如果沒有這個配置 那么靜態的js css文件將會報錯 因為在web.xml中我們配置了<url-pattern>/</url-pattern> -->
-
<mvc:default-servlet-handler />
-
-
<!-- 視圖解析器 這里使用InternalResourceViewResolver -->
-
<bean
-
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="prefix" value="/WEB-INF/view/"></property>
-
<property name="suffix" value=".jsp"></property>
-
</bean>
-
-
</beans>