Bean的生命周期方法
src\dayday\Car.java
package dayday;
import com.sun.org.apache.xpath.internal.SourceTree;
import javax.sound.midi.Soundbank;
/**
* Created by I am master on 2016/11/28.
*/
public class Car {
private String name;
public void setName(String name){
this.name=name;
System.out.println("setName...");
}
public String getName(){
return name;
}
public String toString(){
return "["+"carname="+name+"]";
}
public Car(){
System.out.println("Car's constructor...");
}
public void init(){
System.out.println("init...");
}
public void destroy(){
System.out.println("destroy...");
}
}
src\dayday\Main.java
package dayday;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.net.SocketTimeoutException;
import java.sql.SQLException;
/**
* Created by I am master on 2016/11/28.
*/
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
Car car=ctx.getBean("car",Car.class);
System.out.println(car);
//用close()方法是就会调用bean对应的destroy()方法
ctx.close();
}
}
src\beans.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="car" class="dayday.Car" init-method="init" destroy-method="destroy">
<property name="name" value="Audi"/>
</bean>
</beans>
运行结果
Car's constructor...
setName...
init...
[carname=Audi]
destroy...
bean的生命周期进行管理的过程分为五步
1)通过构造方法或工厂方法创建bean实例
2)为bean的属性设置值和对其他bean的引用
3)调用bean的初始化方法
4)使用bean
5)当spring容器关闭时,调用bean的销毁方法
bean后置处理器
在调用初始化工作前后对bean进行额外的操作
需要实现BeanPostProcessor接口
postProcessBeforeInitialization(Object bean, String beanName)在调用init()方法之前调用
postProcessAfterInitialization(Object bean, String beanName)在调用init()方法之后调用
src\dayday\MyBeanPostProcessor.java
package dayday;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* Created by I am master on 2016/11/29.
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization "+bean+" "+beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization "+bean+" "+beanName);
return bean;
}
}
Main/Car.java代码不发生变化
src\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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="car" class="dayday.Car" init-method="init" destroy-method="destroy">
<property name="name" value="Audi"/>
</bean>
<!--配置bean的后置处理器-->
<bean class="dayday.MyBeanPostProcessor"></bean>
</beans>
运行结果:
Car's constructor... setName... postProcessBeforeInitialization [carname=Audi] car init... postProcessAfterInitialization [carname=Audi] car
[carname=Audi] destroy...
//调用close()方式是就会调用bean中的destroy()方法