【spring】spring如何在所有Bean创建完后做扩展

一、何时所有bean创建完
  • 1.所有bean创建完:new ApplicationContext()->refresh()->finishBeanFactoryInitialization(循环所有的BeanDefinition,通过BeanFactory.getBean()生成所有的Bean),循环结束后所有的Bean创建完
二、扩展方式
  • 1.扩展方式一:基于SmartInitializingSingleton接口,在finishBeanFactoryInitialization(beanFactory); (源码在DefaultListableBeanFactory.java的preInstantiateSingletons()方法中),实现SmartInitializingSingleton接口,可以在所有单例bean全部实例化以后回调
  • 2.扩展方式二:在AbstractApplicationContext.java中的finishRefresh()方法中,发布了一个最后的事件,publishEvent(new ContextRefreshedEvent(this));监听事件@EventListener(ContextRefreshedEvent.class)
三、示例截图

在这里插入图片描述

四、代码示例
package com.learning.beanCreationExtension;

import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.stereotype.Component;

/**
 * @Author wangyouhui
 * @Description 实现SmartInitializingSingleton接口
 **/
@Component
public class SmartInitializingSingletonExtend implements SmartInitializingSingleton {
	@Override
	public void afterSingletonsInstantiated() {
		System.out.println("SmartInitializingSingleton");
	}
}

package com.learning.beanCreationExtension;

import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @Author wangyouhui
 * @Description 监听器
 **/
@Component
public class ContextRefreshedEventListener {

	@EventListener(ContextRefreshedEvent.class)
	public void onApplicationEvent(ContextRefreshedEvent event){
		System.out.println("contextRefreshEvent");
	}

}

package com.learning.beanCreationExtension;

import org.springframework.stereotype.Component;

/**
 * @Author wangyouhui
 * @Description bean
 **/
@Component
public class UserService {
	public UserService(){
		System.out.println("对象实例化完成");
	}
}

package com.learning.beanCreationExtension;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author wangyouhui
 * @Description 配置类
 **/
@Configuration
public class BeanConfig {
	@Bean
	public UserService userService(){
		return new UserService();
	}

	@Bean
	public ContextRefreshedEventListener contextRefreshedEventListener(){
		return new ContextRefreshedEventListener();
	}

	@Bean
	public SmartInitializingSingletonExtend smartInitializingSingletonExtend(){
		return new SmartInitializingSingletonExtend();
	}
}

package com.learning.beanCreationExtension;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author wangyouhui
 * @Description 启动类
 **/
public class Applicaiton {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
		applicationContext.getBean(UserService.class);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值