一:基本流程
把一个bean纳入到Spring IoC容器之中,这个bean的生命周期就会交由容器进行管理
1.Bean的建立
由BeanFactory读取Bean定义文件,并生成各个实例。
2.Setter注入
执行Bean的属性依赖注入。
3.BeanNameAware的setBeanName()
如果Bean类实现了org.springframework.beans.factory.BeanNameAware接口,则执行其setBeanName()方法。
4.BeanFactoryAware的setBeanFactory()
如果Bean类实现了org.springframework.beans.factory.BeanFactoryAware接口,则执行其setBeanFactory()方法。
5.BeanPostProcessors的processBeforeInitialization()
容器中如果有实现org.springframework.beans.factory.BeanPostProcessors接口的实例,则任何Bean在初始化之前都会执行这个实例的processBeforeInitialization()方法。
6.InitializingBean的afterPropertiesSet()
如果Bean类实现了org.springframework.beans.factory.InitializingBean接口,则执行其afterPropertiesSet()方法。
7.Bean定义文件中定义init-method
在Bean定义文件中使用“init-method”属性设定方法名称,如下:
<bean id="demoBean" class="com.yangsq.bean.DemoBean" init-method="initMethod">
.......
</bean>
这时会执行initMethod()方法,注意,这个方法是不带参数的。
8.BeanPostProcessors的processAfterInitialization()
容器中如果有实现org.springframework.beans.factory.BeanPostProcessors接口的实例,则任何Bean在初始化之前都会执行这个实例的processAfterInitialization()方法。
9.DisposableBean的destroy()
在容器关闭时,如果Bean类实现了org.springframework.beans.factory.DisposableBean接口,则执行它的destroy()方法。
10.Bean定义文件中定义destroy-method
在容器关闭时,可以在Bean定义文件中使用“destory-method”定义的方法
二:使用案例
1.案例截图
2.基本代码
package com.cloud.beanlife;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
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;
private Integer age;
public PersonService(){
System.out.println("实例化对象");
}
public PersonService(String abc){
System.out.println("实例化对象");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("调用了setName函数");
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void sayhello(){
System.out.println("hello:"+name+"的年龄是"+age);
}
//该方法可以传递ApplicationContext
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("setApplicationContext "+arg0);
}
//可以通过注解的方式配置<bean init-method="init">初始化方法
@PostConstruct
public void init(){
System.out.println("自己的init方法");
}
//该方法可以传递beanFactory
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("setBeanFactory "+arg0);
}
//该方法的arg0表示正在被实例化的bean的id 是多少
@Override
public void setBeanName(String arg0) {
System.out.println("setBeanName被调用 值是"+arg0);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
//可以通过注解的方式配置<bean destroy-method="mydestory">销毁方法
@PreDestroy
public void mydestroy(){
System.out.println("生命周期结束,释放各种资源");
}
}
package com.cloud.beanlife;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessAfterInitialization 函数被调用");
System.out.println(arg0+"被创建的 时间是"+new java.util.Date());
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessBeforeInitialization 函数被调用");
return arg0;
}
}
3.配置代码
<?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" destroy-method="mydestroy" class="com.cloud.beanlife.PersonService">
<property name="name" value="Spring"/>
<property name="age">
<value>25</value>
</property>
</bean>
<bean id="personService2" class="com.cloud.beanlife.PersonService">
<property name="name" value="小明"/>
</bean>
<!-- 配置自己的处理器,类似于过滤器 -->
<bean id="myBeanPostProcessor" class="com.cloud.beanlife.MyBeanPostProcessor">
</bean>
</beans>
4.测试代码
package com.cloud.beanlife;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestLife {
public static void main(String[] args) {
//bena的生命周期
ApplicationContext ac=new ClassPathXmlApplicationContext("com/cloud/beanlife/beans.xml");
PersonService ps=(PersonService) ac.getBean("personService");
ps.sayhello();
}
}
5.测试结果
实例化对象
调用了setName函数
setBeanName被调用 值是personService
setBeanFactory org.s[email protected]9a082e2: defining beans [personService,personService2,myBeanPostProcessor]; root of factory hierarchy
setApplicationContext org[email protected]735cda3f: display name [org[email protected]735cda3f]; startup date [Thu Aug 27 16:43:00 CST 2015]; root of context hierarchy
postProcessBeforeInitialization 函数被调用
afterPropertiesSet
postProcessAfterInitialization 函数被调用
[email protected]被创建的 时间是Thu Aug 27 16:43:01 CST 2015
实例化对象
调用了setName函数
setBeanName被调用 值是personService2
setBeanFactory org.s[email protected]9a082e2: defining beans [personService,personService2,myBeanPostProcessor]; root of factory hierarchy
setApplicationContext org[email protected]735cda3f: display name [org[email protected]735cda3f]; startup date [Thu Aug 27 16:43:00 CST 2015]; root of context hierarchy
postProcessBeforeInitialization 函数被调用
afterPropertiesSet
postProcessAfterInitialization 函数被调用
[email protected]被创建的 时间是Thu Aug 27 16:43:01 CST 2015
hello:Spring的年龄是25
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21