spring bean的生命周期图解及案例分析

来源:互联网 发布:调度数据网络柜 编辑:程序博客网 时间:2024/06/07 04:44

因为自己最近在研究spring,所以以下是自己对spring bean生命周期的理解

1.spring bean生命周期图解如下:



2.上面是spring bean生命周期的图解,图中有一小部分解释


3.别的什么也不说,直接上案例:

为了演示spring bean的生命周期,首先需要让bean实现四个接口:

BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这4个接口


同时还需要在<bean>中的 init-method  和 destroy-method设置相应的方法


a.编写一个java bean,实现BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这4个接口:

package com.hy.test;


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.DisposableBean;
import org.springframework.beans.factory.InitializingBean;


public class Pet implements BeanFactoryAware, BeanNameAware, InitializingBean,
DisposableBean {


private String name;


private String master;


private String color;


private BeanFactory BeanFactory;


private String beanName;


public Pet(){
System.out.println("调用构造器实例化");
}

public String getName() {
return name;
}


public void setName(String name) {
System.out.println("注入name属性");
this.name = name;
}


public String getMaster() {
return master;
}


public void setMaster(String master) {
System.out.println("master属性");
this.master = master;
}


public String getColor() {
return color;
}


public void setColor(String color) {
System.out.println("注入color属性");
this.color = color;
}


// 这个是DisposableBean的方法
@Override
public void destroy() throws Exception {


System.out.println("通过DisposableBean调用destroy()方法");
}


// 这个是InitializingBean的方法
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub


}


// 这个是BeanNameAware的方法
@Override
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
this.beanName = arg0;
}


// 这个是BeanFactoryAware的方法
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
this.BeanFactory = arg0;
}

//<bean>中的init-method
public void myInit(){

System.out.println("调用<bean>中的init-method中指定的init方法");
}


//<bean>中的destroy-method方法
public void myDestroy(){

System.out.println("调用<bean>中的destroy-method中指定的destroy方法");
}

@Override
public String toString() {
// TODO Auto-generated method stub
return "[name]:"+name+"  [master]:"+master+"  [color]:"+color;
}
}


b.在来写一个实现BeanPostProcessor的类,这个类是后处理器,在实现aop等地方很有用:

package com.hy.test;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;


public class MyBeanPostProcesstor implements BeanPostProcessor{


public MyBeanPostProcesstor(){
super();
System.out.println("这是BeanPostProcessor构造器实现类");
}

@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("这是BeanPostProcessor的postProcessAfterInitialization对bean的更改");
return arg0;
}


@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("这是BeanPostProcessor的postProcessBeforeInitialization对bean的更改");
return arg0;
}


}


c.在写一个实现BeanFactoryPostProcessor接口的类,如下方法:

package com.hy.test;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;


public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor{

public MyBeanFactoryPostProcessor(){
super();
System.out.println("调用MyBeanFactoryPostProcessor的构造器");
}


@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("调用BeanFactoryPostProcessor得postProcessBeanFactory()的方法");

BeanDefinition bd = arg0.getBeanDefinition("pet");
bd.getPropertyValues().addPropertyValue("color", "red");

}


}


d.再写一个初始化类,继承InstantiationAwareBeanPostProcessorAdapter

package com.hy.test;


import java.beans.PropertyDescriptor;


import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;


public class MyInstantiationAwareBeanPostProcessor extends
InstantiationAwareBeanPostProcessorAdapter {


public MyInstantiationAwareBeanPostProcessor() {
super();
System.out.println("这个是InstantiationAwareBeanPostProcessorAdapter的构造器");
}


// 这个是实例化bean之后
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out
.println("InstantiationAwareBeanPostProcessorAdapter调用postProcessAfterInstantiation方法");
return super.postProcessAfterInitialization(bean, beanName);
}


// 这个是实例化bean之前
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass,
String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out
.println("InstantiationAwareBeanPostProcessorAdapter调用postProcessBeforeInstantiation方法");
return null;
}


// 这个是设置某个属性是调用
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs,
PropertyDescriptor[] pds, Object bean, String beanName)
throws BeansException {
System.out
.println("InstantiationAwareBeanPostProcessorAdapter调用postProcessPropertyValues方法");
return pvs;
}
}



e.基本的都写完了,接下来就是写配置文件了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" 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/context 
          http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop.xsd
          ">

<bean id="beanPostProcesstor" class="com.hy.test.MyBeanPostProcesstor"></bean>

<bean id="instantiationAwareBeanPostProcessor" class="com.hy.test.MyInstantiationAwareBeanPostProcessor"></bean>

<bean id="beanFactoryPostProcessor" class="com.hy.test.MyBeanFactoryPostProcessor"></bean>


<bean id="pet" class="com.hy.test.Pet" init-method="myInit" destroy-method="myDestroy">
<property name="name" value="泰迪"></property>
<property name="master" value="小鹏"></property>
<property name="color" value="棕色"></property>
</bean>
</beans>


f.最后写一个测试类测试类替换了原来在beans.xml中设置的color属性,由棕色变成红色了,请仔细观察:

package com.hy.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class ApplicationTest {


public static void main(String[] args) {

ApplicationContext ac = new ClassPathXmlApplicationContext("com/hy/test/beans.xml");
System.out.println("容器初始化成功");

Pet pet = ac.getBean("pet", Pet.class);

System.out.println(pet);

System.out.println("现在开始关闭容器");

((ClassPathXmlApplicationContext)ac).registerShutdownHook();
}

}


g.打印信息:

调用MyBeanFactoryPostProcessor的构造器
调用BeanFactoryPostProcessor得postProcessBeanFactory()的方法
这是BeanPostProcessor构造器实现类
这个是InstantiationAwareBeanPostProcessorAdapter的构造器
InstantiationAwareBeanPostProcessorAdapter调用postProcessBeforeInstantiation方法
调用构造器实例化
InstantiationAwareBeanPostProcessorAdapter调用postProcessPropertyValues方法
注入name属性
master属性
注入color属性
这是BeanPostProcessor的postProcessBeforeInitialization对bean的更改
调用<bean>中的init-method中指定的init方法
这是BeanPostProcessor的postProcessAfterInitialization对bean的更改
InstantiationAwareBeanPostProcessorAdapter调用postProcessAfterInstantiation方法
容器初始化成功
[name]:泰迪  [master]:小鹏  [color]:red
现在开始关闭容器
通过DisposableBean调用destroy()方法
调用<bean>中的destroy-method中指定的destroy方法

0 0
原创粉丝点击