Spring框架初探【3】

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教程课堂笔记,参考学习


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值