mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
update docs
This commit is contained in:
parent
1dfbe5db7d
commit
87d6e58040
@ -198,5 +198,21 @@ HikariDataSource newDataSource = new HikariDataSource();
|
||||
flexDataSource.addDataSource("newKey", newDataSource);
|
||||
```
|
||||
|
||||
> 需要注意的是:通过 FlexGlobalConfig 去获取 FlexDataSource 时,需等待应用完全启动成功后,才能正常获取 FlexDataSource,
|
||||
> 否则将会得到 null 值。
|
||||
**需要注意的是:** 通过 FlexGlobalConfig 去获取 FlexDataSource 时,需等待应用完全启动成功后,才能正常获取 FlexDataSource,
|
||||
否则将会得到 null 值。
|
||||
|
||||
Spring 用户可以通过 `ApplicationListener` 去监听 `ContextRefreshedEvent` 事件,然后再去添加新的数据源,如下代码所示:
|
||||
|
||||
```java
|
||||
public class DataSourceInitListener implements ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
FlexDataSource dataSource = (FlexDataSource) FlexGlobalConfig.getDefaultConfig()
|
||||
.getConfiguration().getEnvironment().getDataSource();
|
||||
|
||||
dataSource.addDataSource("....", new DruidDataSource("..."));
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@ -0,0 +1,20 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.datasource.FlexDataSource;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DataSourceInitListener implements ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
FlexDataSource dataSource = (FlexDataSource) FlexGlobalConfig.getDefaultConfig()
|
||||
.getConfiguration().getEnvironment().getDataSource();
|
||||
|
||||
System.out.println("onApplicationEvent>>>> datasource:" + dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user