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);
回豆;//
}
}