SpringBoot ApplicationListener监听器的使用-监听ApplicationReadyEvent事件

SpringBoot监听器

ApplicationContext事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理。
如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEvent时,ApplicationListener Bean将自动被触发。这种事件机制都必须需要程序显示的触发。

spring boot中支持的事件类型如下:

  • ApplicationFailedEvent:该事件为spring boot启动失败时的操作
  • ApplicationPreparedEvent:上下文context准备时触发
  • ApplicationReadyEvent:上下文已经准备完毕的时候触发
  • ApplicationStartedEvent:spring boot 启动监听类
  • SpringApplicationEvent:获取SpringApplication
  • ApplicationEnvironmentPreparedEvent:环境事先准备

SpringBoot监听ApplicationReadyEvent事件用例

自定义监听器

解决mybatis多数据源,可以在项目启动的时候候指定数据源

package com.sl.listener;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;


/**
 * @author shuliangzhao
 * @Title: MyApplicationEventListener
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/10/17 20:24
 */
@Component
public class MyApplicationEventListener implements ApplicationListener<ApplicationReadyEvent> {


    @Resource(name = "targetSqlSessionTemplate")
    private SqlSessionTemplate targetSqlSessionTemplate;
    
    @Resource(name = "midSqlSessionTemplate")
    private SqlSessionTemplate midSqlSessionTemplate;

    //包名 不同的包名以逗号分隔
    @Value("{taget.source}")
    private String targetSource;

    //包名
    @Value("{mid.source}")
    private String midSource;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        ConfigurableApplicationContext applicationContext = applicationReadyEvent.getApplicationContext();
        Map<String, SqlSessionDaoSupport> beanMap = applicationContext.getBeansOfType(SqlSessionDaoSupport.class);
        String[] targetScanPath = targetSource.split(",");
        String[] midScanPath = midSource.split(",");
        List<String> targetList = Arrays.asList(targetScanPath);
        List<String> midList = Arrays.asList(midScanPath);
        beanMap.forEach((k,v) -> {
            String name = v.getClass().getPackage().getName();
            if (targetList.contains(name)) {
                v.setSqlSessionTemplate(targetSqlSessionTemplate);
            }else if (midList.contains(name)) {
                v.setSqlSessionTemplate(midSqlSessionTemplate);
            }                                                 
        } );
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

境里婆娑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值