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

Spring框架初探【3】

2016-05-15 22:01 274 查看
Spring中bean的生命周期

一个PersonService类

package com.xlc.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.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class PersonService implements BeanNameAware,BeanFactoryAware,
ApplicationContextAware,InitializingBean,
DisposableBean{
public String name;

public String getName() {
return name;
}

public PersonService() {
System.out.println("PersonService.PersonService()调用");
}

public void setName(String name) {
System.out.println("PersonService.setName()");
this.name = name;
}
public void sayHi(){
System.out.println("Hi"+name);
}

@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("PersonService.setApplicationContext()");
}

@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("PersonService.setBeanFactory()");
}

@Override
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
System.out.println("PersonService.setBeanName()");
}

@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("PersonService.afterPropertiesSet()");
}
public void init(){
System.out.println("PersonService.init()");
}

@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("PersonService.destroy()");
}
public void Mydestroy(){
System.out.println("PersonService.Mydestroy()");
}
}
Spring  bean配置文件
<bean id="PersonService" init-method="init" destroy-method="Mydestroy" class="com.xlc.beanlife.PersonService">
<property name="name" >
<value>小喵</value>
</property>
</bean>
<!-- 配置我们自己的后置处理器,有点类似我们的Filter -->
<bean id="myBeanPostProcessor.java" class="com.xlc.beanlife.MyBeanPostProcessor"></bean>
</beans>


一个MyBeanPostProcessor 实现 BeanPostProcessor接口

<span style="font-family: Arial, Helvetica, sans-serif;">package com.xlc.beanlife;</span>
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 {
// TODO Auto-generated method stub
System.out
.println("MyBeanPostProcessor.postProcessAfterInitialization()");
return arg0;
}

@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
// TODO Auto-generated method stub
System.out
.println("MyBeanPostProcessor.postProcessBeforeInitialization()");
return arg0;
}

}


package com.xlc.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
ApplicationContext ac =new ClassPathXmlApplicationContext("com/xlc/beanlife/beans.xml");
PersonService ps=(PersonService) ac.getBean("PersonService");
ps.sayHi();
}

}
运行结果,说明生命周期执行顺序





①    实例化(当我们的程序加载beans.xml文件),把我们的bean(前提是scope=singleton)实例化到内存

②    调用set方法设置属性

③    如果你实现了bean名字关注接口(BeanNameAware)则,可以通过setBeanName获取id号

④    如果你实现了 bean工厂关注接口,(BeanFactoryAware),则可以获取BeanFactory

⑤    如果你实现了 ApplicationContextAware接口,则调用方法

//该方法传递ApplicationContext

       publicvoid setApplicationContext(ApplicationContext arg0)

                     throwsBeansException {

              //TODO Auto-generated method stub

              System.out.println("setApplicationContext"+arg0);

             

       }

⑥    如果bean 和 一个后置处理器关联,则会自动去调用
Object postProcessBeforeInitialization方法

⑦    如果你实现InitializingBean 接口,则会调用 afterPropertiesSet

⑧    如果自己在<bean init-method=”init” /> 则可以在bean定义自己的初始化方法.

⑨    如果bean 和 一个后置处理器关联,则会自动去调用
Object postProcessAfterInitialization方法

⑩    使用我们的bean

11. 容器关闭

12. 可以通过实现DisposableBean 接口来调用方法 destory

13. 可以在<bean destory-method=”fun1”/> 调用定制的销毁方法

 常见的顺序1->2->6->10->9->11 

Bean在工厂中创建,生命周期会简单些



注:非博主总结,以上总结(除代码部分)为韩老师spring教程课堂笔记,参考学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring bean