欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中Bean的加載與SpringBoot的初始化流程詳解

 更新時間:2021年11月17日 14:37:24   作者:逆襲的小學生  
這篇文章主要介紹了Spring中Bean的加載與SpringBoot的初始化流程詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

前言

一直對它們之間的關系感到好奇,SpringBoot既然是Spring的封裝,那么SpringBoot在初始化時應該也會有Bean的加載,那么是在何時進行加載的呢?

第一章 Spring中Bean的一些簡單概念

1.1 SpingIOC簡介

Spring啟動時去讀取應用程序提供的Bean配置信息,并在Spring容器中生成相應的Bean定義注冊表,然后根據注冊表去實例化Bean,裝配好Bean之間的依賴關系,為上層提供準備就緒的運行環(huán)境.

Spring提供一個配置文件描述Bean與Bean之間的依賴關系,利用Java語言的反射功能實例化Bean,并建立Bean之間的依賴關系.

1.2 BeanFactory

BeanFactory是接口,提供了IOC容器最基本的形式,給具體的IOC容器的實現提供了規(guī)范。

1.2.1 BeanDefinition

主要用來描述Bean的定義,Spring在啟動時會將Xml或者注解里Bean的定義解析成Spring內部的BeanDefinition.

beanClass保存bean的class屬性,scop保存bean是否單例,abstractFlag保存該bean是否抽象,lazyInit保存是否延遲初始化,autowireMode保存是否自動裝配,等等等

public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
        implements BeanDefinition, Cloneable {
    private volatile Object beanClass;
    private String scope = SCOPE_DEFAULT;
    private boolean abstractFlag = false;
    private boolean lazyInit = false;
    private int autowireMode = AUTOWIRE_NO;
    private int dependencyCheck = DEPENDENCY_CHECK_NONE;
    private String[] dependsOn;
    private ConstructorArgumentValues constructorArgumentValues;
    private MutablePropertyValues propertyValues;
    private String factoryBeanName;
    private String factoryMethodName;
    private String initMethodName;
    private String destroyMethodName;
}

1.2.2 BeanDefinitionRegistry

registerBeanDefinition方法主要是將BeanDefinition注冊到BeanFactory接口的實現類DefaultListableBeanFacory中的beanDefinitionMap中。

private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

1.2.3 BeanFactory結構圖

ListableBeanFactory該接口定義了訪問容器中Bean的若干方法,如查看Bean的個數,獲取某一類型Bean的配置名,查看容器中是否包括某一Bean等方法.

HierarchicalBeanFactory是父子級聯的IOC容器接口,子容器可以通過接口方法訪問父容器,通過HierarchicalBeanFactory接口SpringIOC可以建立父子層級關聯的IOC層級體系,子容器可以訪問父容器的Bean,父容器不能訪問子容器的Bean,比如展現層的Bean位于子容器中而業(yè)務層和持久層的Bean位于父容器的Bean.

  • ConfigurableBeanFactory:增強了IOC接口的可定制性,定義了設置類裝載器,屬性遍歷器,以及屬性初始化后置處理器等方法.
  • AutowireCapableBeanFactory:定義了將容器中的Bean按某種規(guī)則,按名字匹配,按類型匹配等.
  • SingletonBeanRegistry:允許在運行期間向容器注冊SingletonBean實例的方法.

通過這些接口也證明了BeanFactory的體系也確實提供了IOC的基礎及依賴注入和Bean的裝載等功能.

1.3 ApplicationContext

由于BeanFactory的功能還不夠強大,于是Spring在BeanFactory的基礎上還設計了一個更為高級的接口即ApplicationContext,它是BeanFactory的子接口之一.在我們使用SpringIOC容器時,大部分都是context的實現類。

我理解著就是BeanFactory只提供IOC,ApplicationContext還提供很多別的功能。

第二章 SpringBoot的初始化流程

@SpringBootApplication
public class RepApplication {
	public static void main(String[] args) {
        //要理解的SpringApplication
	SpringApplication.run(RepApplication.class, args);
	}
}

SpringApplication的run分為兩個階段,即new SpringApplication()時的執(zhí)行構造函數的準備階段,和run時的運行階段。

public static ConfigurableApplicationContext run(Class<?>[] primarySources,
			String[] args) {
		return new SpringApplication(primarySources).run(args);
	}

2.1 準備階段

在準備階段會

配置SpringBean的來源

推斷web應用類型

加載應用上下文初始器

加載應用事件監(jiān)聽器

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
        //推斷web應用類型
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
        //加載應用上下文初始化器
		setInitializers((Collection) getSpringFactoriesInstances(
				ApplicationContextInitializer.class));
        //加載應用事件監(jiān)聽器
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}

2.2 運行階段

  • 加載:SpringApplication獲得監(jiān)聽器
  • 運行:SpringApplication運行監(jiān)聽器
  • 監(jiān)聽:SpringBoot事件、Spring事件
  • 創(chuàng)建:應用上下文、Enviroment、其它(不重要),應用上下文創(chuàng)建后會被應用上下文初始化器初始化,Enviroment是抽象的環(huán)境對象。
  • 失?。汗收戏治鰣蟾?。
  • 回調:CommandLineRunner、ApplicationRunner
public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		configureHeadlessProperty();
        //獲得監(jiān)聽器
		SpringApplicationRunListeners listeners = getRunListeners(args);
        //運行監(jiān)聽器
		listeners.starting();
		try {
            //應用上下文
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
            //環(huán)境
			ConfigurableEnvironment environment = prepareEnvironment(listeners,
					applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
            //依據不同的配置加載不同的ApplicationContext
			context = createApplicationContext();
			exceptionReporters = getSpringFactoriesInstances(
					SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			prepareContext(context, environment, listeners, applicationArguments,
					printedBanner);
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass)
						.logStarted(getApplicationLog(), stopWatch);
			}
			listeners.started(context);
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, listeners);
			throw new IllegalStateException(ex);
		}
 
		try {
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

2.2.1 監(jiān)聽器分析

這個是看了源碼后的個人理解,不保證一定正確,只提供一定的參考。

在準備階段加載實現了ApplicationListener的監(jiān)聽器。

然后在運行階段調用了starting()方法。

//獲得監(jiān)聽器
SpringApplicationRunListeners listeners = getRunListeners(args);
//運行監(jiān)聽器
listeners.starting();

下面是listeners的源碼,可以看到它的每一個方法,都對應SpringBoot的一個階段,這表明每到對應的階段,都要廣播對應的事件。

class SpringApplicationRunListeners { 
	private final Log log; 
	private final List<SpringApplicationRunListener> listeners; 
	SpringApplicationRunListeners(Log log,
			Collection<? extends SpringApplicationRunListener> listeners) {
		this.log = log;
		this.listeners = new ArrayList<>(listeners);
	}
 
	public void starting() {
		for (SpringApplicationRunListener listener : this.listeners) {
			listener.starting();
		}
	}
 
	public void environmentPrepared(ConfigurableEnvironment environment) {
		for (SpringApplicationRunListener listener : this.listeners) {
			listener.environmentPrepared(environment);
		}
	}
 
	public void contextPrepared(ConfigurableApplicationContext context) {
		for (SpringApplicationRunListener listener : this.listeners) {
			listener.contextPrepared(context);
		}
	}
 
	public void contextLoaded(ConfigurableApplicationContext context) {
		for (SpringApplicationRunListener listener : this.listeners) {
			listener.contextLoaded(context);
		}
	}
 
	public void started(ConfigurableApplicationContext context) {
		for (SpringApplicationRunListener listener : this.listeners) {
			listener.started(context);
		}
	} 
	//省略... 
}

那么問題來了,廣播事件后,事件是怎么被監(jiān)聽到的呢?我們打開listener.environmentPrepared(environment)的源碼,發(fā)現其調用了initialMulticaster進行了事件廣播

@Override
	public void environmentPrepared(ConfigurableEnvironment environment) {
		this.initialMulticaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(
				this.application, this.args, environment));
	}

廣播的代碼如下,看了一下感覺大意就是找到根事件匹配的監(jiān)聽器,然后調用線程池去執(zhí)行對應的觸發(fā)函數。

@Override
	public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
		ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
		for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
			Executor executor = getTaskExecutor();
			if (executor != null) {
				executor.execute(() -> invokeListener(listener, event));
			}
			else {
				invokeListener(listener, event);
			}
		}
	}

2.2.2 refreshContext

再往下最核心的是refreshContext方法,一直點進去可以看到如下:

  • prepareRefresh:完成配置之類的解析,設置Spring的狀態(tài),初始化屬性源信息,驗證環(huán)境信息中必須存在的屬性.
  • ConfigurableListableBeanFactory:是用來獲取beanFactory的實例的(第一張也寫過BeanFactory負責bean的加載與獲?。?。
  • PrepareBeanFactory:對beanFactory進行相關的設置,為后續(xù)的使用做準備,包括設置classLoader用來加載Bean,設置表達式解析器等等.
  • postProcessBeanFactory:是用于在BeanFactory設置之后進行后續(xù)的BeanFactory的操作.
  • invokeBeanFactoryPostProcessors:點進去發(fā)現調用了如下方法,點進去之后發(fā)現邏輯相當復雜,主要調用工廠后處理器,調用Bean標簽,掃描Bean文件,并解析成一個個的Bean,這時候這些Bean是被加載進了Spirng容器當中,這里涉及了各種類,我們在這里主要說一下ConfigurationClassParser,主要是解析Bean的類.該方法會對帶有@configuration,@import,@bean,以及@SpringBootApplication等標簽的Bean進行解析,
  • registerBeanPostProcessors:會從Spring容器中找出實現BeanPostProcessors接口的Bean,并設置到BeanFactory的屬性之中,之后Bean實例化時會調用BeanProcessor,也就是Bean的后置處理器.會和AOP比較相關.
  • initMessageSource:初始化消息源(這個自己推斷的)
  • initApplicationEventMuticaster:初始化事件廣播器
  • onRefresh:是一個模板方法,不同的Spring容器會重寫它做不同的事情.比如web程序的容器,會調用create..方法去創(chuàng)建內置的servlet容器.
  • registerListeners:注冊事件監(jiān)聽器
  • finishBeanFactoryInitialization:會實例化BeanFactory中已被注冊但未被實例化的所有實例,懶加載是不需要被實例化的.前面的invokeBeanFactoryPostProcessors方法中根據各種注解解析出來的Bean在這個時候都會被初始化,同時初始化過程中的各種PostProcessor就會開始起作用了.
  • finishRefresh:會做初始化生命周期處理器相關的事情.
@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			prepareRefresh();
 
			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
 
			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);
 
			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);
 
				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);
 
				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);
 
				// Initialize message source for this context.
				initMessageSource();
 
				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();
 
				// Initialize other special beans in specific context subclasses.
				onRefresh();
 
				// Check for listener beans and register them.
				registerListeners();
 
				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);
 
				// Last step: publish corresponding event.
				finishRefresh();
			}
 
			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}
 
				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();
 
				// Reset 'active' flag.
				cancelRefresh(ex);
 
				// Propagate exception to caller.
				throw ex;
			}
 
			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
	}

當上面的代碼執(zhí)行完畢后,返回上層代碼,后面是注冊鉤子,這鉤子是希望開發(fā)者能結合自己的實際需求擴展出一些在Spring容器關閉時的行為.

private void refreshContext(ConfigurableApplicationContext context) {
		refresh(context);
		if (this.registerShutdownHook) {
			try {
				context.registerShutdownHook();
			}
			catch (AccessControlException ex) {
				// Not allowed in some environments.
			}
		}
	}

繼續(xù)返回上層代碼,可以看到afterRefresh方法它的方法體是空的, 也就說明Spring框架考慮了擴展性,留了很多的口子,讓大家在框架層面繼承很多的模塊并去做自定義的實現

protected void afterRefresh(ConfigurableApplicationContext context,
			ApplicationArguments args) {
	}

2.3 總結

總的來說,SpringBoot加載的Bean的時機為,點進一開始的run方法,層層遞進后,由

refreshContext(context);

進行了bean的加載,更詳細的話,那就層層遞進點進去,是在如下方法進行了bean的加載。

invokeBeanFactoryPostProcessors(beanFactory);

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Spring?代碼技巧梳理總結讓你愛不釋手

    Spring?代碼技巧梳理總結讓你愛不釋手

    這篇文章主要分享了Spring?代碼技巧梳理總結,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • SpringMVC一步到位精通攔截器

    SpringMVC一步到位精通攔截器

    攔截器(Interceptor)是一種動態(tài)攔截方法調用的機制,在SpringMVC中動態(tài)攔截控制器方法的執(zhí)行。本文將詳細講講SpringMVC中攔截器的概念及入門案例,感興趣的可以嘗試一下
    2022-12-12
  • Java由淺入深全面講解方法的使用

    Java由淺入深全面講解方法的使用

    方法,也稱函數,如果想要重復一段或者多段代碼塊的使用,可以將這些代碼封裝成一個方法,方法具體表現為某種行為,使用方法可以提高代碼的復用性
    2022-04-04
  • JAVA線上常見問題排查手段(小結)

    JAVA線上常見問題排查手段(小結)

    這篇文章主要介紹了JAVA線上常見問題排查手段(小結),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-07-07
  • 通過代碼實例解析JAVA類生命周期

    通過代碼實例解析JAVA類生命周期

    這篇文章主要介紹了通過代碼實例解析JAVA類生命周期,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • springboot中項目啟動時實現初始化方法加載參數

    springboot中項目啟動時實現初始化方法加載參數

    這篇文章主要介紹了springboot中項目啟動時實現初始化方法加載參數,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java實現控制小數精度的方法

    Java實現控制小數精度的方法

    這篇文章主要介紹了Java實現控制小數精度的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Java線程安全的計數器簡單實現代碼示例

    Java線程安全的計數器簡單實現代碼示例

    這篇文章主要介紹了Java線程安全的計數器簡單實現代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • RestTemplate使用不當引發(fā)的問題及解決

    RestTemplate使用不當引發(fā)的問題及解決

    這篇文章主要介紹了RestTemplate使用不當引發(fā)的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Springboot Redis設置key前綴的方法步驟

    Springboot Redis設置key前綴的方法步驟

    這篇文章主要介紹了Springboot Redis設置key前綴的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04

最新評論