springboot之BeanPostProcessor功能及例子(一)

更新时间:2023-05-04 16:24:51 阅读: 评论:0

springboot之BeanPostProcessor功能及例⼦(⼀)
==>
==>
⼀、BeanPostProcessor
字⾯上的意思是bean的后置处理器,什么意思呢?其实就是说,在bean的⽣命周期中,可以对它⼲什么。再简单点就是,bean初始化之前⼲点什么,之后⼜⼲点什么。
public interface BeanPostProcessor {
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//bean初始化之前,要⼲点什么或者什么都不⼲,但是⼲不⼲都要把⼈家给返回回去,不能⼀进来就出不去了~
return bean;
}
@Nullable
default 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);
}
@Component
static class MyBeanPostProcessor1 implements BeanPostProcessor {
@Override
@Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("bean 初始化之前");
return bean;
}
@Override
@Nullable
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("bean 初始化之后");
return bean;
}
}
}
⼆、InstantiationAwareBeanPostProcessor
字⾯上的意思是实例化bean后置处理器,什么意思呢?就是说你可以在bean实例化前后做⼀些事情。注意,是先实例化才到初始化的!
public interface InstantiationAwareBeanPostProcessor extends B民事诉讼立案流程 eanPostProcessor {
//the bean object to expo instead of a default instance of the target bean, or  null to proceed with default instantiation
@Nullable
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
//如果什么都不做,返回null,代表⾛默认实例化的逻辑
//如果在这⾥做了什么,那么就返回你指定实例化逻辑产⽣的对象
return null;
}
//true if properties should be t on the bean,fal if property population should be skipped. Normal implementations should return true
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
//实例化完了之后,如果这⾥返回true,代表这个笔记本功率 bean的属性会被正常t进去,如果是fal,那么就会跳过,spring给的建议是⼀般都要返回true。
return true;
}
//the actual property values to apply to the given bean (can be the pasd-in PropertyValues instance), or null which proceeds with the existing properti es  but specifically continues with a call to postProcessPropertyValues
@Nullable
default 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
@Nullable
default 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);
}
@Component
static class MyBeanPostProcessor2 implements InstantiationAwareBeanPostProcessor {
@Override
@Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("bean 初始化之前");
return bean;
}
@Override
@Nullable
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("bean 初始化之后");
return bean;
}
@Override
@Nullable
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("bean 实例化之前");
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("bean 实例化之后");
return true;
}
@Override
@Nullable
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
throws BeansException {
System.out.println("bean 变量处理");
return null;
}
}
}
三、SmartInstantia历史资料 tionAwareBeanPostProcessor
字⾯意思就是智能实例化bean后置处理器,什么意思呢?其实就是⽐InstantiationAwareBeanPostProcessor多三个接⼝,在bean实例化之前,多做⼀些事情⽽已。
public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {
//return the type of the bean, or null if not predictable
@Nullable
default Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
//实例化之前先预测bean的类型??(我也不是很懂它的⽤途,下篇⽂章讲例⼦的时候再顺带解决)
return null;
}
//return the candidate constructors, or  null if none specified
@Nullable
default Constructor<?>[] determineCandidateConstructors(Class<?> beanClass,九九歌谣 String beanName)
throws BeansException {
//说⽩了就是可以决定⽤哪个构造器来实例化这个bean
return null;
}
//return the object to expo as bean reference typically with the pasd-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);
}
@Component
static class MyBeanPostProcessor3 implements SmartInstantiationAwareBeanPostProcessor {
@Override
@Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("bean 初始化之前");
return bean;
}
@Override
@Nullable
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("bean 初始化之后");
return bean;
}
@Override
@Nullable
public Object postProcessBeforeInstantia五年级上册手抄报 tion(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("bean 实例化之前");
return null;
}
@O绿豆面条 verride
public boolean postProcessAfterInstantiation(Object bean, String b做老师 eanName) throws BeansException {
System.out.println("bean 实例化之后");
return true;
}
@Override
@Nullable
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
throws BeansException {
System.out.println("bean 变量处理");
return null;
}
@Override
@Nullable
public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("预测 bean 类型");
return null;
}
@Override
@Nullable
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)
throws BeansException {
System.out.println("选择构造器");
return null;
}
@Override
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
System.out.println("提前暴露引⽤");
return bean;
}
}
@Component
static class B {
@Autowired
A a;
}
@Component
static class A {
@Autowired
B b;
}
}
四、DestructionAwareBeanPostProcessor
字⾯上的意思就是销毁bean后置处理器,什么意思呢?就是销毁bean之前,你还想⼲嘛。
public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {
void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
//return true} if postProcessBeforeDestruction is suppod to be called for this bean instance eventually, or fal if not needed  default boolean requiresDestruction(Object bean) {
//默认是true,也就是需要销毁之前做点什么,会调⽤postProcessBeforeDestruction
//如果是fal,那么就不会调⽤
return true;
}
简单⽰例

本文发布于:2023-05-04 16:24:51,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/525213.html

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

标签:实例   返回   时候   意思
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图