关于vue3:2022全面升级Vue3-TS-仿知乎专栏企业级项目完结内置文档

1次阅读

共计 6626 个字符,预计需要花费 17 分钟才能阅读完成。

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);

回豆;//
}

}

正文完
 0