最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中的ContextLoaderListener詳細解析

 更新時間:2023年12月08日 10:37:09   作者:遇見更好的自己、  
這篇文章主要介紹了Spring中的ContextLoaderListener詳細解析,在web容器即Tomact容器啟動web應用即servlet應用時,會觸發(fā)ServletContextEvent時間,這個事件會被ServletContextListener監(jiān)聽,需要的朋友可以參考下

前言

我們已經了解到,在web容器即Tomact容器啟動web應用即servlet應用時,會觸發(fā)ServletContextEvent時間,這個事件會被ServletContextListener監(jiān)聽,監(jiān)聽到了以后會通過ContextLoaderListener監(jiān)聽器(ServletContextListener的一個實現(xiàn)類)去初始化Spring容器其實就是去初始化一些Bean。即默認WEB-INF/applicationContext.xml文件的配置信息。

關于有如下兩點需要理解:

1.web項目自身:接收web容器啟動web應用的通知,開始自身配置的解析加載,創(chuàng)建bean實例,通過一個WebApplicationContext來維護spring項目的主容器相關的bean,以及其他一些組件。

2.web容器:web容器使用ServletContext來維護每一個web應用,ContextLoaderListener將spring容器,即WebApplicationContext,作為ServletContext的一個attribute,key為WebApplicationContext.class.getName() + “.ROOT”,保存在ServletContext中,從而web容器和spring項目可以通過ServletContext來交互。

ContextLoaderListener只是作為一個中間層來建立spring容器和web容器的關聯(lián)關系,而實際完成以上兩個角度的工作是通過ContextLoader來進行的,即在ContextLoader中定義以上邏輯,

ContextLoaderListener的實現(xiàn)

ContextLoaderListener的源碼如下:

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
	/**
	 * Create a new {@code ContextLoaderListener} that will create a web application
	 * context based on the "contextClass" and "contextConfigLocation" servlet
	 * context-params. See {@link ContextLoader} superclass documentation for details on
	 * default values for each.
	 * <p>This constructor is typically used when declaring {@code ContextLoaderListener}
	 * as a {@code <listener>} within {@code web.xml}, where a no-arg constructor is
	 * required.
	 * <p>The created application context will be registered into the ServletContext under
	 * the attribute name {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE}
	 * and the Spring application context will be closed when the {@link #contextDestroyed}
	 * lifecycle method is invoked on this listener.
	 * @see ContextLoader
	 * @see #ContextLoaderListener(WebApplicationContext)
	 * @see #contextInitialized(ServletContextEvent)
	 * @see #contextDestroyed(ServletContextEvent)
	 */
	public ContextLoaderListener() {
	}
	/**
	 * Create a new {@code ContextLoaderListener} with the given application context. This
	 * constructor is useful in Servlet 3.0+ environments where instance-based
	 * registration of listeners is possible through the {@link javax.servlet.ServletContext#addListener}
	 * API.
	 * <p>The context may or may not yet be {@linkplain
	 * org.springframework.context.ConfigurableApplicationContext#refresh() refreshed}. If it
	 * (a) is an implementation of {@link ConfigurableWebApplicationContext} and
	 * (b) has <strong>not</strong> already been refreshed (the recommended approach),
	 * then the following will occur:
	 * <ul>
	 * <li>If the given context has not already been assigned an {@linkplain
	 * org.springframework.context.ConfigurableApplicationContext#setId id}, one will be assigned to it</li>
	 * <li>{@code ServletContext} and {@code ServletConfig} objects will be delegated to
	 * the application context</li>
	 * <li>{@link #customizeContext} will be called</li>
	 * <li>Any {@link org.springframework.context.ApplicationContextInitializer ApplicationContextInitializer org.springframework.context.ApplicationContextInitializer ApplicationContextInitializers}
	 * specified through the "contextInitializerClasses" init-param will be applied.</li>
	 * <li>{@link org.springframework.context.ConfigurableApplicationContext#refresh refresh()} will be called</li>
	 * </ul>
	 * If the context has already been refreshed or does not implement
	 * {@code ConfigurableWebApplicationContext}, none of the above will occur under the
	 * assumption that the user has performed these actions (or not) per his or her
	 * specific needs.
	 * <p>See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
	 * <p>In any case, the given application context will be registered into the
	 * ServletContext under the attribute name {@link
	 * WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} and the Spring
	 * application context will be closed when the {@link #contextDestroyed} lifecycle
	 * method is invoked on this listener.
	 * @param context the application context to manage
	 * @see #contextInitialized(ServletContextEvent)
	 * @see #contextDestroyed(ServletContextEvent)
	 */
	public ContextLoaderListener(WebApplicationContext context) {
		super(context);
	}
	/**
	 * Initialize the root web application context.
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		initWebApplicationContext(event.getServletContext());
	}
	/**
	 * Close the root web application context.
	 */
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		closeWebApplicationContext(event.getServletContext());
		ContextCleanupListener.cleanupAttributes(event.getServletContext());
	}

可以看到ContextLoaderListener繼承了ServletContextListener并實現(xiàn)了ContextLoader類。

在web容器啟動的時候,ContextLoaderListener就會監(jiān)聽到。

并將監(jiān)聽事件傳入到方法:contextInitialized(ServletContextEvent event)中,然后,調用了父類的方法去初始化initWebApplicationContext:

Spring容器初始化

接下來我們的重點是Spring容器是如何初始化的:

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
		if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
			throw new IllegalStateException(
					"Cannot initialize context because there is already a root application context present - " +
					"check whether you have multiple ContextLoader* definitions in your web.xml!");
		}
		servletContext.log("Initializing Spring root WebApplicationContext");
		Log logger = LogFactory.getLog(ContextLoader.class);
		if (logger.isInfoEnabled()) {
			logger.info("Root WebApplicationContext: initialization started");
		}
		long startTime = System.currentTimeMillis();
		try {
			// Store context in local instance variable, to guarantee that
			// it is available on ServletContext shutdown.
			if (this.context == null) {
              // 創(chuàng)建一個WebApplicationContext
                   // 具體類型如果是指定了contextClass則使用該指定的;
               // 默認使用XmlWebApplicationContext
				this.context = createWebApplicationContext(servletContext);
			}
			if (this.context instanceof ConfigurableWebApplicationContext) {
				ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
				if (!cwac.isActive()) {
					// The context has not yet been refreshed -> provide services such as
					// setting the parent context, setting the application context id, etc
					if (cwac.getParent() == null) {
						// The context instance was injected without an explicit parent ->
						// determine parent for root web application context, if any.
						ApplicationContext parent = loadParentContext(servletContext);
						cwac.setParent(parent);
					}
                          // 核心方法,完成配置加載,BeanDefinition定義和bean對象創(chuàng)建
					configureAndRefreshWebApplicationContext(cwac, servletContext);
				}
			}
			servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
			ClassLoader ccl = Thread.currentThread().getContextClassLoader();
			if (ccl == ContextLoader.class.getClassLoader()) {
				currentContext = this.context;
			}
			else if (ccl != null) {
				currentContextPerThread.put(ccl, this.context);
			}
			if (logger.isInfoEnabled()) {
				long elapsedTime = System.currentTimeMillis() - startTime;
				logger.info("Root WebApplicationContext initialized in " + elapsedTime + " ms");
			}
			return this.context;
		}
		catch (RuntimeException | Error ex) {
			logger.error("Context initialization failed", ex);
			servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
			throw ex;
		}
	}

在看上述代碼前,我們需要了解ContextLoader的一些重要含義

ContextLoader主要負責加載spring主容器,即root ApplicationContext,在設計層面主要定義了contextId,contextConfigLocation,contextClass,contextInitializerClasses。這些參數都可以在配置中指定,如web.xml的context-param標簽,或者是基于Java編程方式配置的WebApplicationInitializer中定義,作為分別為:contextId:當前容器的id,主要給底層所使用的BeanFactory,在進行序列化時使用。

  • contextConfigLocation:配置文件的位置,默認為WEB-INF/applicationContext.xml,可以通過在web.xml使用context-param標簽來指定其他位置,其他名字或者用逗號分隔指定多個。在配置文件中通過beans作為主標簽來定義bean。這樣底層的BeanFactory會解析beans標簽以及里面的bean,從而來創(chuàng)建BeanDefinitions集合,即bean的元數據內存數據庫。
  • contextClass:當前所使用的WebApplicationContext的類型,如果是在WEB-INF/applicationContext.xml中指定beans,則使用XmlWebApplicationContext,如果是通過注解,如@Configuration,@Component等,則是AnnotationConfigWebApplicationContext,通過掃描basePackages指定的包來創(chuàng)建bean。
  • contextInitializerClasses:ApplicationContextInitializer的實現(xiàn)類,即在調用ApplicationContext的refresh加載beanDefinition和創(chuàng)建bean之前,對WebApplicationContext進行一些初始化

configureAndRefreshWebApplicationContext源碼如下:

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
		if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
			// The application context id is still set to its original default value
			// -> assign a more useful id based on available information
			String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
			if (idParam != null) {
				wac.setId(idParam);
			}
			else {
				// Generate default id...
				wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
						ObjectUtils.getDisplayString(sc.getContextPath()));
			}
		}
		wac.setServletContext(sc);
		String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
		if (configLocationParam != null) {
			wac.setConfigLocation(configLocationParam);
		}
		// The wac environment's #initPropertySources will be called in any case when the context
		// is refreshed; do it eagerly here to ensure servlet property sources are in place for
		// use in any post-processing or initialization that occurs below prior to #refresh
		ConfigurableEnvironment env = wac.getEnvironment();
		if (env instanceof ConfigurableWebEnvironment) {
			((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
		}
		customizeContext(sc, wac);
		// ApplicationContext的核心方法:在refresh方法中完成ApplicationContext的啟動
		// 即spring容器的各個核心組件的創(chuàng)建,如beanDefinitions,enviromnet等
		wac.refresh();
	}

總結

通過上面的分析可知,ContextLoader完成spring主容器的創(chuàng)建,工作主要是負責從ServletContext中,具體為ServletContext從web.xml文件或者WebApplicationInitializer的實現(xiàn)類中,獲取WebApplicationContext的相關配置信息,如使用contextClass指定使用哪種WebApplicationContext實現(xiàn),contextConfigLocation指定spring容器的配置文件在哪里,以及獲取WebApplicationContextInitializers來在進行spring容器創(chuàng)建之前,對WebApplicationContext進行加工處理。

而spring容器的創(chuàng)建,則是使用spring-context包的ApplicationContext,spring-beans包的BeanFactory,來完成從配置中獲取beans定義并創(chuàng)建BeanDefinition,獲取一些資源屬性值,以及完成單例bean的創(chuàng)建等。具體在之后關于ApplicationContext和BeanFactory體系結構的文章中分析。

到此這篇關于Spring中的ContextLoaderListener詳細解析的文章就介紹到這了,更多相關Spring中的ContextLoaderListener內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java實現(xiàn)模擬RPG格斗

    java實現(xiàn)模擬RPG格斗

    這篇文章主要介紹了java實現(xiàn)模擬RPG格斗,每個英雄具有以下幾個屬性:生命值(為0時英雄倒下)、攻擊力(每次攻擊時扣除對方的生命值點數)、攻擊間隔(每次攻擊過后都要等待間隔時間才能進行下次攻擊首次攻擊之前也要先等待間隔時間)簡單設置了下吧,小伙伴可以參考下
    2015-03-03
  • SpringBoot異步調用相同類的解決方案

    SpringBoot異步調用相同類的解決方案

    在SpringBoot中,同一個類中調用帶有@Async注解的方法時,異步調用會失效,因為直接通過this調用方法時,并沒有通過Spring的代理對象,下面給大家分享SpringBoot異步調用相同類的解決方案,感興趣的朋友一起看看吧
    2025-02-02
  • Java中內部類的概念與分類詳解

    Java中內部類的概念與分類詳解

    一個類的定義放在另一個類的內部,這個類就叫做內部類,下面這篇文章主要給大家介紹了關于Java中內部類的概念與分類的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-09-09
  • Java?輕松掌握字符緩沖流的使用

    Java?輕松掌握字符緩沖流的使用

    這篇文章主要介紹了Java的字符緩沖流用法,字符緩沖流的用途很多,主要是幾個構造方法的使用,在項目開發(fā)中經常會用到,需要的朋友參考下吧
    2022-04-04
  • java中Javers?比較兩個類的差異

    java中Javers?比較兩個類的差異

    本文主要介紹了Javers?比較兩個類的差異,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Spring底層核心源碼原理解析

    Spring底層核心源碼原理解析

    這篇文章主要介紹了Spring底層核心源碼原理解析,當在某個方法上加了@Transactional注解后,就表示該方法在調用時會開啟Spring事務,而這個方法所在的類所對應的Bean對象會是該類的代理對象,需要的朋友可以參考下
    2023-09-09
  • 如何解決EasyExcel導出文件LocalDateTime報錯問題

    如何解決EasyExcel導出文件LocalDateTime報錯問題

    這篇文章主要介紹了如何解決EasyExcel導出文件LocalDateTime報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Java8中Optional的使用方法詳解

    Java8中Optional的使用方法詳解

    這篇文章主要介紹了Java8中Optional的使用方法詳解,傳統(tǒng)的寫代碼方式經常會遇到NullPointerException,這就需要我們在代碼中經常判空,而判空的寫法又會顯得很累贅,這里就可以用到Optional來簡化代碼,需要的朋友可以參考下
    2024-01-01
  • Java生成讀取條形碼和二維碼的簡單示例

    Java生成讀取條形碼和二維碼的簡單示例

    條形碼(barcode)是將寬度不等的多個黑條和空白,按照一定的規(guī)則排列,用來表示一組信息的圖形標識符,而二維碼大家應該都很熟悉了,這篇文章主要給大家介紹了關于Java生成讀取條形碼和二維碼的相關資料,需要的朋友可以參考下
    2021-07-07
  • 利用spring-data-redis實現(xiàn)incr自增的操作

    利用spring-data-redis實現(xiàn)incr自增的操作

    這篇文章主要介紹了利用spring-data-redis實現(xiàn)incr自增的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11

最新評論

慈利县| 东平县| 小金县| 利津县| 斗六市| 化德县| 广德县| 靖西县| 信阳市| 马鞍山市| 靖江市| 宝山区| 格尔木市| 祁连县| 黄山市| 綦江县| 友谊县| 乐昌市| 富民县| 诏安县| 彰化市| 上高县| 沾益县| 云龙县| 荆门市| 桂阳县| 石台县| 清涧县| 商丘市| 十堰市| 辰溪县| 凤庆县| 上饶县| 彰化县| 固镇县| 晋州市| 班玛县| 漳浦县| 民县| 泰州市| 梧州市|