总结Bean的三种自定义初始化和销毁方法

作者:zijikanwa 时间:2023-01-05 13:38:09 

Bean三种自定义初始化和销毁

一. 三种方法概述

在配置类中指定 @Bean(initMethod = “init”,destroyMethod = “destory”)注解

实现InitializingBean接口并重写其afterPropertiesSet方法,实现DisposableBean接口并重写destroy方法

利用java的JSR250规范中的@PostConstruct标注在init方法上,@PreDestroy标注在destroy方法上

二. 方法详述

1. 方法1:配置类中指定

示例代码

public class CarA {
   public CarA() {
       System.out.println("CarA。。。构造函数");
   }
   public void initCarA(){
       System.out.println("CarA的init()方法");
   }
   public void destroyCarA(){
       System.out.println("CarA的destroy()方法");
   }
}
@Configuration
public class ConfigTest {
   @Bean(initMethod = "initCarA",destroyMethod = "destroyCarA")
   public CarA carA(){
       return new CarA();
   }
}

执行结果

CarA。。。构造函数
CarA的init()方法

服务启动

CarA的destroy()方法

2. 方法2:实现接口并重写方法

2.1 示例代码

public class CarB implements InitializingBean, DisposableBean {
   public CarB() {
       System.out.println("CarB。。。构造函数");
   }
   @Override
   public void afterPropertiesSet() throws Exception {
       System.out.println("CarB。。。afterPropertiesSet()方法执行");
   }
   @Override
   public void destroy() throws Exception {
       System.out.println("CarB。。。destroy()方法执行");
   }
}
@Configuration
public class ConfigTest {
   @Bean
   public CarB carB(){
       return new CarB();
   }
}

执行结果

CarB。。。构造函数
CarB。。。afterPropertiesSet()方法执行

服务启动

CarB。。。destroy()方法执行

2.2 概述

Spring 开放了扩展接口,允许我们自定义 bean 的初始化和销毁方法。即当 Spring 容器在 bean 进行到相应的生命周期阶段时,会自动调用我们自定义的初始化和销毁方法。这两个扩展接口是 InitializingBean 和 DisposableBean 。

InitializingBean 接口说明:该接口为 bean 提供了 bean 属性初始化后的处理方法,它只有 afterPropertiesSet 一个方法,凡是实现此接口的类,在 bean 的属性初始化后都会执行该方法。
package org.springframework.beans.factory;

public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}

DisposableBean 接口说明:该接口为单例 bean 提供了在容器销毁 bean 时的处理方法,它只有 destroy 一个方法,凡是实现此接口的类,在 bean 被销毁时都会执行该方法。

package org.springframework.beans.factory;
public interface DisposableBean {
    void destroy() throws Exception;
}

2.3 方法1 && 方法2

  • 相同点:都是在 bean 属性初始化之后需要执行的初始化方法。

  • 不同点

方法1:代码不与Spring耦合;执行效率较低(通过反射来执行initMethod 方法)

方法2:代码与Spring紧耦合;速度更快(将 bean 强制转换成 InitializingBean 接口类型,然后直接调用 afterPropertiesSet 方法)

  • 说明:afterPropertiesSet 和 initMethod 可以同时存在,但是 afterPropertiesSet 方法是在 initMethod 方法之前执行的。

  • 一个 bean 从创建到初始化的过程总结

通过构造器创建 bean

属性注入

执行 afterPropertiesSet 方法

执行 initMethod 方法

3. 方法3:利用java的JSR250规范

代码示例

public class CarC {
   public CarC() {
       System.out.println("CarC。。。构造函数");
   }
   @PostConstruct
   public void initCarC(){
       System.out.println("CarC。。。初始化方法initCarC()");
   }
   @PreDestroy
   public void destroyCarC(){
       System.out.println("CarC。。。销毁方法destroyCarC");
   }
}
@Configuration
public class ConfigTest {
   @Bean
   public CarC carC(){
       return new CarC();
   }
}

执行结果

CarC。。。构造函数
CarC。。。初始化方法initCarC()

服务启动

CarC。。。销毁方法destroyCarC

spring初始化后获取自定义注解Bean

目的是通过注解将特定类的信息(如接口编号)与类关联,之后可通过接口编号获取对应bean来执行对应逻辑。

一.新建注解类

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Service
public @interface ServiceCode {
    String code() default ""; 
    String className() default "";
}

包含接口编号和beanName信息。

二.新建接口类

@ServiceCode(code = "100010", className = "echoService")
@Service("echoService")
public class EchoService { 
}

三.实现接口ApplicationListener

来监听spring容器初始化完成后执行:

@Component
@Order(1)
public class ServiceInitListener implements ApplicationListener<ContextRefreshedEvent> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInitListener.class); 
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext applicationContext = event.getApplicationContext();
        //注意 需要时根容器才能通过注解获取到bean,比如event直接获取的容器中只有一些公共注册bean
        if (applicationContext.getParent() != null) {
            applicationContext = applicationContext.getParent();
        }
        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(ServiceCode.class);
        for (Object bean : beansWithAnnotation.values()) {
            ServiceCode annotation = bean.getClass().getAnnotation(ServiceCode.class);
            String code = annotation.code();
            String className = annotation.className();
            //注册接口编号和beanName
            //在统一入口可通过code获取beanName,然后通过springContext获取对应bean执行自定义逻辑
            //或者完成其他逻辑
        }
    } 
}

注意:

 ContextRefreshedEvent获取到的上下文环境不是根spring容器,其中只有部分spring内置bean,无法通过注解获取到自定义bean,需要获取其父容器来完成操作。我第一次获取是beanList总为空,后来发现其容器内部bean没有自定义的service bean,获取父容器后操作一切正常。

通过@Order注解来定制执行顺序,越小越优先执行。

来源:https://blog.csdn.net/zijikanwa/article/details/120768948

标签:Bean,自定义,初始化,销毁
0
投稿

猜你喜欢

  • 详解spring boot 使用application.properties 进行外部配置

    2023-02-13 02:09:11
  • 详解如何使用Java编写图形化的窗口

    2022-04-18 03:25:52
  • javaweb实现app扫码登录功能

    2022-03-25 03:48:19
  • Java使用Preference类保存上一次记录的方法

    2023-05-08 01:49:14
  • 浅谈Android ANR的信息收集过程

    2023-12-02 00:54:00
  • Java中弱引用和软引用的区别以及虚引用和强引用介绍

    2023-01-23 18:55:33
  • Android中使用itemdecoration实现时间线效果

    2021-10-08 12:15:49
  • 深入多线程之:内存栅栏与volatile关键字的使用分析

    2021-05-29 10:02:56
  • java 8如何自定义收集器(collector)详解

    2022-02-12 07:22:17
  • 详解Java中Callable和Future的区别

    2023-07-25 21:18:58
  • Android app应用多语言切换功能实现

    2023-12-27 22:06:27
  • 使用PageHelper插件实现Service层分页

    2023-03-06 00:40:39
  • java GUI编程之监听操作实例分析

    2022-09-28 05:55:53
  • string与stringbuilder两者的区别

    2021-11-26 00:01:06
  • SpringRetry重试框架的具体使用

    2022-09-21 00:37:35
  • C/C++根据年月日计算星期几(蔡勒公式篇)

    2023-12-05 00:48:16
  • c# 委托的常见用法

    2021-08-11 13:44:05
  • android 自定义Android菜单背景的代码

    2022-06-23 08:54:40
  • 解析Java中PriorityQueue优先级队列结构的源码及用法

    2023-11-08 13:33:11
  • JavaWeb文件上传入门教程

    2022-04-22 10:11:46
  • asp之家 软件编程 m.aspxhome.com