1.BeanPostProcessor
2个方法:
postProcessBeforeInitialization
postProcessAfterInitialization
初始化执行顺序:
postProcessBeforeInitialization->@postConstruc->
afterPropertiesSet()->init-method->postProcessBeforeInitialization
代码示例:
public class User implements InitializingBean, DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println("destroy()");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet()");
}
@PostConstruct
public void postConst() {
System.out.println("postConst");
}
public void init() {
System.out.println("init()");
}
}
@Configuration
public class Config {
@Bean(initMethod = "init" ,destroyMethod = "destroy")
public User user() {
User user = new User();
return user;
}
}
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// 这边只做简单打印 原样返回bean
System.out.println("postProcessBeforeInitialization====" + beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// 这边只做简单打印 原样返回bean
System.out.println("postProcessAfterInitialization====" + beanName);
return bean;
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
context.close();
}
}
·
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public MyBeanFactoryPostProcessor() {
super();
System.out.println("这是BeanFactoryPostProcessor实现类构造器!!");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out
.println("BeanFactoryPostProcessor调用postProcessBeanFactory方法");
BeanDefinition beanDefinition= beanFactory.getBeanDefinition("person");
beanDefinition.getPropertyValues().addPropertyValue("phone",111);
}
}
public class MyBeanPostProcessor implements BeanPostProcessor{
public MyBeanPostProcessor() {
super();
System.out.println("这是BeanPostProcessor实现类构造器!!");
}
@Nullable
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!beanName: "+beanName);
return bean;
}
@Nullable
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!beanName: "+beanName);
return bean;
}
}
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
public MyInstantiationAwareBeanPostProcessor() {
super();
System.out.println("这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!!");
}
// 接口方法、实例化Bean之前调用
@Override
public Object postProcessBeforeInstantiation(Class beanClass,
String beanName) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法");
return null;
}
// 接口方法、实例化Bean之后调用
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法");
return bean;
}
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs,
PropertyDescriptor[] pds, Object bean, String beanName)
throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");
return pvs;
}
}
public class Person implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
public Person() {
System.out.println("【构造器】调用Person的构造器实例化");
}
private String name;
private String address;
private int phone;
BeanFactory beanFactory;
String beanName;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("【注入属性】注入属性name");
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
System.out.println("【注入属性】注入属性address");
this.address = address;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
System.out.println("【注入属性】注入属性phone");
this.phone = phone;
}
@Override
public String toString() {
return "Person [address=" + address + ", name=" + name + ", phone="
+ phone + "]";
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out
.println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
this.beanFactory=beanFactory;
}
@Override
public void setBeanName(String name) {
System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
this.beanName=name;
}
@Override
public void destroy() throws Exception {
System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out
.println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
}
// 通过<bean>的init-method属性指定的初始化方法
public void myInit() {
System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
}
// 通过<bean>的destroy-method属性指定的初始化方法
public void myDestory() {
System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
}
}
@Configuration
public class SpringConfig {
@Bean
Person person() {
Person person = new Person();
return person;
}
@Bean
MyBeanFactoryPostProcessor myBeanFactoryPostProcessor() {
MyBeanFactoryPostProcessor processor = new MyBeanFactoryPostProcessor();
return processor;
}
@Bean
MyBeanPostProcessor myBeanPostProcessor() {
MyBeanPostProcessor processor = new MyBeanPostProcessor();
return processor;
}
@Bean
MyInstantiationAwareBeanPostProcessor myInstantiationAwareBeanPostProcessor() {
MyInstantiationAwareBeanPostProcessor processor = new MyInstantiationAwareBeanPostProcessor();
return processor;
}
}
public class SpringTest {
public static void main(String[] args) {
ApplicationContext context= new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println("初始化容器开始....");
Object object=context.getBean("person");
System.out.println("初始化容器成功....");
Person person=(Person) object;
System.out.println(person);
System.out.println("现在关闭容器...");
((AnnotationConfigApplicationContext)context).registerShutdownHook();
}
}
参考博客
1.http://uule.iteye.com/blog/2094609
2.https://blog.csdn.net/u011734144/article/details/52505263