Spring5 - Bean的初始化和销毁的4种方式

简介: Spring5 - Bean的初始化和销毁的4种方式

20201011230207293.png



概述


针对单实例bean的话,容器启动的时候,bean的对象就创建了,而且容器销毁的时候,也会调用Bean的销毁方法

针对原型bean的话,容器启动的时候,bean是不会被创建的而是在获取bean的时候被创建,而且bean的销毁不受 IOC容器的管理.


方式一: 自行指定bean的初始化方法和bean的销毁方法

20201011230427989.png

【beans】

package com.artisan.base.lifeCycle;
public class A1 {
  public void init(){
    System.out.println("A1 init");
  }
  public void destory(){
    System.out.println("A1 destory");
  }
}
package com.artisan.base.lifeCycle;
public class A2 {
  public void init(){
    System.out.println("A2 init");
  }
  public void destory(){
    System.out.println("A2 destory");
  }
}

【配置类】

package com.artisan.base.lifeCycle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class LCConfig {
  @Bean(initMethod = "init",destroyMethod = "destory")
  public A1 a1(){
    return  new A1();
  }
  @Scope("prototype")
  @Bean
  public A2 a2(){
    return  new A2();
  }
}


【测试】

package com.artisan.base.lifeCycle;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.concurrent.TimeUnit;
/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2020/10/11 22:58
 * @mark: show me the code , change the world
 */
public class LCTest {
  public static void main(String[] args) throws InterruptedException {
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(LCConfig.class);
    TimeUnit.SECONDS.sleep(3);
    // 容器销毁
    ac.close();
  }
}


20201011230531699.png

方式二: 通过 InitializingBean和DisposableBean 接口实现bean的初始化以及销毁方法

package com.artisan.base.lifeCycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2020/10/11 23:06
 * @mark: show me the code , change the world
 */
public class A3 implements InitializingBean, DisposableBean {
  public A3() {
    System.out.println("A3 init");
  }
  @Override
  public void destroy() throws Exception {
    System.out.println("A3 destroy");
  }
  @Override
  public void afterPropertiesSet() throws Exception {
    System.out.println("A3 afterPropertiesSet");
  }
}

20201011230820889.png


执行上面的测试代码

20201011231031608.png

方式三: 通过JSR250规范 提供的注解@PostConstruct 和@ProDestory标注的方法

package com.artisan.base.lifeCycle;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2020/10/11 23:12
 * @mark: show me the code , change the world
 */
public class A4 {
  public A4() {
    System.out.println("A4 Construct ");
  }
  @PostConstruct
  public void init(){
    System.out.println("A4 init");
  }
  @PreDestroy
  public void destory(){
    System.out.println("A4 destory");
  }
}

2020101123145081.png

测试结果

20201011231507937.png


方式四:通过Spring的BeanPostProcessor的 bean的后置处理器会拦截所有bean创建过程

public class A5 {
  public A5() {
    System.out.println("A5 Construct");
  }
  public void init(){
    System.out.println("A5 init");
  }
  public void destory(){
    System.out.println("A5 destory");
  }
}


【bean後置處理器】

package com.artisan.base.lifeCycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class A5BeanPostProcessor implements BeanPostProcessor {
  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof  A5){
      System.out.println("A5BeanPostProcessor...postProcessBeforeInitialization:"+beanName);
    }
    return bean;
  }
  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof  A5){
      System.out.println("A5BeanPostProcessor...postProcessAfterInitialization:"+beanName);
    }
    return bean;
  }
}


【config】


20201011233147978.png


【test】


20201011233230343.png

简单说一下 BeanPostProcessor的执行时机


20201011233619830.png


看看invokeInitMethods


20201011233820971.png


相关文章
|
4天前
|
Java uml Spring
手写spring第四章-完善bean实例化,自动填充成员属性
手写spring第四章-完善bean实例化,自动填充成员属性
13 0
|
21天前
|
Java 应用服务中间件 Spring
Spring系列文章:Bean的作⽤域
Spring系列文章:Bean的作⽤域
|
21天前
|
Java Spring 容器
Spring系列文章:Bean的获取⽅式
Spring系列文章:Bean的获取⽅式
|
3天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
17 2
|
4天前
|
XML Java 数据格式
手写spring第七章-完成便捷实现bean对象初始化和销毁方法
手写spring第七章-完成便捷实现bean对象初始化和销毁方法
6 0
|
4天前
|
XML Java 数据格式
手写spring第六章-实现应用上下文,完成bean的扩展机制
手写spring第六章-实现应用上下文,完成bean的扩展机制
10 0
|
4天前
|
设计模式 搜索推荐 Java
手写spring第三章-重构,使用依赖关系完善实例化bean操作
手写spring第三章-重构,使用依赖关系完善实例化bean操作
11 0
|
5天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
13天前
|
Java 数据库连接 开发者
浅谈Spring的Bean生命周期
浅谈Spring的Bean生命周期
20 1
|
18天前
|
XML Java 数据格式
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
21 0
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道