日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring ioc加载流程

發布時間:2025/3/19 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring ioc加载流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、總框架加載流程

? ? ?1.applicationContext創建beanFactory->

? ? ?2.beanFactory通過XMLbeandefineReader解析文件,獲取BeanDefinition,并注冊到beanfactory中。這時只是把定義放到工廠,并沒有真正生成實例。

? ? ?3.中間會調用beanfactoryPostProcesser的BEAN,在這種可以自定義加載擴展BEAN的流程。

? ? ?4.finishBeanFactoryInitialization對所有非延遲加載的單例BEAN進行實例化,此時會創建對象,然后會先調用beanPostProcesser的before接口,插入屬性,?然后注入屬性到對象中,后面調用beanPostProcesser的after接口修改屬性。通過beanWrapperImpl.

二、XML方式加載流程。

? ? 1.使用ClassPathXmlApplicationContext加載BEAN創建的XML文件。

ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring1.xml"); PeopleBean peopleBean = (PeopleBean) classPathXmlApplicationContext.getBean("people1"); System.out.println(" name: "+ peopleBean.getName()); IPeopleService peopleService = (IPeopleService) classPathXmlApplicationContext.getBean("peopleService");

三、通過注解掃描加載流程。

? ? 1.使用ClassPathXmlApplicationContext加載BEAN創建的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:aop="http://www.springframework.org/schema/aop"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--使用context命名空間,通知spring掃描指定目錄,進行注解的解析--><context:component-scan base-package="com.tpw.newday.service.people"/><bean id="people1" class="com.tpw.newday.bean.PeopleBean"><property name="name" value="zhangsan"/></bean><bean id="people2" class="com.tpw.newday.bean.PeopleBean"><property name="name" value="lisi"/></bean><bean id="people3" class="com.tpw.newday.bean.PeopleBean"><property name="name" value="wangwu"/></bean><bean id="peopleConfig" class="com.tpw.newday.config.PeopleConf"></bean><bean id="peopleService" class="com.tpw.newday.service.people.PeopleServiceImpl"><property name="peopleBean1" ref="people1"/><property name="peopleBean2" ref="people2"/></bean></beans>

PeopleServiceImpl源碼:

package com.tpw.newday.service.people;import cn.hutool.json.JSONUtil; import com.tpw.newday.bean.PeopleBean; import lombok.Data; import org.springframework.beans.BeansException; import org.springframework.beans.factory.*;import javax.annotation.Resource;/*** <h3>newday</h3>* <p></p>** @author : lipengyao* @date : 2021-06-25 09:53:24**/@Data public class PeopleServiceImpl implements IPeopleService,InitializingBean,BeanNameAware,BeanClassLoaderAware ,BeanFactoryAware{private PeopleBean peopleBean1;private PeopleBean peopleBean2;@Resource(name = "people3")private PeopleBean peopleBean3;@Overridepublic void save(PeopleBean peopleBean) {System.out.println("PeopleServiceImpl--> save peopleBean:" + JSONUtil.toJsonStr(peopleBean));}@Overridepublic PeopleBean getPeople(int index) {PeopleBean peopleBean = null;switch (index){case 1:peopleBean = peopleBean1;break;case 2:peopleBean = peopleBean2;break;case 3:peopleBean = peopleBean3;break;}System.out.println("PeopleServiceImpl--> getPeople peopleBean:" + JSONUtil.toJsonStr(peopleBean));return peopleBean;}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("PeopleServiceImpl--> afterPropertiesSet PeopleServiceImpl:" );}@Overridepublic void setBeanClassLoader(ClassLoader classLoader) {System.out.println(" PeopleServiceImpl-->setBeanClassLoader name:" + classLoader.getClass().getName());}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {PeopleBean peopleBeanxx = (PeopleBean) beanFactory.getBean("people1");System.out.println(" PeopleServiceImpl-->setBeanFactory name:" + peopleBeanxx.getName());}@Overridepublic void setBeanName(String name) {System.out.println(" PeopleServiceImpl--> setBeanName name:" + name );} }

2.spring的XML解析器解析component-scan標簽時,會觸發composerScanParser,此類為自動掃描指定包下的component注解類到beanfactory,然后再注入configPostProcesser,

AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor,

?internalEventListenerProcessor,

internalEventListenerFactory,

internalPersistenceAnnotationProcessor,

3.在每個BEAN實例化后,會首先調用系統中MergedBeanDefinitionPostProcessor所有接口的

postProcessMergedBeanDefinition,

CommonAnnotationBeanPostProcessor類就會在這個實現中查找當前類所有帶有resource注解的屬性和方法,并且保存到InjectionMetadata中。并將屬性設置到BeanFactory的externPropertiesmap中,AutowiredAnnotationBeanPostProcessor則是查找帶有autowired,注解的屬性和方法,原理一樣。

4.在填充對象屬性時populateBean函數會調用所有實現

InstantiationAwareBeanPostProcessor接口的對象的postProcessProperties方法,將上面保存的屬性通過反射設置到對象屬性中。

總結

以上是生活随笔為你收集整理的spring ioc加载流程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。