springboot之BeanPostProcessor功能及例子(一)

阅读: 评论:0

springboot之BeanPostProcessor功能及例子(一)

springboot之BeanPostProcessor功能及例子(一)

==> 学习汇总(持续更新)
==> 从零搭建后端基础设施系列(一)-- 背景介绍


一、BeanPostProcessor

字面上的意思是bean的后置处理器,什么意思呢?其实就是说,在bean的生命周期中,可以对它干什么。再简单点就是,bean初始化之前干点什么,之后又干点什么。

public interface BeanPostProcessor {@Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {//bean初始化之前,要干点什么或者什么都不干,但是干不干都要把人家给返回回去,不能一进来就出不去了~return bean;}@Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {//bean已经初始化完了,这时候你想干点什么或者什么都不干,和上面一样,都要给人家返回回去return bean;}
}

简单示例

public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@Componentstatic class MyBeanPostProcessor1 implements BeanPostProcessor {@Override@Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之前");return bean;}@Override@Nullablepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之后");return bean;}}
}

二、InstantiationAwareBeanPostProcessor

字面上的意思是实例化bean后置处理器,什么意思呢?就是说你可以在bean实例化前后做一些事情。注意,是先实例化才到初始化的!

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {//the bean object to expose instead of a default instance of the target bean, or  null to proceed with default instantiation@Nullabledefault Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {//如果什么都不做,返回null,代表走默认实例化的逻辑//如果在这里做了什么,那么就返回你指定实例化逻辑产生的对象 return null;}//true if properties should be set on the bean,false if property population should be skipped. Normal implementations should return truedefault boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {//实例化完了之后,如果这里返回true,代表这个bean的属性会被正常set进去,如果是false,那么就会跳过,spring给的建议是一般都要返回true。return true;}//the actual property values to apply to the given bean (can be the passed-in PropertyValues instance), or null which proceeds with the existing properties  but specifically continues with a call to postProcessPropertyValues@Nullabledefault PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)throws BeansException {//null,相当于什么都没做//返回不是null,相当于将返回的变量值替换原来的变量赋值 return null;}//deprecated as of 5.1, in favor of {@link #postProcessProperties(PropertyValues, Object, String)}@Deprecated@Nullabledefault PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {return pvs;}
}

简单示例

public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@Componentstatic class MyBeanPostProcessor2 implements InstantiationAwareBeanPostProcessor {@Override@Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之前");return bean;}@Override@Nullablepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之后");return bean;}@Override@Nullablepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {System.out.println("bean 实例化之前");return null;}@Overridepublic boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {System.out.println("bean 实例化之后");return true;}@Override@Nullablepublic PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)throws BeansException {System.out.println("bean 变量处理");return null;}}
}

三、SmartInstantiationAwareBeanPostProcessor

字面意思就是智能实例化bean后置处理器,什么意思呢?其实就是比InstantiationAwareBeanPostProcessor多三个接口,在bean实例化之前,多做一些事情而已。

public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {//return the type of the bean, or null if not predictable@Nullabledefault Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {//实例化之前先预测bean的类型??(我也不是很懂它的用途,下篇文章讲例子的时候再顺带解决)return null;}//return the candidate constructors, or  null if none specified@Nullabledefault Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)throws BeansException {//说白了就是可以决定用哪个构造器来实例化这个beanreturn null;}//return the object to expose as bean reference typically with the passed-in bean instance as default)default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {//这个就牛逼了,可以解决循环引用的问题,例如A引用了B,B引用了A,A实例化的时候,需要先实例化B,B实例化的时候又需要去实例化A,当B->实例化A的时候,就会走到这个方法,提前把A的引用暴露出去,这时候B就可以完成实例化,最后A也完成的实例化。return bean;}

简单示例

public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@Componentstatic class MyBeanPostProcessor3 implements SmartInstantiationAwareBeanPostProcessor {@Override@Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之前");return bean;}@Override@Nullablepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之后");return bean;}@Override@Nullablepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {System.out.println("bean 实例化之前");return null;}@Overridepublic boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {System.out.println("bean 实例化之后");return true;}@Override@Nullablepublic PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)throws BeansException {System.out.println("bean 变量处理");return null;}@Override@Nullablepublic Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {System.out.println("预测 bean 类型");return null;}@Override@Nullablepublic Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)throws BeansException {System.out.println("选择构造器");return null;}@Overridepublic Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {System.out.println("提前暴露引用");return bean;}}@Componentstatic class B {@AutowiredA a;}@Componentstatic class A {@AutowiredB b;}
}

四、DestructionAwareBeanPostProcessor

字面上的意思就是销毁bean后置处理器,什么意思呢?就是销毁bean之前,你还想干嘛。

public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;//return true} if postProcessBeforeDestruction is supposed to be called for this bean instance eventually, or false if not neededdefault boolean requiresDestruction(Object bean) {//默认是true,也就是需要销毁之前做点什么,会调用postProcessBeforeDestruction//如果是false,那么就不会调用return true;}

简单示例

public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@Componentstatic class MyBeanPostProcessor4 implements DestructionAwareBeanPostProcessor {@Override@Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之前");return bean;}@Override@Nullablepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之后");return bean;}@Overridepublic void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {System.out.println(beanName + " 销毁之前");}@Overridepublic boolean requiresDestruction(Object bean) {return true;}}
}

五、MergedBeanDefinitionPostProcessor

字面上意思是合并bean定义后置处理器,什么意思呢?目前我也还不太懂~,哈哈,等下一篇讲例子再详细说一下。

public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);default void resetBeanDefinition(String beanName) {//目前还不知道是干嘛用的,怎么用的}

简单示例

public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@Componentstatic class MyBeanPostProcessor5 implements MergedBeanDefinitionPostProcessor {@Override@Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之前");return bean;}@Override@Nullablepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("bean 初始化之后");return bean;}@Overridepublic void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {System.out.println("bean 合并");}@Overridepublic void resetBeanDefinition(String beanName) {System.out.println("重置 bean定义");}}
}

六、总结

spring中带Processor的类还有很多,但是本质上都和这几个差不多,都是用来处理XXX在XXX的时候XXX。大白话讲就是,在A创建/发生/实例化/初始化之前,你想干什么。有点类似AOP的思想,在中间横叉一道。详细的场景及使用例子,等整理好另开一篇文章讲解。

本文发布于:2024-01-29 19:15:15,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170652691717658.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23