生命周期
所谓的生命周期就是bean从创建到初始化到销毁的一个过程,而spring中bean的生命周期都是交给容器进行管理的。因此我们可以自动以初始化和销毁方法,容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法,下面就演示下如何自定义初始化方法和销毁方法的几种方式。
- 通过@Bean是定destroyMethod和initMethod,这种方式相当于在之前的配置文件中编写init-method和destroy-method的方法,具体实例如下:
public class Car {
public Car() {
System.out.println("car construction...");
}
public void destory(){
System.out.println("car destory...");
}
public void init(){
System.out.println("car init...");
}
}
//通过配置类的形式将当前的bean注入到容器中,并且指定destroyMethod 和initMethod 。
@Configuration
public class MyConfig3 {
@Bean(destroyMethod = "destory",initMethod = "init")
public Car car(){
return new Car();
}
}
//测试方法
public class App {
public static void main(String[] args) {
//读取注解
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig3.class);
System.out.println("Spring 容器加载完成....");
((AnnotationConfigApplicationContext) applicationContext).close();
}
}
输出的结果为:
从以上输出内容可以看出,初始化方法是在对象创建完成并赋值好,才会调用初始化方法。而销毁方法是在容器关闭时。但是如果定义 @Scope("prototype")
,定义bean为多实例时,是在获取bean的时候才会调用初始化方法,但是不处理bean的销毁方式,输出结果如下:
2. 通过InitializingBean定义初始化逻辑, DisposableBean定义销毁逻辑
//定义一个对象实现 InitializingBean, DisposableBean并且重新里面的destroy和afterPropertiesSet方法
@Component
public class Tree implements InitializingBean, DisposableBean {
public Tree() {
System.out.println("Tree construction .....");
}
@Override
public void destroy() throws Exception {
System.out.println("Tree InitializingBean destroy.....");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Tree InitializingBean afterPropertiesSet.....");
}
}
//扫描对象所在的包
@ComponentScan("Spring.bean")
@Configuration
public class MyConfig3 {
@Bean(destroyMethod = "destory",initMethod = "init")
public Car car(){
return new Car();
}
}
//测试方法
public class App {
public static void main(String[] args) {
//读取注解
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig3.class);
System.out.println("Spring 容器加载完成....");
((AnnotationConfigApplicationContext) applicationContext).close();
}
}
这里可以看到输出的结果为:
3. 使用JSR250规范中的@PostConstruct定义初始化方法,该注解是在bean创建完成并且属性赋值完成,来执行这个初始化方法。@PreDestroy定义销毁前的方法。
//定义一个bean对象在其中的方法是添加@PostConstruct、@PreDestroy注解
@Component
public class Cat {
public Cat() {
System.out.println("Cat construction...");
}
@PreDestroy
public void destory(){
System.out.println("Cat destory...");
}
@PostConstruct
public void init(){
System.out.println("Cat init...");
}
}
//配置类
@ComponentScan("Spring.bean")
@Configuration
public class MyConfig3 {
@Bean(destroyMethod = "destory",initMethod = "init")
public Car car(){
return new Car();
}
}
//测试类
public class App {
public static void main(String[] args) {
//读取注解
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig3.class);
System.out.println("Spring 容器加载完成....");
((AnnotationConfigApplicationContext) applicationContext).close();
}
}
输出结果为:
4. BeanPostProcessor(bean的后置处理器)
postProcessBeforeInitialization:在初始化之前
postProcessAfterInitialization:在初始化方法调用之后
//需要先定义一个后置处理器 实现postProcessAfterInitialization、postProcessBeforeInitialization方法
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor postProcessAfterInitialization....."+bean.getClass());
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeanPostProcessor postProcessBeforeInitialization....."+bean.getClass());
return bean;
}
}
//配置类
@ComponentScan("Spring.bean")
@Configuration
public class MyConfig3 {
@Bean(destroyMethod = "destory",initMethod = "init")
public Car car(){
return new Car();
}
}
//测试方法
public static void main(String[] args) {
//读取注解
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig3.class);
System.out.println("Spring 容器加载完成....");
((AnnotationConfigApplicationContext) applicationContext).close();
}
输出结果如下: