Bean的后置处理器
Spring有一个接口:后置处理器(BeanPostProcessor),它可以在bean的初始化前后调用方法。
-
创建简单java类
package com.atguigu.bean; public class Person { private String lastName; private Integer age; private String gender; private String email; private Car car; public void myInit(){ System.out.println("Person对象被初始化了"); } public void myDestory(){ System.out.println("Person对象被销毁了"); } public Person() { super(); System.out.println("Person对象被创建了"); } public Person(String lastName, Integer age, String gender, String email) { super(); this.lastName = lastName; this.age = age; this.gender = gender; this.email = email; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person [lastName=" + lastName + ", age=" + age + ", gender=" + gender + ", email=" + email + ", car=" + car + "]"; } }
-
创建BeanPostProcessor实现子类
package com.atguigu.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * (1)编写后置处理器的实现类 * (2)将后置处理器注册在配置文件中 * @author Keeper * */ public class MyBeanPostProcessor implements BeanPostProcessor{ /** * 在初始化方法之前调用 * @beanName 在xml中配置的id */ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName+"将要调用初始化方法了"+ "它的内容为"+bean); //返回bean return bean; } /** * 初始化方法之后调用 * @beanName 在xml中配置的id */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName+"bean初始化方法调用完了,AfterInitialization调用,它的内容是"+bean); return bean; } }
-
编写配置文件
<?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 id="person01" class="com.atguigu.bean.Person" destroy-method="myDestory" init-method="myInit"></bean> <!-- 定义后置处理器 --> <bean id="beanPostProcessor" class="com.atguigu.bean.MyBeanPostProcessor"></bean> </beans>
-
测试
package com.atguigu.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class IOCTest { private ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml"); /** * baen的生命周期: * (容器启动)构造器-->postProcessBeforeInitialization-->初始化-->postProcessAfterInitialization-->(容器关闭)销毁 */ @Test public void test(){ //关闭容器 ((ClassPathXmlApplicationContext)context).close(); /* * Person对象被创建了 person01将要调用初始化方法了它的内容为Person [lastName=null, age=null, gender=null, email=null, car=null] Person对象被初始化了 person01bean初始化方法调用完了,AfterInitialization调用,它的内容是Person [lastName=null, age=null, gender=null, email=null, car=null] Person对象被销毁了 */ } }
注意:就算没有定义init-method(初始化方法),后置处理器照样会运行。