您的位置:首页 > 编程语言 > Java开发

Spring中bean的生命周期

2015-11-01 21:19 585 查看


根据上面的图片流程进行代码验证了一下:

applicationContext.xml(我取的另一个名字: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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="personServer" init-method="myInit" destroy-method="myDestroy" scope="prototype" class="com.zsy.person.PersonServer">
<property name="name" value="大锤" />
<property name="age" value="22"></property>
</bean>

<!--注入前置和后置处类-->
<bean id="myBeanPostProcessor" class="com.zsy.person.MyBeanPostProcessor"/>

</beans>

Server类:

public class PersonServer implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean
{
private String name;

private int age;

//业务逻辑方法
public void output()
{
System.out.println(name + "=================" + age);
}

//无参构造函数
public PersonServer()
{
System.out.println("PersonServer初始化");
}

//有参构造函数
public PersonServer(int age, String name)
{
this.name = name;
this.age = age;

}

public String getName()
{
return name;
}

//打印出set值
public void setName(String name)
{
System.out.println("name开始赋值:" + name);
this.name = name;
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

//获取bean的id
@Override
public void setBeanName(String arg0)
{

System.out.println("bean 的 id 开始赋值:" + arg0);
}

//获取BeanFactory的对象
@Override
public void setBeanFactory(BeanFactory arg0)
throws BeansException
{
System.out.println("beanFactory 的id 开始赋值:" + arg0);

}

//获取容器对象
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException
{

System.err.println("applicationContext :" + applicationContext);
}

//在执行postProcessAfterInitialization的方法之前调用
@Override
public void afterPropertiesSet()
throws Exception
{

System.out.println("InitializingBean=======================================");

}

//在postProcessAfterInitialization和postProcessBeforeInitialization之间调用
public void myInit()
{
System.out.println("myInit()的方法");
}

//自定义销毁方法
public void myDestroy()
{
System.out.println("销毁方法");
}

}

前置处理和后置处理类:

public class MyBeanPostProcessor implements BeanPostProcessor
{

@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException
{
System.out.println("postProcessAfterInitialization:after");
return arg0;
}

@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException
{
System.out.println("postProcessBeforeInitialization:before");
//System.out.println(arg0 + "时间:" + new Date() + " 类名:" + arg0.getClass().getName());
// TODO Auto-generated method stub
return arg0;
}

}

//Main方法
public class TestApp
{

/**
* main:(这里用一句话描述这个方法的作用). <br/>
* @author syzhao
* @param args
* @since JDK 1.6
*/
public static void main(String[] args)
{
//BeanFactory context = new XmlBeanFactory(new ClassPathResource("com/zsy/person/beans.xml"));
ApplicationContext context = new ClassPathXmlApplicationContext("com/zsy/person/beans.xml");
PersonServer person = (PersonServer)context.getBean("personServer");
person.output();
}

}

//打印出的结果
Person初始化
name开始赋值:大锤
bean 的 id 开始赋值:personServer
beanFactory 的id 开始赋值:org.springframework.beans.factory.support.DefaultListableBeanFactory@b307f0: defining beans [personServer,myBeanPostProcessor]; root of factory hierarchy
applicationContext :org.springframework.context.support.ClassPathXmlApplicationContext@ece65: startup date [Sun Nov 01 20:58:56 CST 2015]; root of context hierarchy
postProcessBeforeInitialization:before
InitializingBean=======================================
myInit()的方法
postProcessAfterInitialization:after
大锤=================22
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: