作為一個 Java? 程序員 Spring? 框架在我們?nèi)粘9ぷ骱兔嬖囍锌芍^必不可少,學習和掌握好 Spring 對我們來說是很有必要的。
今天了不起就給大家介紹一下 Spring? 的 Bean 的初始化和銷毀的幾種方式,看看你平時用的都是哪種。
初始化
由于 Spring Bean? 的初始化都是 Spring 容器幫我們處理的,我們這里說的初始化是指在容器幫我們初始化的過程,我們有哪些方式可以進行手動干預,或者說初始化的時候如何運行我們自己的邏輯。
廢話不多說,我們依次來看下面的幾種方式
實現(xiàn) InitializingBean 接口
我們可以寫一個類,然后實現(xiàn) InitializingBean? 接口,通過覆蓋其中的 afterPropertiesSet()? 方法來實現(xiàn)我們自己的邏輯,我們寫一個 case 來實現(xiàn)一下,如下所示
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極客技術(shù) afterPropertiesSet";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
實現(xiàn)的方式很簡單,直接實現(xiàn) org.springframework.beans.factory.InitializingBean? 這個接口然后覆蓋 afterPropertiesSet 這個方法即可,在這個方法里面,我們就可以執(zhí)行我們需要在初始化時候就執(zhí)行的代碼。
比如這里我們在初始化方法里面直接給一個屬性進行賦值,后續(xù)就可以直接使用了,如下所示
package com.example.demo.controller;
@RestController
public class HelloController {
@Autowired
InitServiceImpl initService;
@GetMapping(value = "/hello")
public String hello(@RequestParam String name) {
return initService.getName();
}
}

可以看到是可以直接獲取到屬性值的,這個比較簡單我們就不贅述了,繼續(xù)看下面兩個方式。
增加 @PostConstruct 注解
我們繼續(xù)在 InitServiceImpl? 類中增加一個方法,并且在方法上面增加 @PostConstruct 注解,如下所示
@PostConstruct
public void postConstruct() {
System.out.println("postConstruct...");
this.name = "Java極客技術(shù) postConstruct";
}
這種寫法比實現(xiàn)一個接口要簡單一點,畢竟只要增加一個注解就行了,而不需要覆蓋接口的方法,不過本質(zhì)上沒什么區(qū)別。
自定義 init 方法
這種方式也很簡單,只不過我們做兩個動作,第一個是在 InitServiceImpl
public void initMethod() {
System.out.println("initMethod...");
this.name = "Java極客技術(shù) initMethod";
}
然后再寫一個配置類,在配置類中定義一個方法,在方法上面增加一個 @Bean? 注解,并且賦值一個 init-method 方法,同時這個方法需要創(chuàng)建對象并返回,如下所示
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();
}
}
怎么樣是不是也很簡單。
小結(jié)
在這里問大家一個問題,如果我們在同一個類中同時存在這三種初始化方法,那會是什么情況呢?
我們來試一下就知道了,通過完整的代碼,我們將三種方式都寫進來,然后啟動服務。
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極客技術(shù) afterPropertiesSet";
}
@PostConstruct
public void postConstruct() {
System.out.println("postConstruct...");
this.name = "Java極客技術(shù) postConstruct";
}
public void initMethod() {
System.out.println("initMethod...");
this.name = "Java極客技術(shù) initMethod";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
可以看到,我們的服務是正常的啟動和運行,并且三個方法都正常的執(zhí)行了,執(zhí)行的順序依次是 postConstruct,afterPropertiesSet,initMethod?,那么這里問一下小伙伴們,此時我們的 name 值是什么呢?

銷毀
既然我們初始化有三種形式,可以很自然的想到銷毀是不是也有三種形式呢?沒錯,銷毀的三種方法,也是跟上面類似,分別是
- 實現(xiàn) org.springframework.beans.factory.DisposableBean? 接口,覆蓋 destroy() 方法;
- 自定義一個方法,在方法上面增加 @PreDestroy 注解;
- 在 InitServiceImpl? 中增加一個自定義銷毀方法,然后在配置類中增加 Bean? 的 destoryMethod;
相關(guān)的內(nèi)容也比較簡單,在上面的 InitServiceImpl? 和 Config 基礎上只要增加一點點內(nèi)容就好了
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();
}
銷毀的三個方法在執(zhí)行的時候也是有優(yōu)先級的,依次是 preDestroy,destroy,destroyMethod。
至此完整的一個包含三個初始化和三個銷毀方法的代碼就完成了,我們來運行一下看看整體的流程。

總結(jié)
雖然說 Spring 給我們提供了三種初始化和三種銷毀的方法,不過我們在日常的寫代碼中很少會把三種都寫上,但是對于這幾種的優(yōu)先級還是有必要了解的,萬一別人給你挖坑了怎么辦呢?
好了,今天了不起給大家介紹了一下 Spring 的三種初始化和三種銷毀對象的方式,并且通過示例案例給大家介紹了一下優(yōu)先級,希望對大家有幫助。