(6)Spring框架----Bean生命周期分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27706119/article/details/88982365

实现方式

当我们需要在bean初始化和销毁时执行特定的动作时,Spring为我们提供了两个不同的方式来实现:

  • bean继承InitializingBeanDisposableBean接口。接口InitializingBeanafterPropertiesSet()会在属性设置方法调用完成后调用;接口DisposableBeandestroy()方法会在bean销毁时(超出作用域)执行;
  • 使用XML配置的方式:其中<bean>标签的init-method属性可制定该bean的初始化方法,destroy-method属性可指定bean的销毁方法。效果与集成接口方式一致。

Bean的生命周期的配置:

Bean对象的创建和销毁的两个属性配置

通过配置<bean>标签上的init-method作为Bean的初始化的时候执行的方法,配置destroy-method作为Bean的销毁的时候执行的方法。  

销毁方法想要执行,需要是单例创建的Bean而且在工厂关闭的时候,Bean才会被销毁.

*init-method: 当Bean被载入到容器时,调用init-method属性指定的方法

*destory-method: 当Bean从容器中删除时,调用destory-method属性指定的方法

为了简化学习成本并且减少代码与Spring框架的耦合,这里我们主要讲解演示XML配置方式的使用。

Bean生命周期分析

1.Spring对Bean进行实例化(相当于程序中的new Xx())

2.Spring将值和Bean的引用注入进Bean对应的属性中(注入属性)

扫描二维码关注公众号,回复: 5870415 查看本文章

3.如果Bean实现了BeanNameAware接口,Spring将Bean的ID传递给setBeanName()方法 
(实现BeanNameAware清主要是为了通过Bean的引用来获得Bean的ID,一般业务中是很少有用到Bean的ID的)

4.如果Bean实现了BeanFactoryAware接口,Spring将调用setBeanDactory(BeanFactory bf)方法并把BeanFactory容器实例作为参数传入。 
(实现BeanFactoryAware 主要目的是为了获取Spring容器,如Bean通过Spring容器发布事件等)

5.如果Bean实现了ApplicationContextAwaer接口,Spring容器将调用setApplicationContext()方法,把bean所在的应用上下文的引用传入. 
(作用与BeanFactory类似都是为了获取Spring容器,不同的是Spring容器在调用setApplicationContext方法时会把它自己作为setApplicationContext 的参数传入,而Spring容器在调用setBeanDactory前需要程序员自己指定(注入)setBeanDactory里的参数BeanFactory )

6.如果Bean实现了BeanPostProcess接口,Spring将调用它们的postProcessBeforeInitialization(预初始化)方法 
(作用是在Bean实例创建成功后对进行增强处理,如对Bean进行修改,增加某个功能)

7.如果Bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet方法,作用与在配置文件中对Bean使用init-method声明初始化的作用一样,都是在Bean的全部属性设置成功后执行的初始化方法。

8.如果Bean实现了BeanPostProcess接口,Spring将调用它们的postProcessAfterInitialization(后初始化)方法(作用与6的一样,只不过6是在Bean初始化前执行的,而这个是在Bean初始化后执行的,时机不同 )

9.此时,bean已经准备就绪,可以被程序使用了,Bean将一直驻留在应用上下文中给应用使用,直到应用上下文被销毁

10.如果Bean实现了DispostbleBean接口,Spring将调用它的destory方法,作用与在配置文件中对Bean使用destory-method属性的作用一样,都是在Bean实例销毁前执行的方法。

@Component 
public class UserEntity implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean,DisposableBean {

	private String userName;
	private Integer age = null;

	public UserEntity() {
		System.out.println("无惨构造函数.....");
	}

	public UserEntity(String userName, Integer age) {
		System.out.println("我是有参构造函数 userName:" + userName + ",age:" + age);
		this.userName = userName;
		this.age = age;
	}

	public String getUserName() {

		return userName;
	}

	public void setUserName(String userName) {

		this.userName = userName;
	}

	public Integer getAge() {

		return age;
	}

	public void setAge(Integer age) {

		this.age = age;
	}

	@Override
	public String toString() {
		return "UserEntity [userName=" + userName + ", age=" + age + "]";
	}

	public void setBeanName(String name) {
		System.out.println("BeanName:" + name);
	}

	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("setBeanFactory");
	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("setApplicationContext");
	}

	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessBeforeInitialization bean初始化之前" + beanName);
		return bean;

	}

	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessAfterInitialization bean初始化之后" + beanName);
		return bean;

	}
    public void init(){
    	System.out.println("init()");
    }
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet");
	}

	public void destroy() throws Exception {
		 System.out.println("destroy 销毁bean");
		    
	}

}

测试

	ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring003.xml");
		UserEntity user = (UserEntity) app.getBean("userEntity");
		app.destroy();

猜你喜欢

转载自blog.csdn.net/qq_27706119/article/details/88982365