测试项目目录:
代码部分:
- App.java
package com.hsp.beanlife;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("bean在上下文中的生命周期");
ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/beanlife/beans.xml");
PersonService ps = (PersonService) ac.getBean("personService");
ps.sayHi();
System.out.println("################################");
System.out.println("以下3步因为容器已经关闭,所以无法显示,可以不用管");
System.out.println("11,容器关闭");
System.out.println("12,可以通过实现DisposableBean 接口来调用方法 destory");
System.out.println("13,可以在<bean destory-method=”fun1”/> 调用定制的销毁方法,在这里关闭打开的资源,如文件流,socket连接等");
}
}
- MyBeanPostProcessor.java
package com.hsp.beanlife;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* 自定义后置处理器
* @author Administrator
*
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("9,如果bean 和 一个后置处理器关联,则会自动去调用 postProcessAfterInitialization方法");
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("6,如果bean 和 一个后置处理器关联,则会自动去调用 postProcessBeforeInitialization方法");
return arg0;
}
}
- PersonService.java
package com.hsp.beanlife;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("2,调用类的set方法");
}
public void sayHi(){
System.out.println("10,可以使用bean了");
}
public PersonService() {
System.out.println("1,实例化类");
}
@Override
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
System.out.println("3,如果类实现了BeanNameAware接口,则会调用setBeanName方法,获取id号:"+arg0);
}
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("4,如果类实现了BeanFactoryAware接口,则会调用setBeanFactory方法,获取BeanFactory:"+arg0);
}
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("5,如果类实现了ApplicationContextAware接口,则会调用setApplicationContext方法,获取ApplicationContext:"+arg0);
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("7,如果你实现InitializingBean 接口,则会调用 afterPropertiesSet");
}
public void init(){
System.out.println("8,如果自己在<bean init-method=”init” /> 则可以在bean定义自己的初始化方法.");
}
}
- 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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="personService" class="com.hsp.beanlife.PersonService" init-method="init" > <property name="name"> <value>小小仕</value> </property> </bean> <bean id="myBeanPostProcessor" class="com.hsp.beanlife.MyBeanPostProcessor"> </bean> </beans>
运行后的输出内容:
1,实例化类 2,调用类的set方法 3,如果类实现了BeanNameAware接口,则会调用setBeanName方法,获取id号:personService 4,如果类实现了BeanFactoryAware接口,则会调用setBeanFactory方法,获取BeanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@38d3cc2f: defining beans [personService,myBeanPostProcessor]; root of factory hierarchy 5,如果类实现了ApplicationContextAware接口,则会调用setApplicationContext方法,获取ApplicationContext:org.springframework.context.support.ClassPathXmlApplicationContext@6f6827b5: display name [org.springframework.context.support.ClassPathXmlApplicationContext@6f6827b5]; startup date [Thu Dec 06 11:34:05 CST 2018]; root of context hierarchy 6,如果bean 和 一个后置处理器关联,则会自动去调用 postProcessBeforeInitialization方法 7,如果你实现InitializingBean 接口,则会调用 afterPropertiesSet 8,如果自己在<bean init-method=”init” /> 则可以在bean定义自己的初始化方法. 9,如果bean 和 一个后置处理器关联,则会自动去调用 postProcessAfterInitialization方法 10,可以使用bean了 ################################ 以下3步因为容器已经关闭,所以无法显示,可以不用管 11,容器关闭 12,可以通过实现DisposableBean 接口来调用方法 destory 13,可以在<bean destory-method=”fun1”/> 调用定制的销毁方法,在这里关闭打开的资源,如文件流,socket连接等
以上就是bean的整个生命周期流程。