目录
1、BeanPostProcessor 概念介绍
2、BeanPostProcessor接口方法
2.1 BeanPostProcessor源码内容
3、BeanPostProcessor 接口的作用
4、代码示例
4.1 新建Person.java 类文件
4.2、新建MyBeanPostProcessor.java
4.3、新建TestBeanPostProcessorConfig.java 配置类
4.4、新建测试类 TestBeanPostProcessor.java
今天给大家介绍BeanPostProcessor接口用法,希望对大家能有所帮助!
1、BeanPostProcessor 概念介绍
BeanPostProcessor接口通常被称为Bean的后置处理器,它是Spring中定义的接口,可以在Spring容器的创建过程中(主要在Bean初始化前后进行工作)回调BeanPostProcessor中定义的两个方法。
2、BeanPostProcessor接口方法
postProcessBeforeInitialization:在每一个bean对象的初始化方法调用之前回调。
postProcessAfterInitialization:在每个bean对象的初始化方法调用之后被回调。
说明:以上两个方法的返回值都不能为null,否则在后续的初始化方法会报空指针或者通过getBean()方法无法获取Bean对象。主要原因是如果返回值为null的情况下以上两个方法从Spring容器获取bean实例,没有再次放入Spring容器中去,这样容器中就没有了。
2.1 BeanPostProcessor源码内容
package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;public interface BeanPostProcessor {@Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}
}
3、BeanPostProcessor 接口的作用
可以Spring容器中完成bean实例化、配置以及其他初始化方法前后根据业务的场景增加相应的逻辑处理。典型的案例AOP的实现。
4、代码示例
4.1 新建Person.java 类文件
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;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age='" + age + '\'' +", address='" + address + '\'' +'}';}
}
4.2、新建MyBeanPostProcessor.java
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;}
}
4.3、新建TestBeanPostProcessorConfig.java 配置类
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 {}
4.4、新建测试类 TestBeanPostProcessor.java
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 初始化方法执行
postProcessAfterInitializationuserInfoProcess finished with exit code 0