今天给大家介绍BeanPostProcessor接口用法,希望对大家能有所帮助!
BeanPostProcessor接口通常被称为Bean的后置处理器,它是Spring中定义的接口,可以在Spring容器的创建过程中(主要在Bean初始化前后进行工作)回调BeanPostProcessor中定义的两个方法。
postProcessBeforeInitialization:在每一个bean对象的初始化方法调用之前回调。
postProcessAfterInitialization:在每个bean对象的初始化方法调用之后被回调。
说明:以上两个方法的返回值都不能为null,否则在后续的初始化方法会报空指针或者通过getBean()方法无法获取Bean对象。主要原因是如果返回值为null的情况下以上两个方法从Spring容器获取bean实例,没有再次放入Spring容器中去,这样容器中就没有了。
package org.springframework.beans.factory.config;
import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;
public interface BeanPostProcessor {
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
可以Spring容器中完成bean实例化、配置以及其他初始化方法前后根据业务的场景增加相应的逻辑处理。典型的案例AOP的实现。
package com.spring.bean;
public class Person {
private String name;
private Integer age;
private String address;
public Person(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", address='" + address + '\'' +
'}';
}
}
package com.spring.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
* 实例化、依赖注入完毕,在调用显示的初始化之前完成一些自定义的初始化逻辑
* 方法返回值不能为null
* 返回null那么在后续初始化方法会报空指针异常或者通过getBean()方法获取不到bena实例
* 如果设置为null情况下,后置处理器从Spring IoC容器中取出bean实例对象没有再次放回IoC容器中
*/
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization执行了"+beanName);
return bean;
}
/**
* 实例化、依赖注入、初始化完成后时执行
* 方法返回值也不能为null
* 如果返回null那么在后续初始化方法将报空指针异常或者通过getBean()方法获取不到bena实例对象
* 如果设置为null情况下,后置处理器从Spring IoC容器中取出bean实例对象没有再次放回IoC容器中
*/
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization"+beanName);
return bean;
}
}
package com.spring.config;
import com.spring.bean.UserInfo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(value = "com.spring.bean",includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION
, classes = {Component.class, Controller.class}
),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE
, classes = {UserInfo.class}
),
})
public class TestBeanPostProcessorConfig {
}
package com.spring.test;
import com.spring.config.TestBeanPostProcessorConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestBeanPostProcessor {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestBeanPostProcessorConfig.class);
}
}
输出结果:
postProcessBeforeInitialization执行了testBeanPostProcessorConfig
postProcessAfterInitializationtestBeanPostProcessorConfig
UserInfo构造器执行了
postProcessBeforeInitialization执行了userInfo
PostConstruct 初始化方法执行
postProcessAfterInitializationuserInfo
Process finished with exit code 0
IT技术分享社区
个人博客网站:https://programmerblog.xyz