16 Spring扩展之SmartInitializingSingleton

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。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值