需求背景:
1、項(xiàng)目啟動(dòng)時(shí)友酱,加載緩存
2晴音、項(xiàng)目啟動(dòng)時(shí),開(kāi)啟對(duì)外服務(wù)端
一缔杉、InitializingBean實(shí)現(xiàn)
InitializingBean簡(jiǎn)介:
InitializingBean是Spring提供的拓展性接口锤躁,InitializingBean接口為bean提供了屬性初始化后的處理方法,它只有一個(gè)afterPropertiesSet方法或详,凡是繼承該接口的類(lèi)系羞,在bean的屬性初始化后都會(huì)執(zhí)行該方法。
InitializingBean用法:
package com.pcf.spdemo.configuration;
import com.pcf.spdemo.common.httpserver.NettyServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanInit implements InitializingBean {
private static Logger log = LoggerFactory.getLogger(BeanInit.class);
@Autowired
private NettyServer nettyServer;
@Override
public void afterPropertiesSet() throws Exception {
log.info("netty----------------------------------");
init();
}
private void init() {
/** 另起一個(gè)線程器啟動(dòng)netty霸琴,主線程繼續(xù)啟動(dòng)項(xiàng)目 **/
new Thread(){
@Override
public void run() {
nettyServer.start();
}
}.start();
log.info("netty對(duì)外服務(wù)端已啟動(dòng)...");
}
}
效果圖:
二椒振、@PostConstruct注解實(shí)現(xiàn)
@PostContruct是JDK自帶的注解,用來(lái)修飾非靜態(tài)void方法梧乘。spring初始化該bean后杠人,會(huì)執(zhí)行bean中的該方法。
示例代碼:
package com.pcf.spdemo.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class InitCache {
private static Logger log = LoggerFactory.getLogger(InitCache.class);
@PostConstruct
public void init() {
log.info("通過(guò)@PostConstruct注解宋下,實(shí)現(xiàn)項(xiàng)目啟動(dòng)時(shí)加載緩存...");
}
}
效果展示:
三嗡善、通過(guò)配置@bean的init-method屬性實(shí)現(xiàn)
代碼實(shí)現(xiàn):
package com.pcf.spdemo.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class InitCache {
private static Logger log = LoggerFactory.getLogger(InitCache.class);
@Bean(initMethod = "init")
public void init() {
log.info("通過(guò)配置@bean的init-method屬性,實(shí)現(xiàn)項(xiàng)目啟動(dòng)時(shí)加載緩存...");
}
}
效果展示:
四学歧、通過(guò)bean構(gòu)造方法實(shí)現(xiàn)
bean初始化時(shí)實(shí)現(xiàn)罩引。(沒(méi)見(jiàn)用過(guò),最好別用這種方式)
代碼實(shí)例:
package com.pcf.spdemo.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class InitCache {
private static Logger log = LoggerFactory.getLogger(InitCache.class);
public InitCache() {
init();
}
public void init() {
log.info("通過(guò)放入bean構(gòu)造方法枝笨,實(shí)現(xiàn)項(xiàng)目啟動(dòng)時(shí)加載緩存...");
}
}
效果展示:
五袁铐、四種方式優(yōu)先級(jí)比較
測(cè)試代碼:
package com.pcf.spdemo.configuration;
import com.pcf.spdemo.common.httpserver.NettyServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class BeanInit implements InitializingBean {
private static Logger log = LoggerFactory.getLogger(BeanInit.class);
@Autowired
private NettyServer nettyServer;
public BeanInit() {
init2();
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("netty----------------------------------");
init();
}
private void init() {
/** 另起一個(gè)線程器啟動(dòng)netty,主線程繼續(xù)啟動(dòng)項(xiàng)目 **/
new Thread(){
@Override
public void run() {
nettyServer.start();
}
}.start();
log.info("netty對(duì)外服務(wù)端已啟動(dòng)...");
}
@PostConstruct
public void init1() {
log.info("通過(guò)@PostConstruct注解横浑,實(shí)現(xiàn)項(xiàng)目啟動(dòng)時(shí)加載緩存...");
}
public void init2() {
log.info("通過(guò)放入bean構(gòu)造方法剔桨,實(shí)現(xiàn)項(xiàng)目啟動(dòng)時(shí)加載緩存...");
}
@Bean(initMethod = "init")
public void init3() {
log.info("通過(guò)配置@bean的init-method屬性,實(shí)現(xiàn)項(xiàng)目啟動(dòng)時(shí)加載緩存...");
}
}
測(cè)試結(jié)果展示:
由此可以看出:
優(yōu)先級(jí): 構(gòu)造方法 > @PostContruct > afterPropertiesSet() > init-method