spring ioc加载流程
一、總框架加載流程
? ? ?1.applicationContext創(chuàng)建beanFactory->
? ? ?2.beanFactory通過XMLbeandefineReader解析文件,獲取BeanDefinition,并注冊到beanfactory中。這時只是把定義放到工廠,并沒有真正生成實例。
? ? ?3.中間會調(diào)用beanfactoryPostProcesser的BEAN,在這種可以自定義加載擴展BEAN的流程。
? ? ?4.finishBeanFactoryInitialization對所有非延遲加載的單例BEAN進行實例化,此時會創(chuàng)建對象,然后會先調(diào)用beanPostProcesser的before接口,插入屬性,?然后注入屬性到對象中,后面調(diào)用beanPostProcesser的after接口修改屬性。通過beanWrapperImpl.
二、XML方式加載流程。
? ? 1.使用ClassPathXmlApplicationContext加載BEAN創(chuàng)建的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創(chuàng)建的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標簽時,會觸發(fā)composerScanParser,此類為自動掃描指定包下的component注解類到beanfactory,然后再注入configPostProcesser,
AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor,?internalEventListenerProcessor,
internalEventListenerFactory,
internalPersistenceAnnotationProcessor,
3.在每個BEAN實例化后,會首先調(diào)用系統(tǒng)中MergedBeanDefinitionPostProcessor所有接口的
postProcessMergedBeanDefinition,CommonAnnotationBeanPostProcessor類就會在這個實現(xiàn)中查找當前類所有帶有resource注解的屬性和方法,并且保存到InjectionMetadata中。并將屬性設置到BeanFactory的externPropertiesmap中,AutowiredAnnotationBeanPostProcessor則是查找?guī)в衋utowired,注解的屬性和方法,原理一樣。
4.在填充對象屬性時populateBean函數(shù)會調(diào)用所有實現(xiàn)
InstantiationAwareBeanPostProcessor接口的對象的postProcessProperties方法,將上面保存的屬性通過反射設置到對象屬性中。總結
以上是生活随笔為你收集整理的spring ioc加载流程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rtmp推流直播流程
- 下一篇: spring service ,cont