Spring5源码 - 14 如何在所有Bean创建完后做扩展?
        【摘要】 
                    
                        
                    
                    
 文章目录
 葛大爷的问题Answer方式一 基于SmartInitializingSingleton接口SourceCode
   方式二 基于Spring事件监听SourceCode
  
  验...
    
    
    
    
葛大爷的问题

Answer
想要回答这个问题,就要对Spring的生命周期有一定的了解,今天我们就来回顾一下IOC的生命周期及Spring提供给开发人员的扩展点,当然了,我们今天只聊Bean加载完成后的事儿 。
老规矩 先应用后源码 ,开搞~

AAA BBB CCC 均是spring管理的bean
@Component
public class AAA {
	
	public AAA() {
		System.out.println("AAA init");
	}
}
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 
方式一 基于SmartInitializingSingleton接口
Source
生命周期中倒数第二步
// Instantiate all remaining (non-lazy-init) singletons.
 finishBeanFactoryInitialization(beanFactory);
  
 - 1
 - 2
 
SmartInitializingSingleton接口是在所有的Bean实例化完成以后,Spring回调的方法, 所以这里也是一个扩展点,可以在单例bean全部完成实例化以后做处理。

Code
【配置类】
package com.artisan.beanLoadedExtend.smartinit;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.artisan.beanLoadedExtend")
public class SmartInitConfig {
}
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 
【扩展类 implements SmartInitializingSingleton 】
package com.artisan.beanLoadedExtend.smartinit;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.stereotype.Component;
@Component
public class SmartInitExtend implements SmartInitializingSingleton {
	@Override
	public void afterSingletonsInstantiated() {
		System.out.println("all singleton beans loaded , 自定义扩展here ");
	}
}
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 
【测试】
package com.artisan.beanLoadedExtend.smartinit;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SmartInitConfig.class);
	}
}
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 
方式二 基于Spring事件监听
Source
生命周期的最后一步是finishRefresh();,这里面中有一个方法是publishEvent

所以这里也可以进行扩展,监听ContextRefreshedEvent事件 。
Code
【配置类】
package com.artisan.beanLoadedExtend.listener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.artisan.beanLoadedExtend")
public class Config {
}
    
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 
【基于接口的方式】
package com.artisan.beanLoadedExtend.listener;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class BeanLoadedExtendListener implements ApplicationListener<ContextRefreshedEvent> {
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		System.out.println("监听到ContextRefreshedEvent, 自定义扩展here ");
	}
}
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 
【基于注解的方式】
package com.artisan.beanLoadedExtend.listener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class BeanLoadedExtendListenerByAnno  {
	@EventListener(ContextRefreshedEvent.class)
	public void extend(){
		System.out.println("基于@EventListener的监听");
	}
}
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 
二选一,推荐基于注解的方式
【测试】
package com.artisan.beanLoadedExtend.listener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
	}
}
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 
验证


文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。
原文链接:artisan.blog.csdn.net/article/details/109400817
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)