当前位置: 代码迷 >> 综合 >> Spring源码 --- AbstractBeanFactory通过beanName获取bean的过程(简版)----一个Bean创建的生命周期
  详细解决方案

Spring源码 --- AbstractBeanFactory通过beanName获取bean的过程(简版)----一个Bean创建的生命周期

热度:94   发布时间:2024-01-29 16:40:23.0

         前面那篇讲了很多具体的一些细节问题,主要是对自己梳理的各个细节问题进行详细的说明,加深影响。但并不适用只是想简单了解Bean创建过程的生命周期(spring创建过程对其的各种操作),这一片我们就将上一篇的内容压缩,我们就简单说明Spring是怎样创建Bean的以及提供了那些接口供我们自定义处理(主要看其提供给我们有哪些接口去处理)。

      1、通过名称获取Bean,实现是进行beanName的处理,这里就是看你是要获取FactoryBean本身还是要获取由其产生的对象。

      2、从三层缓存中获取单例对象,不能获取到。

      3、InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {@Nullabledefault Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {return null;}default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {return true;}@Nullabledefault PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {return pvs;}
}

       如果有自己实现了这个接口,这里就会运行其的postProcessBeforeInstantiation方法,让你自己去创建对象,如果你自己在这里没有返回return,就用你自己创建的对象,然后就直接返回了(不会再走Spring创建Bean的过程)。

      4、BeanPostProcessor接口的postProcessAfterInitialization方法

      当你如果有通过上面InstantiationAwareBeanPostProcessor 接口的postProcessBeforeInstantiation方法创建对象,就会在这里提前运行自己实现的BeanPostProcessor接口的postProcessAfterInitialization方法,可以通过这个方法再处理一次这个Bean。

public interface BeanPostProcessor {@Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}
}

    5、SmartInstantiationAwareBeanPostProcessor接口的determineCandidateConstructors方法

public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {@Nullabledefault Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {return null;}@Nullabledefault Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)throws BeansException {return null;}default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {return bean;}
}

        如果在上面没有通过InstantiationAwareBeanPostProcessor 接口去自己创建这个Bean对象(现在没有填充注入的属性,用三层缓存可以解决循环依赖的问题),其不会提前调用BeanPostProcessor接口的postProcessBeforeInitialization方法。Spring就会走其自己创建Bean的流程。这时候Spring提供了SmartInstantiationAwareBeanPostProcessor 接口让我们自己去产生创建Bean 时使用的构造函数(通过SmartInstantiationAwareBeanPostProcessor 的determineCandidateConstructors方法)。

   6、上面如果有自己实现InstantiationAwareBeanPostProcessor接口去创建构造函数,这里Spring接口直接用这个构造函数去创建对象。

   7、如果没有主动产生构造函数就Spring 自己去处理对象的创建。其主动去创建这个还没有进行属性赋值的对象。           

   8、MergedBeanDefinitionPostProcessor

       现在对象有了,下面就会通过Bean 的Class描叙BeanDefinition去进行属性赋值,这个时候Spring有提供一个MergedBeanDefinitionPostProcessor接口供你去处理这个BeanDefinition,通过其的postProcessMergedBeanDefinition方法。

public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);
}

   9、InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation

        之后Spring就会去进行Bean的属性填充,不过这个时候Spring还提供一个让你决定是否去进行属性填充,就是前面讲的InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation方法,返回false,就不进行Spring的属性填充。

   10、InstantiationAwareBeanPostProcessor接口的方法postProcessPropertyValues

      在进行属性填充的时候,这个时候Spring又会运行前面讲的InstantiationAwareBeanPostProcessor接口的方法postProcessPropertyValues。这个方法就是让你对属性PropertyValue(填充的属性值描叙)进行操作。

   11、BeanNameAware、BeanClassLoaderAware、BeanFactoryAware

在进行属性填充之后,Spring就会运行三种Aware接口,BeanNameAware、BeanClassLoaderAware、BeanFactoryAware(要注意BeanPostProcessor是工厂级别的接口,作用在所有Bean的创建,而Aware是需要实现在具体的Bean上面的)。

public interface Aware {}
public interface BeanFactoryAware extends Aware {void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
public interface BeanNameAware extends Aware {void setBeanName(String name);
}
public interface BeanClassLoaderAware extends Aware {void setBeanClassLoader(ClassLoader classLoader);
}

  12、BeanPostProcessor接口的postProcessBeforeInitialization

    现在就会再运行BeanPostProcessor接口的postProcessBeforeInitialization方法

  13、InitializingBean的afterPropertiesSet方法

public interface InitializingBean {void afterPropertiesSet() throws Exception;
}

     运行BeanPostProcessor接口的postProcessBeforeInitialization方法后,现在是运行InitializingBean的afterPropertiesSet方法,这个与Aware接口类似,是Bean级别的接口。

  14、initMethod方法的执行

    我们知道一个bean我们能在设置其的initMethod犯法、同样还有destroyMethod犯法。先在initMethod就现在运行(在afterPropertiesSet方法之后)。

  15、BeanPostProcessor接口的postProcessAfterInitialization方法

default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;
}

    现在会运行BeanPostProcessor接口的postProcessAfterInitialization方法,可以让你再处理一次这个Bean。(前面如果是自己创建Bean,会提前运行这个postProcessAfterInitialization方法,再将对象返回)。

  15、DestructionAwareBeanPostProcessor接口的requiresDestruction方法

public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;default boolean requiresDestruction(Object bean) {return true;}
}

现在整个Bean已经创建完成了。就会到支持为一次性Bean中。这个接口有执行条件,要不是多例。然后运行requiresDestruction方法,如果放回true,就将你自定义的这个类添加到BeanFactory容器中,然后在这个bean进行销毁的时候运行其的postProcessBeforeDestruction方法。

  16、上面这些都处理完了之后,会判断如果是FactoryBean接口的实现,如果是要获取FactoryBean本身就直接返回,如果是要获取其产生的对象,就会去调其的getObject方法,获取其产生的Bean对象。

public interface FactoryBean<T> {@NullableT getObject() throws Exception;@NullableClass<?> getObjectType();default boolean isSingleton() {return true;}
}

   至此,整个Bean获取过程就简单梳理完毕。如果要了解其中某些方面的具体细节,可以看上一篇文章。

     

  相关解决方案