InitializingBean、@PostConstruct、
2022-09-08 本文已影响0人
King斌
一、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对外服务端已启动...");
}
}
效果图:
image.png
二、@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属性,实现项目启动时加载缓存...");
}
}
效果展示:
image.png四、通过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构造方法,实现项目启动时加载缓存...");
}
}
效果展示:
image.png
五、四种方式优先级比较
测试代码:
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