Spring Boot实践--自定义侦听器类ApplicationListener

有时候我们需要在spring boot容器启动并加载完后,开一些线程或者一些程序来干某些事情:比如加载缓存数据等。这时候我们需要配置ContextRefreshedEvent事件来实现我们要做的事情

ApplicationListener自定义侦听器类 

@Component
public class InstantiationTracingBeanPostProcessor implements
        ApplicationListener<ContextRefreshedEvent> {
    private static final Logger LOG = Logger.getLogger(InstantiationTracingBeanPostProcessor.class);
    private static boolean initialized;
    
    @Autowired
    private ManageResolver manageResolver;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            //只在初始化“根上下文”的时候执行
            final ApplicationContext app = event.getApplicationContext();
            //event.getApplicationContext().getBean(T.CLASS) ;
            if (null == app.getParent()
                    && ("Root WebApplicationContext".equals(app.getDisplayName())
                            || app.getDisplayName().contains("AnnotationConfigEmbeddedWebApplicationContext"))
                    && "/xweb".equals(app.getApplicationName())
                    ) { // 当存在父子容器时,此判断很有用
                LOG.info("*************:" + event.getSource());
                LOG.info("*************:" + app.getDisplayName());
                LOG.info("*************:" + app.getApplicationName());
                LOG.info("*************:" + app.getBeanDefinitionCount());
                LOG.info("*************:" + app.getEnvironment());
                LOG.info("*************:" + app.getParent());
                LOG.info("*************:" + app.getParentBeanFactory());
                LOG.info("*************:" + app.getId());
                LOG.info("*************:" + app.toString());
                LOG.info("*************:" + app);
                if(!initialized && !manageResolver.IsInitialCompleted()) {
                    manageResolver.initLater();
                    initialized = true;
                }
            }
        } catch (Exception e) {
            LOG.error("((XmlWebApplicationContext) event.getSource()).getDisplayName() 执行失败,请检查Spring版本是否支持");
        }
    }

}

 

SpringBoot应用程序启动类

@SpringBootApplication
@ImportResource({"classpath:config/applicationContext-xweb-dubbo.xml","classpath:config/applicationContext-xweb.xml"})
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, RedisAutoConfiguration.class})
public class XwebApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication springApplication =new SpringApplication(XwebApplication.class);
        springApplication.addListeners(new InstantiationTracingBeanPostProcessor());
        springApplication.run(args);
    }  
    
    /**
     * 上传附件容量限制
     * @return
     */
    @Bean  
    public MultipartConfigElement multipartConfigElement() {  
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("102400KB");  
        factory.setMaxRequestSize("112400KB");  
        return factory.createMultipartConfig();  
    } 
    
    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
    
}

PS:还有一些spring内置的事件

  • 1、 ContextRefreshedEvent:ApplicationContext容器初始化或者刷新时触发该事件。
  • 2、 ContextStartedEvent:当使用ConfigurableApplicationContext接口的start()方法启动ApplicationContext容器时触发该事件。
  • 3、 ContextClosedEvent:当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext容器时触发该事件。
  • 4、 ContextStopedEvent: 当使用ConfigurableApplicationContext接口的stop()方法停止ApplicationContext容器时触发该事件。

 

转载于:https://my.oschina.net/spinachgit/blog/1635218

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值