从零开始学Spring-Bean的生命周期
生命周期:
1、实例化(当程序加载applicationContext.xml文件),把bean(前提是scope=singleton)实例化到内存。
2、调用该Bean的setXxx()方法,设置属性。
3、如果实现了bean名字关注接口(BeanNameAware),则可以通过setBeanName获取id号。
4、如果实现了bean工厂关注接口(BeanFactoryAware),则可以获取BeanFactory。
5、如果实现了ApplicationContextAware接口,则可以获得ApplicationContext。
6、如果bean和一个后置处理器关联,则会自动去调用postProcessBeforeInitialization()方法。
7、如果实现InitializingBean接口,则会调用afterPropertiesSet方法。
8、如果配置 则可以在bean自定义初始化方法。
9、如果bean和一个后置处理器关联,则会自动去调用postProcessAfterInitialization()方法。
10、使用bean。
11、容器关闭。
12、可以通过实现DisposableBean接口来调用destory()方法。
13、可以在 调用自定义的销毁方法。
##Spring 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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 在容器文件中配置bean(service/dao/domain/action/数据源) -->
<bean id="personService" init-method="init" destroy-method="myDestory" class="com.pc.beanlife.PersonService">
<!-- 这里注入属性,前提是有setName才能成功 -->
<property name="name" value="zs" />
</bean>
<!-- 配置后置处理器(类似filter) -->
<bean id="myBeanPostProcesseor" class="com.pc.beanlife.MyBeanPostProcesseor" />
</beans>
###bean 的类文件
public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{
private String name;
public PersonService() {
System.out.println("无参构造方法被调用");
}
public PersonService(String name) {
System.out.println("有参构造方法被调用");
}
public void printOk(){
System.out.println(name + " is Ok");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("调用set方法设置属性");
this.name = name;
}
// 该方法可以给name表示正被实例化的Bean的id
@Override
public void setBeanName(String name) {
// TODO Auto-generated method stub
System.out.println("setBeanName 被调用,值是" + name);
}
// 该方法可以传递beanFactory
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
System.out.println("setBeanFactory " + beanFactory);
}
// 该方法可以设置上下文
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("setApplicationContext " + applicationContext);
}
// 该方法可以在InitializingBean使用
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("afterPropertiesSet");
}
// 自定义的初始化方法
public void init() {
System.out.println("调用自定义的初始化方法");
}
// 可以在这关闭数据连接,socket,文件流,释放资源等等。。。
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("调用destroy()");
}
// 自定义我们的销毁方法
public void myDestory() {
System.out.println("调用自定义的销毁方法");
}
}
###后置处理器文件:
public class MyBeanPostProcesseor implements BeanPostProcessor {
// 后置处理之后的过滤
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization方法被调用");
return bean;
}
// 后置处理之前的过滤
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization方法被调用");
return bean;
}
}
测试类
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 通过ApplicationContext加载Bean
System.out.println("------------在ApplicationContext中Bean的生命周期------------");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/pc/beanlife/beans.xml");
// 获取Bean
PersonService personService = (PersonService) applicationContext.getBean("personService");
personService.printOk();
// 通过BeanFactory加载Bean
System.out.println("------------在BeanFactory中Bean的生命周期------------");
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("com/pc/beanlife/beans.xml"));
PersonService personService2 = (PersonService) beanFactory.getBean("personService");
personService2.printOk();
}
测试结果
------------在ApplicationContext中Bean的生命周期------------
无参构造方法被调用
调用set方法设置属性
setBeanName 被调用,值是personService
setBeanFactory org.s[email protected]1f8b81e3: defining beans [personService,myBeanPostProcesseor]; root of factory hierarchy
setApplicationContext org[email protected]771c8a71: display name [org[email protected]771c8a71]; startup date [Sat Feb 20 11:34:05 CST 2016]; root of context hierarchy
postProcessBeforeInitialization方法被调用
afterPropertiesSet
调用自定义的初始化方法
postProcessAfterInitialization方法被调用
zs is Ok
------------在BeanFactory中Bean的生命周期------------
无参构造方法被调用
调用set方法设置属性
setBeanName 被调用,值是personService
setBeanFactory [email protected]: defining beans [personService,myBeanPostProcesseor]; root of factory hierarchy
afterPropertiesSet
调用自定义的初始化方法