spring 容器扩展之BeanPostProcessor
spring的BeanPostProcessor接口给开发者提供了一个容器扩展的入口,
在Spring容器完成Bean实例化和属性设置后,并且在bean调用初始化方法之前或之后。因此BeanPostProcessor(Bean后置处理器)常用在:对bean内部的值进行修改;实现Bean的动态代理等。
可以定义一个或者多个BeanPostProcessor接口的实现,然后注册到容器中。那么该容器里管控的所有Bean在调用初始化方法之前或之后,都会调用BeanPostProcessor接口中对应的方法。
看代码
package com.spring.model;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Person implements DisposableBean, InitializingBean {
private String name;
Person() {
System.out.println("Constructor of person bean is invoked!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//Bean initialization code
public void afterPropertiesSet() throws Exception {
System.out.println("Initializing method of person bean is invoked!");
}
//Bean destruction code
public void destroy() throws Exception {
System.out.println("Destroy method of person bean is invoked!");
}
public void customDestroy() throws Exception {
System.out.println("Custom destroy method of person bean is invoked!");
}
public void customInit() throws Exception {
System.out.println("Custom initializing method of person bean is invoked!");
}
}
package com.spring.model;
public class Student {
String id;
String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.spring.main;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.model.Person;
import com.spring.model.Student;
public class Demoapp {
public static void main(String[] args) {
// Reading configuration from the spring configuration file.
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Person myperson = context.getBean("personBean", Person.class);
Student student = context.getBean("student", Student.class);
System.out.println("student id= " + student.getId());
System.out.println("Name= " + myperson.getName());
// Closing the context object.
context.close();
}
}
package com.spring.processer;
import org.springframework.beans.factory.config.BeanPostProcessor;
import com.spring.model.Student;
public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor{
// simply return the instantiated bean as-is
public Object postProcessBeforeInitialization(Object bean, String beanName) {
System.out.println("Bean '" + beanName + "' before created : " + bean.toString());
if(Student.class.isInstance(bean)){
Student student = (Student) bean;
student.setId("111");
}
return bean; // we could potentially return any object reference here...
}
public Object postProcessAfterInitialization(Object bean, String beanName) {
System.out.println("Bean '" + beanName + "' created : " + bean.toString());
return bean;
}
}
xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean name="personBean" class="com.spring.model.Person"> <property
name="name" value="Jason Clarke" /> </bean> -->
<bean name="personBean" class="com.spring.model.Person"
init-method="customInit" destroy-method="customDestroy">
<property name="name" value="Jason Clarke" />
</bean>
<bean name="student" class="com.spring.model.Student" />
<!-- when the above bean (messenger) is instantiated, this custom BeanPostProcessor
implementation will output the fact to the system console -->
<bean class="com.spring.processer.InstantiationTracingBeanPostProcessor" />
</beans>
spring中的RequiredAnnotationBeanPostProcessor就是实现了BeanPostProcessor的这么一个类,它的作用是用来保证java bean中使用了注解标签的属性的都已经被注入依赖。