javascript
Spring-WebApplicationContext解读
- 概述
- Web應用環境下Bean的作用域
- WebApplicationContext類體系結構
- ConfigurableWebApplication
- WebApplicationContext初始化
- 使用ContextLoaderLisetener啟動WebApplicationContext
- WebApplicationContext中的日志文件的兩種配置方式
- 如何在項目中使用Log4j 2
- 使用JavaConfigJava注解的方式啟動
- 使用Groovy DSL配置Bean信息
概述
WebApplicationContext是專門為web應用準備的,它允許從相對于Web根目錄的路徑中裝載資源配置文件完成初始化工作。
從WebApplication中可以獲取ServletContext的引用,整個Web應用上線文對象作為屬性放在到ServletContext中,以便Web應用能訪問Spring應用上下文。
Spring專門為此提供了一個工具類WebApplicationContextUtils,通過該類的getWebApplicationContext(ServletContext sc)方法,可以從ServletContext中獲取WebApplicationContext實例。
Web應用環境下Bean的作用域
在非Web環境下,Bean只有single和prototype兩種作用域。
WebApplicationContext為Bean添加了3個新的作用域
- request
- session
- globalSession
WebApplicationContext類體系結構
由類繼承圖可以看出,WebApplicationContext擴展了ApplicationContext。
WebApplicationContext定義了一個常
ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,在上線文啟動的時候。
WebApplicationContext實例即以此為鍵放在ServletContext的屬性列表中,可以通過下面的語句從Web容器中獲取WebApplicationContext
WebApplicationContext wac = (WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)正式WebApplicationUtils工具類getWebApplicationContext(ServletContext sc)方法的內部實現。
這樣Spring的Web應用上下文和Web容器的上下文應用就可以實現互訪,二者實現了融合。
ConfigurableWebApplication
ConfigurableWebApplicationContext 擴展了WebApplicaiton, 它允許通過配置的方式實例化WebApplicationContext,其中兩個重要的方法:
void setServletContext(ServletContext servletContext);為Spring設置Web應用上下文,以便二者整合。
void setConfigLocations(String... configLocations);設置Spring配置文件地址,如/WEB-INF/smart-dao.xml ,/WEB-INF/smart-service.xml ,配置文件地址是相對于web根目錄的地址。
但用戶也可以使用帶有資源前綴類型的地址 如 classpath:/com/smart/beans.xml
WebApplicationContext初始化
WebApplicationContext的初始化方式,不同于BeanFactory、ApplicationContext, WebApplicationContext需要ServletContext實例,也就是說必須擁有Web容器的前提下才能完成啟動。
通常情況下,在web.xml中配置自啟動的Servlet或者定義Web容器監聽器(ServletContextListener),借助二者中的任何一個,就可以完成Spring Web應用上下文的啟動工作。
注意:
所有的版本的Web容器都可以定義自啟動的Servlet,但是只有Servlet2.3及以上版本的Web容器才支持Web容器監聽器
Spring分別提供了用于啟動WebApplicationContext的Servlet和Web容器監聽器:
3.0之后的版本只能選擇ContextLoaderListener實現,并在web.xml中配置完成。
使用ContextLoaderLisetener啟動WebApplicationContext
<!--(1) 從類路徑下加載Spring配置文件,classpath關鍵字--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-context.xml</param-value></context-param><!--(2)負責啟動Spring容器的監聽器,它將引用(1)處的上下文參數獲得Spring配置文件的地址 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>ContextLoaderListener通過Web容器上下文參數contextConfigLocation獲取Spring配置文件的位置,可以自動多個配置文件,用逗號 空格 或者冒號分開均可。
對于未帶資源類型前綴的配置文件路徑,WebApplicationContext默認這些路徑相對于Web的根路徑部署。當然也可以采用帶資源類型前綴的路徑配置,如 classpath:spring-context.xml .
WebApplicationContext中的日志文件的兩種配置方式
由于WebApplicationContext啟動需要使用日志功能
兩種配置方式:
- 將Log4J的配置文件放在類路徑class下,這時Log4J引擎可以順利啟動。
放在其他位置,必須在web.xml中通過Log4jConfigListener加載
(Log4jConfigListener在 Spring 4.2.1及以后被廢棄)
Apache Log4j 2 : https://logging.apache.org/log4j/2.x/
Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture.
<!--(1) 從類路徑下加載Spring配置文件,classpath關鍵字--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-context.xml</param-value></context-param><!-- 如果log4j并沒有放到類路徑的根目錄下,比如放在了/WEB-INF/log4j.properties,需要在web.xml中加載 如下 --><context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/log4j.properties</param-value></context-param><listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener><!--(2)負責啟動Spring容器的監聽器,它將引用(1)處的上下文參數獲得Spring配置文件的地址 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>經驗證,可以正確加載啟動
如何在項目中使用Log4j 2
具體查看另外一篇博文 Spring-Spring Web項目中配置使用Log4j 2
使用JavaConfig(Java注解)的方式啟動
<?xml version="1.0" encoding="UTF-8"?> <web-app 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"version="3.0"><display-name>Archetype Created Web Application</display-name><!--通過指定context參數,讓Spring使用AnnotationConfigWebApplicationContext而非XmlWebApplicationContext啟動容器--><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param><!--指定標注了@Configuration的配置類,多個類,使用逗號或者空格分隔--><context-param> <param-name>contextConfigLocation</param-name> <param-value>com.xgj.AppConfig1,com.xgj.AppConfig2</param-value> </context-param> <!-- ContextLoaderListener監聽器根據上面的配置使用AnnotationConfigWebApplicationContext根據contextConfigLocation指定的類啟動Spring容器--><listener><listener-class>rg.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>使用Groovy DSL配置Bean信息
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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!--通過指定context參數,讓Spring使用GroovyWebApplicationContext而非XmlWebApplicationContext或AnnotationConfigWebApplicationContext啟動容器 --><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.GroovyWebApplicationContext</param-value></context-param><!-- 指定標注了Groovy的配置文件--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:conf/spring-mvc.groovy</param-value></context-param><!-- ContextLoaderListener監聽器將根據上面配置使用AnnotationConfigWebApplicationContext根據contextConfigLocation指定的配置類啟動Spring容器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>spring-mvc.groovy
import org.hibernate.validator.HibernateValidator import org.springframework.context.support.ReloadableResourceBundleMessageSource import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean import org.springframework.web.servlet.i18n.CookieLocaleResolver import org.springframework.web.servlet.i18n.LocaleChangeInterceptor import org.springframework.web.servlet.view.InternalResourceViewResolverbeans {xmlns context: "http://www.springframework.org/schema/context"xmlns mvc: "http://www.springframework.org/schema/mvc"context.'component-scan'('base-package': "com.smart")mvc.'annotation-driven'('validator': "validator")validator(LocalValidatorFactoryBean) {providerClass = HibernateValidator.classvalidationMessageSource = ref("messageSource")}messageSource(ReloadableResourceBundleMessageSource) {basenames = ["classpath:messages", "classpath:org/hibernate/validator/ValidationMessages"]defaultEncoding = "UTF-8"cacheSeconds = 60}viewResolver(InternalResourceViewResolver) {prefix = "/WEB-INF/jsp/"suffix = ".jsp"}mvc.interceptors() {localeChangeInterceptor(LocaleChangeInterceptor) {paramName = "language"}}cookieLocaleResolver(CookieLocaleResolver) {cookieName = "language"cookieMaxAge = "3600"defaultLocale = "zh_CN"}}總結
以上是生活随笔為你收集整理的Spring-WebApplicationContext解读的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Maven-Could not reso
- 下一篇: Python-爬取自己博客文章的URL