Spring IOC 容器可以管理 Bean 的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务。举一个简单的例子
Bean 的生命周期
1)你需要创建Person.java和Main.java
首先是是Person.java,注意 init()与destory()
package com.spring.day03;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("setName...");
this.name = name;
}
public Person() {
System.out.println("Car is construction....");
}
public void init() {
System.out.println("init...开始");
}
public void destory() {
System.out.println("destory...结束");
}
}
2)创建application.xml 设置init-method与destroy-method
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="person" class="com.spring.day03.Person" init-method="init" destroy-method="destory">
<property name="name" value="haha"></property>
</bean>
</beans>
3)创建Main.java
package com.spring.day03;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//创建spring的IOC容器对象
//application代表IOC容器
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("application.xml");//读取xml文件
//从IOC容器里获取bean实例
Person person=(Person) ctx.getBean("person");//这里person对应xml文件bean的id
System.out.println(person.getName());
ctx.close();
}
}
4)查看结果
Car is construction....
setName...
postProcessBeforeInitialization:com.spring.day03.Person@56ac3a89,person
init...开始
postProcessAfterInitialization:com.spring.day03.Person@56ac3a89,person
haha
destory...结束
在结果中可以发现Spring IOC的生命周期是,是先构造器->设置属性->调用bean的初始化方法->使用bean->在关闭容器的时候调用销毁方法。
bean的后置处理器
Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理,bean 后置处理器对 IOC 容器里的所有 Bean 实例逐一处理。
1)在上面的基础下,首先创建一个Post.java文件
package com.spring.day03;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class Post implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization:"+bean+","+beanName);
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization:"+bean+","+beanName);
return bean;
}
}
2)在application.xml文件下创建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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="person" class="com.spring.day03.Person" init-method="init" destroy-method="destory">
<property name="name" value="haha"></property>
</bean>
<!-- 创建bean配置后置处理-->
<bean class="com.spring.day03.Post"></bean>
</beans>
3)查看结果
Car is construction....
setName...
postProcessBeforeInitialization:com.spring.day03.Person@56ac3a89,person
init...开始
postProcessAfterInitialization:com.spring.day03.Person@56ac3a89,person
com.spring.day03.Person@56ac3a89
destory...结束
后置处理器有什么用呢?举个例子就明白了。修改Post.java中的postProcessBeforeInitialization(。。)
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization:"+bean+","+beanName);
Person person =new Person();
person.setName("哈哈");//修改person的名字
return person;
}
运行后会发现,print显示是哈哈,因为后置处理器是对所以bean都处理的所以要在函数里进行筛选。
postProcessBeforeInitialization:com.spring.day03.Person@56ac3a89,person
Car is construction....
setName...
init...开始
postProcessAfterInitialization:com.spring.day03.Person@27c20538,person
哈哈
destory...结束