2022全面升级Vue3 + TS 仿知乎专栏企业级项目完结内置文档

download:2022全面升级Vue3 + TS 仿知乎专栏企业级项目完结内置文档

Spring5源代码5-Bean生命周期后处理器

主要解释三种生命周期增强剂:

bean factorypostprocessor:bean factory后处理器
bean definitionregistrypostprocessor:bean定义注册后处理器
插手容器的启动
豆后处理器:豆后处理器
bean后处理器
MergedBeanDefinitionPostProcessor
smartinstantiationwarebeanpostprocessor
instantiationwarebeanpostprocessor
回调接口
DisposableBean(我们暂时不解释销毁方案)
1.1什么是BeanPostProcessor?
BeanPostProcessor是Spring提供的一个非常重要的扩展接口,Spring中的很多功能也是通过BeanPostProcessor完成的(目前看到的最典型的就是AnnotationAwareaspectJaoutProxyCreator的注入)。

1.2 bean post处理器的类型
BeanPostProcessor在Spring中有很多子类(idea中有46个),比如

InstantiationAware后处理器适配器:在Spring的bean加载过程中起着非常重要的作用。
AnnotationawareaspectjautoproxyCreator:当属性在bean创建期间被注入时,它起作用。
aspectjawaareadvisorrotoproxycreator:Aspect的AOP功能也取决于BeanPostProcessor的特性。
1.3创造机会
BeanFactoryPostProcessor:在Spring开始时介入BeanDefinition的创建。

beanPostProcessor:首先,创建与Bean对应的Bean定义。第二个是Bean实例的创建。在Spring容器中,Bean的创建不仅仅是通过反射来完成的,在创建过程中还需要考虑Spring容器中Bean的一些属性,所以BeanDefinition中不仅包含了Bean类文件的信息,还包含了Spring容器中当前Bean的一些属性,比如容器中的作用域、懒加载、别名等信息。当Bean被实例化时,它需要依赖相应的BeanDefinition来提供相应的信息。。

BeanPostProcessor参与了Bean的创建过程。所以必须在普通Bean之前创建。实际上,BeanPostProcessor是在Spring启动时刷新容器时创建的。

BeanPostProcessor的BeanDefinition创建计时和普通的Beans没有什么区别,都是在Spring启动时在BeanFactoryPostProcessor中完成的(准确的说是在ConfigurationClassPostProcessor中完成的)。

但是,BeanPostProcessor的实例创建优先于普通bean的创建,在Spring启动期间将调用abstractapplicationcontext # registerbeanpostprocessors方法。在这个方法中,Spring将从容器中获取BeanPostProcessor类型的所有beanName,通过beanFactory.getBean方法获取相应的实例,对它们进行排序,并在BeanFactory.beanPostProcessors属性中注册它们。当容器需要执行BeanPostProcessor方法时,可以直接从beanPostProcessors中获取。

2.案例
定义几个测试类来实现bean的后处理器:

bean definition registry post processor:

/**

  • BeanFactory的后处理器,优先级排序,已排序
    */
    @组件
    公共类mybean definition registry postprocessor实现bean definition registry postprocessor {
    公共mybean definitionregistrypostprocessor(){
    system . out . println(" mybean definition registry postprocessor ");
    }

@Override //立即执行
public void postprocessbean factory(configurablelistablebean factory)抛出BeansException {
system . out . println(" mybean definition registry postprocessor....postProcessBeanFactory ... ");
}

@Override //首先执行
public void postprocessbean definition registry(bean definition registry)抛出BeansException {
system . out . println(" mybean definition registry postprocessor...postprocessbean definition registry ... ");
//增强bean定义信息的注册,比如自己注册组件。

}
}
复制代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
BeanFactoryPostProcessor:

/**

  • BeanFactory的后处理器
    */
    @组件
    公共类MyBeanFactoryPostProcessor实现BeanFactoryPostProcessor {

公共MyBeanFactoryPostProcessor(){
system . out . println(" MyBeanFactoryPostProcessor ... ");
}

@覆盖
public void postprocessbean factory(configurablelistablebean factory)抛出BeansException {
system . out . println(" BeanFactoryPostProcessor....postprocessbean factory = = > "+bean factory);
}
}
复制代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
BeanPostProcessor:

/**
*后处理器;豆类成分;
*/
@组件
公共类MyBeanPostProcessor实现BeanPostProcessor {

公共MyBeanPostProcessor(){
system . out . println(" MyBeanPostProcessor ... ");
}

公共对象后处理AfterInitialization(对象bean,字符串beanName)抛出bean异常{
system . out . println(" MyBeanPostProcessor...postProcessAfterInitialization ... "+bean+" = = > "+bean name);
回豆;
}

public Object postprocessbefore initial ization(Object bean,String beanName)抛出BeansException {
system . out . println(" MyBeanPostProcessor...postprocessbeforeininitialization ... "+bean+" = = > "+bean name);
回豆;//new Object();
}
}
复制代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
instantiationwarebeanpostprocessor:

@组件
公共类MyInstantiationAwareBeanPostProcessor实现InstantiationAwareBeanPostProcessor {

公共MyInstantiationAwareBeanPostProcessor(){
system . out . println(" MyInstantiationAwareBeanPostProcessor ... ");
}

//初始化之前进行后期处理,Spring留给我们一个回调,为这个组件创建一个对象。
公共对象postProcessBeforeInstantiation(类beanClass,字符串beanName)抛出BeansException {
system . out . println(" MyInstantiationAwareBeanPostProcessor...postprocessbeforeantitation = > "+bean class+"-"+bean name);
//if(class . isass from(cat . class)){ return new Dog()}
//如果我们自己创建对象,则返回它。Spring不会帮我们创建对象,用我们自己的对象?我们创建的这个对象,Spring会保存单个实例?还是每次都是getBean传送给我们来创建一个新的?
返回null
}

//是否希望剩余的后处理器继续处理bean?
public boolean postProcessAfterInstantiation(对象bean,字符串beanName)抛出BeansException {
//预先更改Spring不关心的某个bean中的属性
system . out . println(" MyInstantiationAwareBeanPostProcessor...postProcessAfterInstantiation = > "+bean+"-"+bean name);
返回true//如果返回false,将完成所有bean赋值。
}
//解析属性值注入的自定义批注;Pvs封装了所有的属性信息。
公共属性值后处理属性(属性值pvs,对象bean,字符串beanName)
throws beans exception {//@ GuiguValue();存储
system . out . println(" MyInstantiationAwareBeanPostProcessor...postProcessProperties = > "+bean+"-"+bean name);
返回null
}

//public property values postProcessPropertyValues(
// PropertyValues pvs,PropertyDescriptor[] pds,Object bean,String beanName)抛出BeansException {
//system . out . println(" MyInstantiationAwareBeanPostProcessor...postProcessProperties ");
//返回pvs
// }
}
复制代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
MergedBeanDefinitionPostProcessor:

@组件
公共类mymergedbeanditionpostprocessor实现MergedBeanDefinitionPostProcessor {

public mymergedbeanditionpostprocessor(){
system . out . println(" mymergedbeanditionpostprocessor ... ");
}

@覆盖
public Object postprocessbefore initial ization(Object bean,String beanName)抛出BeansException {
system . out . println(" mymergedbeanditionpostprocessor...初始化前的后处理...= > "+bean+"-"+bean name);
回豆;//null
}

@覆盖
公共对象后处理AfterInitialization(对象bean,字符串beanName)抛出bean异常{
system . out . println(" mymergedbeanditionpostprocessor...后处理软化..= > "+bean+"-"+bean name);
返回null
}

@覆盖
public void postprocessmergedbean definition(rootbean definition bean definition,Class beanType,String beanName) {
system . out . println(" mymergedbeanditionpostprocessor...postprocessmergedbean definition..= > "+bean name+"-"+bean type+"-"+bean definition);
}

@覆盖
public void resetbean definition(String bean name){
system . out . println(" mymergedbeanditionpostprocessor...resetBeanDefinition ..”+bean name);

}
}
复制代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
SmartInstantiationAwareBeanPostProcessor:

复制
在代理增强过程中使用了@Component //bean
公共类MySmartInstantiationAwareBeanPostProcessor实现SmartInstantiationAwareBeanPostProcessor {

public MySmartInstantiationAwareBeanPostProcessor(){
system . out . println(" MySmartInstantiationAwareBeanPostProcessor ... ");
}

//预测bean的类型,最后一次更改组件类型。
公共类predictBeanType(类beanClass,字符串beanName)抛出BeansException {
system . out . println(" MySmartInstantiationAwareBeanPostProcessor...predict bean type = > "+bean class+"-"+bean name);
返回null
}

//返回我们想要使用的构造函数的候选列表
公共构造函数[]determinecandidate constructors(类beanClass,字符串beanName)抛出BeansException {
system . out . println(" MySmartInstantiationAwareBeanPostProcessor...determinecandidate constructors = > "+bean class+"-"+bean name);
//返回我们指定的构造函数
返回null
}

//返回早期bean引用,在三级缓存中定义bean信息。
公共对象getEarlyBeanReference(对象bean,字符串beanName)抛出BeansException {
system . out . println(" MySmartInstantiationAwareBeanPostProcessor...getEarlyBeanReference = > "+bean+"-"+bean name);

回豆;//
}

}

你可能感兴趣的:(vue3)