1 介绍
public interface SmartInitializingSingleton {
/**
* Invoked right at the end of the singleton pre-instantiation phase,
* with a guarantee that all regular singleton beans have been created
* already. {@link ListableBeanFactory#getBeansOfType} calls within
* this method won't trigger accidental side effects during bootstrap.
* <p><b>NOTE:</b> This callback won't be triggered for singleton beans
* lazily initialized on demand after {@link BeanFactory} bootstrap,
* and not for any other bean scope either. Carefully use it for beans
* with the intended bootstrap semantics only.
*/
void afterSingletonsInstantiated();
}
Invoked right at the end of the singleton pre-instantiation phase: 在单例预实例化阶段结束时立即调用
2 测试执行时机
实现该接口
package study.wyy.spring.extend.smartInitializingSingleton.spi;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.stereotype.Component;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:37 下午
*/
@Component
public class MySmartInitializingSingleton implements SmartInitializingSingleton {
@Override
public void afterSingletonsInstantiated() {
System.out.println("MySmartInitializingSingleton => afterSingletonsInstantiated");
}
}
准备两个bean
package study.wyy.spring.extend.smartInitializingSingleton.bean;
/**
* @author wyaoyao
* @data 2020-11-23 07:49
*/
public class Bean1 {
public String name;
/**
* 构造方法
*/
public Bean1() {
System.out.println("Bean1 constructor。。。");
}
/**
* 初始化方法
*/
public void init(){
System.out.println("Bean1 init。。。");
name = "hello";
}
/**
* 销毁方法
*/
public void destroy(){
System.out.println("Bean1 destroy。。。");
}
}
package study.wyy.spring.extend.smartInitializingSingleton.bean;
/**
* @author wyaoyao
* @data 2020-11-23 07:49
*/
public class Bean2 {
public String name;
/**
* 构造方法
*/
public Bean2() {
System.out.println("Bean2 constructor。。。");
}
/**
* 初始化方法
*/
public void init(){
System.out.println("Bean2 init。。。");
name = "world";
}
/**
* 销毁方法
*/
public void destroy(){
System.out.println("Bean2 destroy。。。");
}
}
配置
package study.wyy.spring.extend.smartInitializingSingleton.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import study.wyy.spring.extend.smartInitializingSingleton.bean.Bean1;
import study.wyy.spring.extend.smartInitializingSingleton.bean.Bean2;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:38 下午
*/
@Configuration
@ComponentScan("study.wyy.spring.extend.smartInitializingSingleton")
public class SpringConfig {
@Bean(initMethod = "init",destroyMethod = "destroy")
public Bean1 bean1(){
return new Bean1();
}
@Bean(initMethod = "init",destroyMethod = "destroy")
public Bean2 bean2(){
return new Bean2();
}
}
测试
package study.wyy.spring.extend.smartInitializingSingleton.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import study.wyy.spring.extend.smartInitializingSingleton.config.SpringConfig;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 10:43 下午
*/
public class Test {
@org.junit.Test
public void test01(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
context.close();
}
}
输出
Bean1 constructor。。。
Bean1 init。。。
Bean2 constructor。。。
Bean2 init。。。
MySmartInitializingSingleton => afterSingletonsInstantiated
bean1.name => hello
bean2.name =>world
Bean2 destroy。。。
Bean1 destroy。。。