SpringBoot ApplicationListener Listenerの使用-ApplicationReadyEventイベントのリスニング
11571 ワード
SpringBootリスナー
ApplicationContextイベントメカニズムはオブザーバー設計モードの実装であり,ApplicationEventクラスとApplicationListenerインタフェースによりApplicationContextイベント処理を実現できる.コンテナにApplicationListener Beanがある場合、ApplicationContextがApplicationEventを発行するたびに、ApplicationListener Beanが自動的にトリガーされます.このイベントメカニズムには、プログラム表示のトリガが必要です.
Spring bootでサポートされているイベントのタイプは次のとおりです. ApplicationFailedEvent:このイベントはspring bootの起動に失敗したときの操作 です. ApplicationPreparedEvent:コンテキストcontext準備時に がトリガーされます. ApplicationReadyEvent:コンテキストの準備が完了すると がトリガーされます. ApplicationStartedEvent:spring bootリスニングクラス を開始 SpringApplicationEvent:SpringApplication を取得する A p l i c a tionEnvironmentPreparedEvent:環境事前準備 SpringBootリスニングアプリケーションReadyEventイベントの例
カスタムリスナー
mybatisマルチデータソースを解決し、プロジェクトの起動時にデータソースを指定することができます.
ApplicationContextイベントメカニズムはオブザーバー設計モードの実装であり,ApplicationEventクラスとApplicationListenerインタフェースによりApplicationContextイベント処理を実現できる.コンテナにApplicationListener Beanがある場合、ApplicationContextがApplicationEventを発行するたびに、ApplicationListener Beanが自動的にトリガーされます.このイベントメカニズムには、プログラム表示のトリガが必要です.
Spring bootでサポートされているイベントのタイプは次のとおりです.
カスタムリスナー
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);
}
} );
}
}