Spring容器中Bean的生命周期(二)

一、通过@Bean注解指定初始化销毁方法

bean的生命周期是指bean的创建、初始化和销毁过程。IOC容器可以自定义初始化和销毁方法,容器中的bean进行到相应的生命周期时会调用自定义的初始化和销毁方法。通过@Bean注解的init-method和destroy-method属性,指定初始化和销毁方法。

对象创建时

  • 单实例对象:在容器启动的时候创建。
  • 多实例对象:在每次获取的时候创建。

对象初始化时

对象创建完成,并赋值成功之后,调用初始化方法

对象销毁时

  • 单实例对象:在容器关闭的时候销毁。
  • 多实例对象:容器负责管理和创建这个Bean,但容器关闭时不会调用销毁方法,容器不负责销毁。
1
2
3
4
5
6
7
8
9
@Configuration
public class MainConfigOfLifeCycle {

@Scope("prototype")
@Bean(initMethod="init",destroyMethod="destroy")
public Car car(){
return new Car();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Car {

public Car(){
System.out.println("car constructor...");
}

public void init(){
System.out.println("car....init....");
}

public void destroy(){
System.out.println("car....destroy...");
}
}
二、通过实现接口来指定初始化和销毁方法

在Spring容器中,通过让Bean实现InitializingBean来定义初始化逻辑,通过让Bean实现DisposableBean来定义销毁逻辑。

1
2
3
4
5
@ComponentScan("com.jt")
@Configuration
public class MainConfigOfLifeCycle {
//...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Component
public class Cat implements InitializingBean,DisposableBean{

public Cat(){
System.out.println("Cat....Constructor...");
}

public void destroy() throws Exception {
System.out.println("Cat....destroy...");

}

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

}
}
三、使用JSR250

JSR是Java Specification Requests的缩写,意思是Java 规范提案。是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。任何人都可以提交JSR,以向Java平台增添新的API和服务。JSR已成为Java界的一个重要标准。

  • @PostConstruct:在bean创建完成并且属性值赋值完成,来执行初始化方法。
  • @PreDestroy:在容器销毁bean之前进行销毁工作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Component
public class Dog {

public Dog(){
System.out.println("Dog...Constructor.....");
}

@PostConstruct
public void init(){
System.out.println("Dog...@PostConstruct.....");
}

@PreDestroy
public void destroy(){
System.out.println("Dog...@PreDestroy.....");
}
}
四、通过BeanPostProcessor控制生命周期

BeanPostProcessor接口是bean的后置处理器,在bean初始化前后进行一些处理工作。该接口里面有两个方法。

  • postProcessBeforeInitialization:在bean初始化之前调用。
  • postProcessAfterInitialization:在bean初始化之后调用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* 后置处理器,bean初始化前后调用
* 需将该处理器加入到容器中
* */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor{

/**
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one
* */
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
return bean;
}

/**
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* */
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
return bean;
}
}

BeanPostProcessor原理:

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
*遍历得到容器中所有的BeanPostProcessor;挨个执*行beforeInitialization,
* 一但返回null,跳出for循环,不会执行后面的BeanPostProcessor.postProcessorsBeforeInitialization
*
* BeanPostProcessor原理
* populateBean(beanName, mbd, instanceWrapper);给bean进行属性赋值
* initializeBean
* {
* applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
* invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化
* applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
*}
*/
-------------本文结束感谢您的阅读-------------