盘点Spring的Bean的初始化和销毁的几种方式

发布时间:2026/5/16 10:11:20

盘点Spring的Bean的初始化和销毁的几种方式 作为一个 Java 程序员 Spring 框架在我们日常工作和面试中可谓必不可少学习和掌握好 Spring 对我们来说是很有必要的。今天了不起就给大家介绍一下 Spring 的 Bean 的初始化和销毁的几种方式看看你平时用的都是哪种。初始化由于 Spring Bean 的初始化都是 Spring 容器帮我们处理的我们这里说的初始化是指在容器帮我们初始化的过程我们有哪些方式可以进行手动干预或者说初始化的时候如何运行我们自己的逻辑。废话不多说我们依次来看下面的几种方式实现InitializingBean接口我们可以写一个类然后实现 InitializingBean 接口通过覆盖其中的 afterPropertiesSet() 方法来实现我们自己的逻辑我们写一个 case 来实现一下如下所示package com.example.demo.service; import org.springframework.beans.factory.InitializingBean; public class InitServiceImpl implements InitializingBean { private String name; Override public void afterPropertiesSet() throws Exception { System.out.println(afterPropertiesSet...); this.name Java极客技术 afterPropertiesSet; } public String getName() { return name; } public void setName(String name) { this.name name; } }实现的方式很简单直接实现 org.springframework.beans.factory.InitializingBean 这个接口然后覆盖 afterPropertiesSet 这个方法即可在这个方法里面我们就可以执行我们需要在初始化时候就执行的代码。比如这里我们在初始化方法里面直接给一个属性进行赋值后续就可以直接使用了如下所示package com.example.demo.controller; RestController public class HelloController { Autowired InitServiceImpl initService; GetMapping(value /hello) public String hello(RequestParam String name) { return initService.getName(); } }可以看到是可以直接获取到属性值的这个比较简单我们就不赘述了继续看下面两个方式。增加 PostConstruct 注解我们继续在 InitServiceImpl 类中增加一个方法并且在方法上面增加 PostConstruct 注解如下所示PostConstruct public void postConstruct() { System.out.println(postConstruct...); this.name Java极客技术 postConstruct; }这种写法比实现一个接口要简单一点毕竟只要增加一个注解就行了而不需要覆盖接口的方法不过本质上没什么区别。自定义init方法这种方式也很简单只不过我们做两个动作第一个是在 InitServiceImpl 类中增加一个自定义的方法方法内容如下public void initMethod() { System.out.println(initMethod...); this.name Java极客技术 initMethod; }然后再写一个配置类在配置类中定义一个方法在方法上面增加一个 Bean 注解并且赋值一个 init-method 方法同时这个方法需要创建对象并返回如下所示package com.example.demo.service; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class Config { Bean(initMethod initMethod) public InitServiceImpl initServiceImpl() { return new InitServiceImpl(); } }怎么样是不是也很简单。小结在这里问大家一个问题如果我们在同一个类中同时存在这三种初始化方法那会是什么情况呢我们来试一下就知道了通过完整的代码我们将三种方式都写进来然后启动服务。package com.example.demo.service; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import javax.annotation.PostConstruct; public class InitServiceImpl implements InitializingBean { private String name; Override public void afterPropertiesSet() throws Exception { System.out.println(afterPropertiesSet...); this.name Java极客技术 afterPropertiesSet; } PostConstruct public void postConstruct() { System.out.println(postConstruct...); this.name Java极客技术 postConstruct; } public void initMethod() { System.out.println(initMethod...); this.name Java极客技术 initMethod; } public String getName() { return name; } public void setName(String name) { this.name name; } }可以看到我们的服务是正常的启动和运行并且三个方法都正常的执行了执行的顺序依次是 postConstructafterPropertiesSetinitMethod那么这里问一下小伙伴们此时我们的 name 值是什么呢销毁既然我们初始化有三种形式可以很自然的想到销毁是不是也有三种形式呢没错销毁的三种方法也是跟上面类似分别是实现 org.springframework.beans.factory.DisposableBean 接口覆盖 destroy() 方法自定义一个方法在方法上面增加 PreDestroy 注解在 InitServiceImpl 中增加一个自定义销毁方法然后在配置类中增加 Bean 的 destoryMethod相关的内容也比较简单在上面的 InitServiceImpl 和 Config 基础上只要增加一点点内容就好了public class InitServiceImpl implements InitializingBean, DisposableBean { public void destroyMethod() throws Exception { System.out.println(destroyMethod...); } Override public void destroy() throws Exception { System.out.println(destroy...); } PreDestroy public void preDestroy() throws Exception { System.out.println(preDestroy...); } } Bean(initMethod initMethod, destroyMethod destroyMethod) public InitServiceImpl initServiceImpl() { return new InitServiceImpl(); }销毁的三个方法在执行的时候也是有优先级的依次是 preDestroydestroydestroyMethod。至此完整的一个包含三个初始化和三个销毁方法的代码就完成了我们来运行一下看看整体的流程。总结虽然说 Spring 给我们提供了三种初始化和三种销毁的方法不过我们在日常的写代码中很少会把三种都写上但是对于这几种的优先级还是有必要了解的万一别人给你挖坑了怎么办呢好了今天了不起给大家介绍了一下 Spring 的三种初始化和三种销毁对象的方式并且通过示例案例给大家介绍了一下优先级希望对大家有帮助。

相关新闻