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 BeanPostProcessor {
//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,相当于将返回的变量值替换原来的变量赋值
ret冲锋衣怎么清洗
urn 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<?> b大姨妈不来的原因
eanClass, 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;
}
}
}
三、SmartInstantiationAwareBeanPostProcessor
字⾯意思就是智能实例化bean后置处理器,什么意思呢?其实就是⽐InstantiationAwareBeanPostProcessor多三个接⼝,在bean实例化之前,多做⼀些事情⽽已。
public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPo螃蟹的营养价值
stProcessor {
//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 implementshug过去式
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 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;
}
@Override
@Nullable
public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws Bean医保卡查询
sException {
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 suppo淘宝策划
d to be called for this bean instance eventually, or fal if not needed default boolean requiresDestruction(Object bean) {
//默认是true,也就是需要销毁之前做点什么,会调⽤postProcessBeforeDestruction
//如果是fal,那么就不会调⽤
return true;
}
简单⽰例