• SpringIOCDebug查看Bean的实例化过程


    1 简介

      springIOC它是对bean进行管理。

      我们通常可以通过xml、properties、yml、注解等来配置bean的信息

      spring读取这些配置信息,解析,生成BeanDefinition(用来存储Bean的定义信息)

      根据beanDefinition来创建对象,设置属性....,最后把对象放入容器(BeanFactory)中,但我们需要使用bean时,从容器中那就可以了。

     2 bean

      在spring中,可以把bean分为两大类,普通对象和容器对象

    3 BeanFactory和FactoryBean

      它们两个都是用来创建对象的

      当使用BeanFactory的时候,必须遵循完整的过程,这个过程是由spring管理控制的

      当使用FactoryBean的时候,只需要调用getObject就可以返回对象,整个对象的创建过程是由用户自己定义的,更加灵活,它在getObject中自己去创建对象

    package org.springframework.beans.factory;
    
    import org.springframework.lang.Nullable;
    
    
    public interface FactoryBean<T> {
    
        
        String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
    
        
        @Nullable
        T getObject() throws Exception;
    
        
        @Nullable
        Class<?> getObjectType();
    
        
        default boolean isSingleton() {
            return true;
        }
    
    }

    4 BeanDefinition

      从配置中读取到bean的配置信息,通过BeanDefinitionReader解析,生成的存储bean定义信息的实例

    5 spring的bean默认是单例

      scope属性用来配置创建bean采用什么模式。

      spring创建对象时是采用反射创建的,且默认采用的是单例。

      spring Bean可以设置一个属性,叫做scope,可以设置四种变量。最常用的是前两种。singleton(单例-默认的)、prototype(每次获取都创建新的)

     6 aware方法

      它是用来获取当前对象的容器对象

      比如说我们要获取到bean的AplicationContext对象

      演示获取ApplicationContext容器对象

    public class BeanTest implements ApplicationContextAware {
    
        private Integer id;
    
        private String name;
    
        private ApplicationContext applicationContext;
    
        private BeanFactory beanFactory;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        public void setApplicationContext(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }
    
        public BeanFactory getBeanFactory() {
            return beanFactory;
        }
    
        public void setBeanFactory(BeanFactory beanFactory) {
            this.beanFactory = beanFactory;
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    
        <bean id="beantest" class="com.spring.test1.BeanTest">
            <property name="name" value="张三"></property>
            <property name="id" value="111"></property>
        </bean>
    
    </beans>
    public class Test1 {
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("Test.xml");
            BeanTest bean = context.getBean(BeanTest.class);
            System.out.println(bean.getName());
            System.out.println(bean.getApplicationContext());
        }
    
    }

    执行结果:

    张三
    org.springframework.context.support.ClassPathXmlApplicationContext@7e6cbb7a, started on Sun Mar 13 16:11:15 CST 2022

    我们获得了ApplicationContext对象。

    ApplicationContextAware 源码

    public interface ApplicationContextAware extends Aware {
        void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
    }

    执行aware方法,也就是执行setApplicationContext方法,会把容器对象ApplicationContext赋值给我们定义的applicationContext属性。

    7 postProcesser

    在实例化的过程中,怎么对beanfactory和bean的信息进行修改呢?

    可以使用 BeanFactoryPostProcesser和BeanPostProcesser

     演示

    package com.spring.test1;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    public class BeanTest implements ApplicationContextAware {
    
        private Integer id;
    
        private String name;
    
        private ApplicationContext applicationContext;
    
        private BeanFactory beanFactory;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        public void setApplicationContext(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }
    
        public BeanFactory getBeanFactory() {
            return beanFactory;
        }
    
        public void setBeanFactory(BeanFactory beanFactory) {
            this.beanFactory = beanFactory;
        }
    }
    package com.spring.test1;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.stereotype.Component;
    
    
    public class MyBeanFactoryPostProcesser implements BeanFactoryPostProcessor {
        public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
            System.out.println("自定义的BeanFactoryPostProcessor执行");
        }
    }
    package com.spring.test1;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.lang.Nullable;
    import org.springframework.stereotype.Component;
    
    
    public class MyBeanPostProcesser implements BeanPostProcessor {
    
         public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
             System.out.println("自定义的BeanPostProcessor的before");
            return bean;
        }
    
    
         public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
             System.out.println("自定义的BeanPostProcessor的after");
            return bean;
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    
        <bean id="beantest" class="com.spring.test1.BeanTest">
            <property name="name" value="张三"></property>
            <property name="id" value="111"></property>
        </bean>
    
        <bean id="myBeanFactoryPostProcesser" class="com.spring.test1.MyBeanFactoryPostProcesser">
    
        </bean>
    
        <bean id="myBeanPostProcesser" class="com.spring.test1.MyBeanPostProcesser">
    
        </bean>
    
    </beans>
    package com.spring.test1;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    public class Test1 {
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("Test.xml");
            BeanTest bean = context.getBean(BeanTest.class);
            System.out.println(bean.getName());
            System.out.println(bean.getApplicationContext());
        }
    
    }
    自定义的BeanFactoryPostProcessor执行
    自定义的BeanPostProcessor的before
    自定义的BeanPostProcessor的after
    张三
    org.springframework.context.support.ClassPathXmlApplicationContext@7e6cbb7a, started on Sun Mar 13 16:28:59 CST 2022
    
    Process finished with exit code 0

     8 init-method

    示例

    其它代码不变,在BeanTest定义一个方法

    package com.spring.test1;
    
    import com.sun.org.apache.xml.internal.security.Init;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    public class BeanTest implements ApplicationContextAware {
    
        private Integer id;
    
        private String name;
    
        private ApplicationContext applicationContext;
    
        private BeanFactory beanFactory;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        public void setApplicationContext(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }
    
        public BeanFactory getBeanFactory() {
            return beanFactory;
        }
    
        public void setBeanFactory(BeanFactory beanFactory) {
    
            this.beanFactory = beanFactory;
        }
    
    
        public void init(){
            System.out.println("init-method执行");
        }
    
    }

    在配置文件中加上init-method属性

    <bean id="beantest" class="com.spring.test1.BeanTest" init-method="init">
            <property name="name" value="张三"></property>
            <property name="id" value="111"></property>
        </bean>

     执行结果,看到init-method方法是在BeanPostProcessor的before和after之间执行的

    自定义的BeanFactoryPostProcessor执行
    自定义的BeanPostProcessor的before
    init-method执行
    自定义的BeanPostProcessor的after
    张三

    1 springIOC的bean的创建主要过程

      1 创建beanFactory容器

      2 加载配置文件,解析bean定义的信息,包装成BeanDefinition

      3 执行BeanFactoryPostProcesser

      4 准备工作,准备BeanPostProcessor,广播器,监听器

      5 实例化操作

      6 初始化操作

      7 获取对象

    2 下面通过debug来看整个过程,测试准备

    2.1 实体类

    public class BeanTest implements ApplicationContextAware {
    
        private Integer id;
    
        private String name;
    
        private ApplicationContext applicationContext;
    
        private BeanFactory beanFactory;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        public void setApplicationContext(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }
    
        public BeanFactory getBeanFactory() {
            return beanFactory;
        }
    
        public void setBeanFactory(BeanFactory beanFactory) {
            this.beanFactory = beanFactory;
        }
    }

    2.2 配置属性Test.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    
        <bean id="beantest" class="com.ruoyi.weixin.user.Test.BeanTest">
            <property name="name" value="张三"></property>
            <property name="id" value="111"></property>
        </bean>
    
    </beans>

    2.3 测试类

    public class SpringBeanInitTest {
    
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("Test.xml");
            BeanTest bean = context.getBean(BeanTest.class);
            System.out.println(bean.getName());
            System.out.println(bean.getApplicationContext());
        }
    
    }

    下面是debug的过程

    1 进入ClassPathXmlApplicationContext类,构造函数

    public ClassPathXmlApplicationContext(
                String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
                throws BeansException {
          //调用父类构造函数
            super(parent);
          //设置BeanDefinition的配置信息路径 setConfigLocations(configLocations);
    if (refresh) {
          //容器初始化 refresh(); } }

     

    2 进入refresh()方法

        @Override
        public void refresh() throws BeansException, IllegalStateException {
            synchronized (this.startupShutdownMonitor) {
                //前戏,做日容器刷新前的准备工作
            //1.设置容器的启动时间
            //2.设置活跃状态为true
            //3.设置关闭状态为false
            //4.获取Envirenment对象,并加载当前系统属性值到Envirenment
            //5.准备监听器和事件的集合对象,默认为空的集合
    prepareRefresh(); //创建容器对象ConfigurableListableBeanFactory
            //加载xml配置文件到当前容器对象中,最重要是就是BeanDefinition
    ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context.
           //BeanFactory准备工作,对各种属性进行填充
    prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses.
              //这是一个空方法,子类可以覆盖这个方法,在这个方法做额外的处理。此处我们自己一般不做额外的处理工作
    postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.
              //调用各种BeanFactory增强器
    invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.
              //注册bean处理器,这里只有注册功能,真正调用是getBean方法
    registerBeanPostProcessors(beanFactory); // Initialize message source for this context.
              //为上下文初始化message资源,即不同语言的消息体,国际化处理,简单来说就是为了支持不同语言(如中文、英文不同语言的展示)
    initMessageSource(); // Initialize event multicaster for this context.
              //初始化事件监听多路广播器
    initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
              //留给子类来初始化其它的bean
    onRefresh(); // Check for listener beans and register them.
              //在所有注册的bean中查找监听器bean,注册到消息广播中,也就是注册监听器
    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(); } } }

     

    2.1 第一个方法prepareRefresh

    //前戏,做日容器刷新前的准备工作(不用太在意)
    //1.设置容器的启动时间
    //2.设置活跃状态为true
    //3.设置关闭状态为false
    //4.获取Envirenment对象,并加载当前系统属性值到Envirenment
    //5.准备监听器和事件的集合对象,默认为空的集合
    protected void prepareRefresh() {
            // 设置容器启动时间.
            this.startupDate = System.currentTimeMillis();
        //容器的关闭标志位
    this.closed.set(false);
        //容器活跃状态
    this.active.set(true);     //记录日志 if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) { logger.trace("Refreshing " + this); } else { logger.debug("Refreshing " + getDisplayName()); } } //空的方法,留给子类实现,用来初始化属性资源(可以不管它) initPropertySources(); //创建并获取环境对象,验证需要的属性文件是否已放入环境对象中 getEnvironment().validateRequiredProperties(); // 判断刷新前的应用程序的监听器集合是否为空,如果为空,将监听器添加到集合中 if (this.earlyApplicationListeners == null) { this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners); } else { // 如果不为空,清空集合 this.applicationListeners.clear(); this.applicationListeners.addAll(this.earlyApplicationListeners); } // Allow for the collection of early ApplicationEvents, // to be published once the multicaster is available...
         //创建刷新前监听事件集合
    this.earlyApplicationEvents = new LinkedHashSet<>(); }

     

    2.2第二个方法obtainFreshBeanFactory(这里就完成了第一步BeanFactory对象的创建)

    1)创建容器对象ConfigurableListableBeanFactory
    2)加载xml配置文件到当前容器对象中,最重要是就是BeanDefinition

    往里面进入到方法refreshBeanFactory()
        protected final void refreshBeanFactory() throws BeansException {
        //如果存在BeanFactory,则销毁
    if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try {
          //创建DefaultListableBeanFactory对象 DefaultListableBeanFactory beanFactory
    = createBeanFactory();
          //为了序列化设置id,可以从id反序列化到BeanFactory对象 beanFactory.setSerializationId(getId());
          //定制BeanFactory,设置相关属性,包括是否允许覆盖同名称的不同定义的对象以及循环依赖 customizeBeanFactory(beanFactory);
          //初始化docunmentReader,并进行xml文件读取和解析,默认命名空间的解析,自定义标签的解析 loadBeanDefinitions(beanFactory);
    this.beanFactory = beanFactory; } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
    
    

    我们debug到loadBeanDefinitions(beanFactory)这里,此时查看beanFactory的两个属性,beanDefinitionMap和beanDefinitionMapNames,发现都是空的

     

     执行完loadBeanDefinitions(beanFactory)之后,再查看,发现大小变为1了

     打开beanDefinitionMap属性查看,发现名字为beantest的BeanDefinition,类型为BeanTest,正式我们在xml中定义的bean对象的信息

     

     beanclass往下找到propertyValues属性,可以看到name和id两个属性的值

     

     

     这里就完成了第二步,BeanDefinition已经加载完成了

     

     2.3 第三个方法prepareBeanFactory(beanFactory);

      这个方法先不深究,就是对当前beanFactory的一些属性来填充值

     

    2.4 第四个方法postProcessBeanFactory()

    这是一个空方法,子类可以覆盖这个方法,在这个方法做额外的处理。此处我们自己一般不做额外的处理工作
    不用管这个方法
    
    
    protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    }
    
    

    2.5 第五个方法invokeBeanFactoryPostProcessors(beanFactory);
    调用各种BeanFactory增强器

    这里面爱怎么执行怎么执行,不去看了,太多了

     

    2.6 第六个方法 registerBeanPostProcessors(beanFactory);

      注册bean处理器。注意这里只有注册功能,把BeanPostProcessors准备好,真正调用是getBean方法

     

    2.7 第七个方法 initMessageSource();

      为上下文初始化message资源,即不同语言的消息体,国际化处理,简单来说就是为了支持国际化(如中文、英文不同语言的展示)

      就好像这个网站,右侧有english和pyccknn(俄语)两种选项,可以转换语言 。它就是为了支持这个东西

      

     

    2.8 第八个方法initApplicationEventMulticaster();

      初始化事件监听多路广播器。

      广播事件给监听器用。

      这里也就是观察者模式。

     

    2.9 第九个方法  onRefresh();
      这是一个空方法,子类可以覆盖这个方法,在这个方法做额外的处理,来初始化其它的bean。

        protected void onRefresh() throws BeansException {
            // For subclasses: do nothing by default.
        }

     

    2.10 第十个方法 registerListeners();

    注册监听器。在所有注册的bean中查找监听器bean,注册到消息广播中

    2.11 第十一个方法finishBeanFactoryInitialization(beanFactory);
    初始化剩下的单实例(非懒加载的)
    更详细的创建过程:https://www.cnblogs.com/jthr/p/15919598.html
        protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
            // Initialize conversion service for this context.
            if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
                    beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
                beanFactory.setConversionService(
                        beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
            }
    
            // Register a default embedded value resolver if no bean post-processor
            // (such as a PropertyPlaceholderConfigurer bean) registered any before:
            // at this point, primarily for resolution in annotation attribute values.
            if (!beanFactory.hasEmbeddedValueResolver()) {
                beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
            }
    
            // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
            String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
            for (String weaverAwareName : weaverAwareNames) {
                getBean(weaverAwareName);
            }
    
            // Stop using the temporary ClassLoader for type matching.
            beanFactory.setTempClassLoader(null);
    
            // Allow for caching all bean definition metadata, not expecting further changes.
            beanFactory.freezeConfiguration();
    
            // Instantiate all remaining (non-lazy-init) singletons.
          //实例化剩下的非懒加载单利对象
    beanFactory.preInstantiateSingletons(); }
    
    

    方法前面做了一些列的set、add操作,还是在为BeanFactory做属性赋值,也就是还在做准备工作

    beanFactory.preInstantiateSingletons()方法:实例化剩下的非懒加载单利对象

    进入preInstantiateSingletons()方法

        @Override
        public void preInstantiateSingletons() throws BeansException {
            if (logger.isTraceEnabled()) {
                logger.trace("Pre-instantiating singletons in " + this);
            }
    
            // Iterate over a copy to allow for init methods which in turn register new bean definitions.
            // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
         //创建一个集合,将所有的BeanDefinition的名字放入集合
    List<String> beanNames = new ArrayList<>(this.beanDefinitionNames); // Trigger initialization of all non-lazy singleton beans...
        //触发所有非懒加载的单利bean的实例化,遍历集合的对象
    for (String beanName : beanNames) {
            //合并父类BeanDefinition RootBeanDefinition bd
    = getMergedLocalBeanDefinition(beanName);
            //条件判断 非抽象、单利、非懒加载
    if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
              //判断是否实现了FactoryBean接口
    if (isFactoryBean(beanName)) {
                //根据&+beanName来获取具体的对象 Object bean
    = getBean(FACTORY_BEAN_PREFIX + beanName);
                  //进行类型转换
    if (bean instanceof FactoryBean) { FactoryBean<?> factory = (FactoryBean<?>) bean;
                  //判断这个Factory是否需要立即初始化 boolean isEagerInit;
    if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged( (PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } } else { getBean(beanName); } } } // Trigger post-initialization callback for all applicable beans... for (String beanName : beanNames) { Object singletonInstance = getSingleton(beanName); if (singletonInstance instanceof SmartInitializingSingleton) { SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance; if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { smartSingleton.afterSingletonsInstantiated(); return null; }, getAccessControlContext()); } else { smartSingleton.afterSingletonsInstantiated(); } } } }

     

     这里,前面做了一些列的判断,做到了getBean方法,进入getBean方法

    @Override
        public Object getBean(String name) throws BeansException {
            return doGetBean(name, null, null, false);
        }

     

    再进入doGetBean方法。注意,在spring中,以do开头的方法,是实际上干活的方法

        protected <T> T doGetBean(
                String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
                throws BeansException {
    
            String beanName = transformedBeanName(name);
            Object bean;
    
            // Eagerly check singleton cache for manually registered singletons.
            Object sharedInstance = getSingleton(beanName);
            if (sharedInstance != null && args == null) {
                if (logger.isTraceEnabled()) {
                    if (isSingletonCurrentlyInCreation(beanName)) {
                        logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
                                "' that is not fully initialized yet - a consequence of a circular reference");
                    }
                    else {
                        logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
                    }
                }
                bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
            }
    
            else {
                // Fail if we're already creating this bean instance:
                // We're assumably within a circular reference.
                if (isPrototypeCurrentlyInCreation(beanName)) {
                    throw new BeanCurrentlyInCreationException(beanName);
                }
    
                // Check if bean definition exists in this factory.
                BeanFactory parentBeanFactory = getParentBeanFactory();
                if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                    // Not found -> check parent.
                    String nameToLookup = originalBeanName(name);
                    if (parentBeanFactory instanceof AbstractBeanFactory) {
                        return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
                                nameToLookup, requiredType, args, typeCheckOnly);
                    }
                    else if (args != null) {
                        // Delegation to parent with explicit args.
                        return (T) parentBeanFactory.getBean(nameToLookup, args);
                    }
                    else if (requiredType != null) {
                        // No args -> delegate to standard getBean method.
                        return parentBeanFactory.getBean(nameToLookup, requiredType);
                    }
                    else {
                        return (T) parentBeanFactory.getBean(nameToLookup);
                    }
                }
    
                if (!typeCheckOnly) {
                    markBeanAsCreated(beanName);
                }
    
                try {
                    RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                    checkMergedBeanDefinition(mbd, beanName, args);
    
                    // Guarantee initialization of beans that the current bean depends on.
                    String[] dependsOn = mbd.getDependsOn();
                    if (dependsOn != null) {
                        for (String dep : dependsOn) {
                            if (isDependent(beanName, dep)) {
                                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                        "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
                            }
                            registerDependentBean(dep, beanName);
                            try {
                                getBean(dep);
                            }
                            catch (NoSuchBeanDefinitionException ex) {
                                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                        "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
                            }
                        }
                    }
    
                    // Create bean instance.
                    if (mbd.isSingleton()) {
                        sharedInstance = getSingleton(beanName, () -> {
                            try {
                                return createBean(beanName, mbd, args);
                            }
                            catch (BeansException ex) {
                                // Explicitly remove instance from singleton cache: It might have been put there
                                // eagerly by the creation process, to allow for circular reference resolution.
                                // Also remove any beans that received a temporary reference to the bean.
                                destroySingleton(beanName);
                                throw ex;
                            }
                        });
                        bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                    }
    
                    else if (mbd.isPrototype()) {
                        // It's a prototype -> create a new instance.
                        Object prototypeInstance = null;
                        try {
                            beforePrototypeCreation(beanName);
                            prototypeInstance = createBean(beanName, mbd, args);
                        }
                        finally {
                            afterPrototypeCreation(beanName);
                        }
                        bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
                    }
    
                    else {
                        String scopeName = mbd.getScope();
                        if (!StringUtils.hasLength(scopeName)) {
                            throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
                        }
                        Scope scope = this.scopes.get(scopeName);
                        if (scope == null) {
                            throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
                        }
                        try {
                            Object scopedInstance = scope.get(beanName, () -> {
                                beforePrototypeCreation(beanName);
                                try {
                                    return createBean(beanName, mbd, args);
                                }
                                finally {
                                    afterPrototypeCreation(beanName);
                                }
                            });
                            bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                        }
                        catch (IllegalStateException ex) {
                            throw new BeanCreationException(beanName,
                                    "Scope '" + scopeName + "' is not active for the current thread; consider " +
                                    "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
                                    ex);
                        }
                    }
                }
                catch (BeansException ex) {
                    cleanupAfterBeanCreationFailure(beanName);
                    throw ex;
                }
            }
    
            // Check if required type matches the type of the actual bean instance.
            if (requiredType != null && !requiredType.isInstance(bean)) {
                try {
                    T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
                    if (convertedBean == null) {
                        throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
                    }
                    return convertedBean;
                }
                catch (TypeMismatchException ex) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Failed to convert bean '" + name + "' to required type '" +
                                ClassUtils.getQualifiedName(requiredType) + "'", ex);
                    }
                    throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
                }
            }
            return (T) bean;
        }

    这个方法进行一系列的处理之后,会调用方法createBean(beanName, mbd, args);

     

    进入createBean方法

        @Override
        protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
                throws BeanCreationException {
    
            if (logger.isTraceEnabled()) {
                logger.trace("Creating instance of bean '" + beanName + "'");
            }
            RootBeanDefinition mbdToUse = mbd;
    
            // Make sure bean class is actually resolved at this point, and
            // clone the bean definition in case of a dynamically resolved Class
            // which cannot be stored in the shared merged bean definition.
            Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
            if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
                mbdToUse = new RootBeanDefinition(mbd);
                mbdToUse.setBeanClass(resolvedClass);
            }
    
            // Prepare method overrides.
            try {
                mbdToUse.prepareMethodOverrides();
            }
            catch (BeanDefinitionValidationException ex) {
                throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
                        beanName, "Validation of method overrides failed", ex);
            }
    
            try {
                // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
                Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
                if (bean != null) {
                    return bean;
                }
            }
            catch (Throwable ex) {
                throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
                        "BeanPostProcessor before instantiation of bean failed", ex);
            }
    
            try {
                Object beanInstance = doCreateBean(beanName, mbdToUse, args);
                if (logger.isTraceEnabled()) {
                    logger.trace("Finished creating instance of bean '" + beanName + "'");
                }
                return beanInstance;
            }
            catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
                // A previously detected exception with proper bean creation context already,
                // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
                throw ex;
            }
            catch (Throwable ex) {
                throw new BeanCreationException(
                        mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
            }
        }
    再进行了一些列的处理,调用doCreateBean(beanName, mbdToUse, args)方法,这个方法就是实际创建bean的方法
    进入doCreateBean方法
        protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
                throws BeanCreationException {
    
            // Instantiate the bean.
            BeanWrapper instanceWrapper = null;
            if (mbd.isSingleton()) {
                instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
            }
            if (instanceWrapper == null) {
                instanceWrapper = createBeanInstance(beanName, mbd, args);
            }
            Object bean = instanceWrapper.getWrappedInstance();
            Class<?> beanType = instanceWrapper.getWrappedClass();
            if (beanType != NullBean.class) {
                mbd.resolvedTargetType = beanType;
            }
    
            // Allow post-processors to modify the merged bean definition.
            synchronized (mbd.postProcessingLock) {
                if (!mbd.postProcessed) {
                    try {
                        applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
                    }
                    catch (Throwable ex) {
                        throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                "Post-processing of merged bean definition failed", ex);
                    }
                    mbd.postProcessed = true;
                }
            }
    
            // Eagerly cache singletons to be able to resolve circular references
            // even when triggered by lifecycle interfaces like BeanFactoryAware.
            boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
                    isSingletonCurrentlyInCreation(beanName));
            if (earlySingletonExposure) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Eagerly caching bean '" + beanName +
                            "' to allow for resolving potential circular references");
                }
                addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
            }
    
            // Initialize the bean instance.
            Object exposedObject = bean;
            try {
                populateBean(beanName, mbd, instanceWrapper);
                exposedObject = initializeBean(beanName, exposedObject, mbd);
            }
            catch (Throwable ex) {
                if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
                    throw (BeanCreationException) ex;
                }
                else {
                    throw new BeanCreationException(
                            mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
                }
            }
    
            if (earlySingletonExposure) {
                Object earlySingletonReference = getSingleton(beanName, false);
                if (earlySingletonReference != null) {
                    if (exposedObject == bean) {
                        exposedObject = earlySingletonReference;
                    }
                    else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
                        String[] dependentBeans = getDependentBeans(beanName);
                        Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
                        for (String dependentBean : dependentBeans) {
                            if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
                                actualDependentBeans.add(dependentBean);
                            }
                        }
                        if (!actualDependentBeans.isEmpty()) {
                            throw new BeanCurrentlyInCreationException(beanName,
                                    "Bean with name '" + beanName + "' has been injected into other beans [" +
                                    StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
                                    "] in its raw version as part of a circular reference, but has eventually been " +
                                    "wrapped. This means that said other beans do not use the final version of the " +
                                    "bean. This is often the result of over-eager type matching - consider using " +
                                    "'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");
                        }
                    }
                }
            }
    
            // Register bean as disposable.
            try {
                registerDisposableBeanIfNecessary(beanName, bean, mbd);
            }
            catch (BeanDefinitionValidationException ex) {
                throw new BeanCreationException(
                        mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
            }
    
            return exposedObject;
        }
    
    

    里面又调用了方法 createBeanInstance(beanName, mbd, args);它是根据执行bean使用对应的策略创建新的实例。如工厂方法、构造方法、简单初始化

    进入createBeanInstance方法
    protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
            // Make sure bean class is actually resolved at this point.
          //确定需要创建的bean的实例的类可以实例化
    Class<?> beanClass = resolveBeanClass(mbd, beanName); if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean class isn't public, and non-public access not allowed: " + beanClass.getName()); }     //判断当前Definition是否包含实例供应器。此处相当于一个回调方法,利用回调方法来创建bean Supplier<?> instanceSupplier = mbd.getInstanceSupplier(); if (instanceSupplier != null) { return obtainFromSupplier(instanceSupplier, beanName); }     //如果工厂方法不为空,使用工厂方法策略来初始化策略 if (mbd.getFactoryMethodName() != null) { return instantiateUsingFactoryMethod(beanName, mbd, args); } // Shortcut when re-creating the same bean... boolean resolved = false; boolean autowireNecessary = false; if (args == null) { synchronized (mbd.constructorArgumentLock) { if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } } if (resolved) { if (autowireNecessary) { return autowireConstructor(beanName, mbd, null, null); } else { return instantiateBean(beanName, mbd); } } // Candidate constructors for autowiring?
        //获取构造方法
    Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } // Preferred constructors for default construction? ctors = mbd.getPreferredConstructors(); if (ctors != null) { return autowireConstructor(beanName, mbd, ctors, null); } // No special handling: simply use no-arg constructor. return instantiateBean(beanName, mbd); }
    前面做了一系列的处理,获取了class对象,获取构造器,最后调用instantiateBean(beanName, mbd)方法
    进入instantiateBean方法
    protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
            try {
                Object beanInstance;
                if (System.getSecurityManager() != null) {
                    beanInstance = AccessController.doPrivileged(
                            (PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
                            getAccessControlContext());
                }
                else {
              //获取实例化策略并进行实例化操作 beanInstance
    = getInstantiationStrategy().instantiate(mbd, beanName, this); } BeanWrapper bw = new BeanWrapperImpl(beanInstance); initBeanWrapper(bw); return bw; } catch (Throwable ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex); } }

    里面调用beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);

    进入instantiate方法

    @Override
        public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
            // Don't override the class with CGLIB if no overrides.
            if (!bd.hasMethodOverrides()) {
                Constructor<?> constructorToUse;
                synchronized (bd.constructorArgumentLock) {
                    constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
                    if (constructorToUse == null) {
                        final Class<?> clazz = bd.getBeanClass();
                        if (clazz.isInterface()) {
                            throw new BeanInstantiationException(clazz, "Specified class is an interface");
                        }
                        try {
                            if (System.getSecurityManager() != null) {
                                constructorToUse = AccessController.doPrivileged(
                                        (PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
                            }
                            else {
                  //获取无参构造器 constructorToUse
    = clazz.getDeclaredConstructor(); } bd.resolvedConstructorOrFactoryMethod = constructorToUse; } catch (Throwable ex) { throw new BeanInstantiationException(clazz, "No default constructor found", ex); } } } return BeanUtils.instantiateClass(constructorToUse); } else { // Must generate CGLIB subclass. return instantiateWithMethodInjection(bd, beanName, owner); } }

    里面又获取了无参构造,后面调用了BeanUtils.instantiateClass(constructorToUse);

    进入instantiateClass方法

    public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
            Assert.notNull(ctor, "Constructor must not be null");
            try {
                ReflectionUtils.makeAccessible(ctor);
                if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
                    return KotlinDelegate.instantiateClass(ctor, args);
                }
                else {
                    Class<?>[] parameterTypes = ctor.getParameterTypes();
                    Assert.isTrue(args.length <= parameterTypes.length, "Can't specify more arguments than constructor parameters");
                    Object[] argsWithDefaultValues = new Object[args.length];
                    for (int i = 0 ; i < args.length; i++) {
                        if (args[i] == null) {
                            Class<?> parameterType = parameterTypes[i];
                            argsWithDefaultValues[i] = (parameterType.isPrimitive() ? DEFAULT_TYPE_VALUES.get(parameterType) : null);
                        }
                        else {
                            argsWithDefaultValues[i] = args[i];
                        }
                    }
                    return ctor.newInstance(argsWithDefaultValues);
                }
            }
            catch (InstantiationException ex) {
                throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex);
            }
            catch (IllegalAccessException ex) {
                throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex);
            }
            catch (IllegalArgumentException ex) {
                throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex);
            }
            catch (InvocationTargetException ex) {
                throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException());
            }
        }

    看到里面调用了return ctor.newInstance(argsWithDefaultValues);通过构造器创建了实例。这么深,终于看到了创建实例操作

    回到doCreateBean方法,执行完了createBeanInstance,往下看

    populateBean(beanName, mbd, instanceWrapper);

    接下来执行了populateBean方法,这个方法是给bean的属性赋值的。

    在这里执行后,id和name就有值了。application和beanFactory两个属性还没有值

    接下来又执行了方法exposedObject = initializeBean(beanName, exposedObject, mbd);方法

    进入initializeBean

        protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                    invokeAwareMethods(beanName, bean);
                    return null;
                }, getAccessControlContext());
            }
            else {
                invokeAwareMethods(beanName, bean);
            }
    
            Object wrappedBean = bean;
            if (mbd == null || !mbd.isSynthetic()) {
                wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
            }
    
            try {
                invokeInitMethods(beanName, wrappedBean, mbd);
            }
            catch (Throwable ex) {
                throw new BeanCreationException(
                        (mbd != null ? mbd.getResourceDescription() : null),
                        beanName, "Invocation of init method failed", ex);
            }
            if (mbd == null || !mbd.isSynthetic()) {
                wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
            }
    
            return wrappedBean;
        }

    里面执行了invokeAwareMethods(beanName, bean);

    也就是执行aware方法。

    该方法执行完成后,beanFactory赋值了,而applicationContext属性还没有值

    我们进入invokeAwareMethods方法,发现它只调用了setBeanNamesetBeanClassLoader,setBeanFactory,所以只会给beanName,beanClassLoader、beanFactory赋值

    private void invokeAwareMethods(String beanName, Object bean) {
            if (bean instanceof Aware) {
                if (bean instanceof BeanNameAware) {
                    ((BeanNameAware) bean).setBeanName(beanName);
                }
                if (bean instanceof BeanClassLoaderAware) {
                    ClassLoader bcl = getBeanClassLoader();
                    if (bcl != null) {
                        ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
                    }
                }
                if (bean instanceof BeanFactoryAware) {
                    ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
                }
            }
        }

    invokeAwareMethods执行完成,我们继续看initializeBean方法,接下来执行了applyBeanPostProcessorsBeforeInitialization方法

    wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);

    执行了BeanPostProcessors里面的before,

    这个方法会为applicationContext属性赋值

    在下面又会执行invokeInitMethods(beanName, wrappedBean, mbd);方法

    这里会执行bean的init方法

    再下一步执行applyBeanPostProcessorsAfterInitialization

    wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

    执行了BeanPostProcessors里面的after

    这样子,就有了创建bean的完整脉络。

     
  • 相关阅读:
    超简单本地mock假数据测试,模拟后台数据返回必杀技
    /usr/local/lib/ruby/gems/2.4.0/gems/cocoapods-1.5.3/lib/cocoapods/command.rb:118:in `git_version': Failed to extract git version from `git --version`
    从小白到大神程序员必读15本经典电子书免费送第三季(先到先得)
    从小白到大神程序员必读15本经典电子书免费送第二季(先到先得)
    小白到大神程序员必读15本经典电子书免费送第一季(先到先得)
    在ios Xcode10下小白都能解决library not found for -libstdc++.6.0.9
    52个有效方法(8)
    52个有效方法(7)
    52个有效方法(6)
    52个有效方法(5)
  • 原文地址:https://www.cnblogs.com/jthr/p/15910423.html
Copyright © 2020-2023  润新知