無(wú)論是Spring還是SpringBoot開發(fā)中路捧,PostConstruct注解的使用頻率還是比較高的,通常用于Bean初始化完成的一些動(dòng)作。
在項(xiàng)目代碼中琼讽,會(huì)將配置從配置中心中讀取熔萧,然后初始化到指定的Bean中糖驴。其他需要?jiǎng)討B(tài)獲取配置的地方,直接依賴注入這個(gè)Bean即可佛致。
示例代碼如下:
ApplicationConfig
動(dòng)態(tài)配置所在的類贮缕,主要是屬性。
@Configuration
@Data
@Slf4j
public class ApplicationConfig {
/**
* client host
*/
private String host;
/**
* client port
*/
private String port;
public ApplicationConfig() {
log.info("ApplicationConfig constructor execute");
}
@PostConstruct
public void init() {
log.info("ApplicationConfig postConstructor execute");
}
}
ApplicationConfigLoadService
從遠(yuǎn)程配置中心中獲取配置信息俺榆,主要依賴PostConstruct方法感昼。
@Service
@Slf4j
public class ApplicationConfigLoadService {
@Resource
private ApplicationConfig applicationConfig;
public ApplicationConfigLoadService() {
log.info("ApplicationConfigLoadService constructor execute");
}
@PostConstruct
public void load() {
log.info("ApplicationConfigLoadService postConstruct execute");
// 可以是從數(shù)據(jù)庫(kù),或者遠(yuǎn)程的配置中心中讀取配置
String host = "127.0.0.1";
String port = "8080";
applicationConfig.setHost(host);
applicationConfig.setPort(port);
}
}
ApplicationClientFactory
使用ApplicationConfig罐脊,基于配置信息定嗓,在類初始化完成后,做一些動(dòng)作萍桌。
@Component
@Slf4j
public class ApplicationClientFactory {
@Resource
private ApplicationConfig applicationConfig;
public ApplicationClientFactory() {
log.info("ApplicationClientFactory constructor execute");
}
@PostConstruct
public void init() {
log.info("ApplicationClientFactory postConstruct execute, host:{}, port:{}",
applicationConfig.getHost(), applicationConfig.getPort());
}
}
備注:
- 主要的類中宵溅,提供了一個(gè)無(wú)參的構(gòu)造方法,以及一個(gè)使用了@PostConstructor注解的初始化方法上炎,主要用于看一下執(zhí)行順序恃逻。
- 代碼說(shuō)明
- ApplicationConfigLoadService 的初始化方法中加載配置
- ApplicationConfig的setter方法完成配置初始化
- ApplicationClientFactory依賴ApplicationConfig中的屬性完成一些初始化工作。
將上述代碼執(zhí)行一下藕施,并查看日志寇损。
2021-06-06 15:38:28.591 INFO 2790 --- [ main] c.y.m.c.ApplicationClientFactory : ApplicationClientFactory constructor execute
2021-06-06 15:38:28.598 INFO 2790 --- [ main] c.y.m.configuration.ApplicationConfig : ApplicationConfig constructor execute
2021-06-06 15:38:28.599 INFO 2790 --- [ main] c.y.m.configuration.ApplicationConfig : ApplicationConfig postConstructor execute
2021-06-06 15:38:28.599 INFO 2790 --- [ main] c.y.m.c.ApplicationClientFactory : ApplicationClientFactory postConstruct execute, host:null, port:null
2021-06-06 15:38:28.602 INFO 2790 --- [ main] c.y.m.c.ApplicationConfigLoadService : ApplicationConfigLoadService constructor execute
2021-06-06 15:38:28.603 INFO 2790 --- [ main] c.y.m.c.ApplicationConfigLoadService : ApplicationConfigLoadService postConstruct execut
可以看到ApplicationClientFactory的構(gòu)造方法先被執(zhí)行,然后由于依賴ApplicationConfig類铅碍,所以ApplicationConfig的構(gòu)造方法和標(biāo)識(shí)了PostConstruct注解的方法被執(zhí)行润绵,然后才會(huì)執(zhí)行ApplicationClientFactory自己的postConstruct方法。
但是從日志中可以看出胞谈,此時(shí)由于ApplicationConfigLoadService還沒(méi)被加載尘盼,所以讀取到的配置都是空的憨愉。
嘗試的解決方案
方案1:是可以采用DependsOn指定Bean的加載順序。
修改代碼如下:
value即為依賴Bean的名稱卿捎。
@DependsOn(value = {"applicationConfigLoadService"})
@Component
@Slf4j
public class ApplicationClientFactory
Beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean. Used infrequently in cases where a bean does not explicitly depend on another through properties or constructor arguments, but rather depends on the side effects of another bean's initialization.
從JDK文檔可以看出配紫,DependsOn注解主要的使用場(chǎng)景是當(dāng)前Bean沒(méi)有顯示通過(guò)屬性或者構(gòu)造參數(shù)依賴另外一個(gè)Bean,但是卻要依賴另外一個(gè)Bean的一些初始化動(dòng)作午阵。
在上述代碼示例中躺孝,通過(guò)添加DependsOn注解,可以解決問(wèn)題底桂。
2021-06-06 16:36:59.944 INFO 3688 --- [ main] c.y.m.c.ApplicationConfigLoadService : ApplicationConfigLoadService constructor execute
2021-06-06 16:36:59.948 INFO 3688 --- [ main] c.y.m.configuration.ApplicationConfig : ApplicationConfig constructor execute
2021-06-06 16:36:59.949 INFO 3688 --- [ main] c.y.m.configuration.ApplicationConfig : ApplicationConfig postConstructor execute
2021-06-06 16:36:59.949 INFO 3688 --- [ main] c.y.m.c.ApplicationConfigLoadService : ApplicationConfigLoadService postConstruct execute
2021-06-06 16:36:59.950 INFO 3688 --- [ main] c.y.m.c.ApplicationClientFactory : ApplicationClientFactory constructor execute
2021-06-06 16:36:59.951 INFO 3688 --- [ main] c.y.m.c.ApplicationClientFactory : ApplicationClientFactory postConstruct execute, host:127.0.0.1, port:8080
方案2: 顯示通過(guò)@Resource或者@Autowired注入待依賴的Bean
在DependsOn的JDK代碼中也可以看到植袍,通過(guò)顯示依賴可以解決問(wèn)題。通過(guò)簽名日志可以看出籽懦,當(dāng)顯示依賴注入某個(gè)Bean時(shí)于个,被注入Bean會(huì)依次執(zhí)行對(duì)應(yīng)的構(gòu)造函數(shù)以及@PostConstructor注解的初始化方法。
public class ApplicationClientFactory {
@Resource
private ApplicationConfig applicationConfig;
// 顯示依賴
@Resource
private ApplicationConfigLoadService applicationConfigLoadService;
public ApplicationClientFactory() {
log.info("ApplicationClientFactory constructor execute");
}
@PostConstruct
public void init() {
log.info("ApplicationClientFactory postConstruct execute, host:{}, port:{}",
applicationConfig.getHost(), applicationConfig.getPort());
}
}
執(zhí)行結(jié)果
2021-06-06 16:08:17.458 INFO 3286 --- [ main] c.y.m.c.ApplicationClientFactory : ApplicationClientFactory constructor execute
2021-06-06 16:08:17.464 INFO 3286 --- [ main] c.y.m.configuration.ApplicationConfig : ApplicationConfig constructor execute
2021-06-06 16:08:17.465 INFO 3286 --- [ main] c.y.m.configuration.ApplicationConfig : ApplicationConfig postConstructor execute
2021-06-06 16:08:17.466 INFO 3286 --- [ main] c.y.m.c.ApplicationConfigLoadService : ApplicationConfigLoadService constructor execute
2021-06-06 16:08:17.467 INFO 3286 --- [ main] c.y.m.c.ApplicationConfigLoadService : ApplicationConfigLoadService postConstruct execute
2021-06-06 16:08:17.467 INFO 3286 --- [ main] c.y.m.c.ApplicationClientFactory : ApplicationClientFactory postConstruct execute, host:127.0.0.1, port:8080
此時(shí)可以看到在ApplicationClientFactory的postConstruc中暮顺,依賴的ApplicationConfig是有對(duì)應(yīng)屬性值的厅篓。
但是,此處會(huì)存在一個(gè)風(fēng)險(xiǎn)問(wèn)題捶码,由于applicationConfigLoadService這個(gè)變量在當(dāng)前類中并未實(shí)際使用羽氮,僅僅是為了依賴其postConstruct方法。對(duì)于后續(xù)維護(hù)的同學(xué)惫恼,很有可能無(wú)意將其移除档押。