需求背景:
1、项目启动时,加载缓存
2、项目启动时,开启对外服务端
一、InitializingBean实现
InitializingBean简介:
InitializingBean是Spring提供的拓展性接口,InitializingBean接口为bean提供了属性初始化后的处理方法,它只有一个afterPropertiesSet方法,凡是继承该接口的类,在bean的属性初始化后都会执行该方法。
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() {
/** 另起一个线程器启动netty,主线程继续启动项目 **/
new Thread(){
@Override
public void run() {
nettyServer.start();
}
}.start();
log.info("netty对外服务端已启动...");
}
}
效果图:
二、@PostConstruct注解实现
@PostContruct是JDK自带的注解,用来修饰非静态void方法。spring初始化该bean后,会执行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("通过@PostConstruct注解,实现项目启动时加载缓存...");
}
}
效果展示:
三、通过配置@bean的init-method属性实现
代码实现:
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("通过配置@bean的init-method属性,实现项目启动时加载缓存...");
}
}
效果展示:
四、通过bean构造方法实现
bean初始化时实现。(没见用过,最好别用这种方式)
代码实例:
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("通过放入bean构造方法,实现项目启动时加载缓存...");
}
}
效果展示:
五、四种方式优先级比较
测试代码:
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() {
/** 另起一个线程器启动netty,主线程继续启动项目 **/
new Thread(){
@Override
public void run() {
nettyServer.start();
}
}.start();
log.info("netty对外服务端已启动...");
}
@PostConstruct
public void init1() {
log.info("通过@PostConstruct注解,实现项目启动时加载缓存...");
}
public void init2() {
log.info("通过放入bean构造方法,实现项目启动时加载缓存...");
}
@Bean(initMethod = "init")
public void init3() {
log.info("通过配置@bean的init-method属性,实现项目启动时加载缓存...");
}
}
测试结果展示:
由此可以看出:
优先级: 构造方法 > @PostContruct > afterPropertiesSet() > init-method