|
@@ -1,11 +1,14 @@
|
|
|
package org.hswebframework.web.datasource;
|
|
|
|
|
|
import org.hswebframework.ezorm.rdb.executor.SqlExecutor;
|
|
|
+import org.hswebframework.web.datasource.service.InMemoryDynamicDataSourceService;
|
|
|
import org.hswebframework.web.datasource.starter.AopDataSourceSwitcherAutoConfiguration;
|
|
|
import org.hswebframework.web.datasource.switcher.DataSourceSwitcher;
|
|
|
import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
@@ -13,8 +16,6 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
/**
|
|
|
- * TODO 完成注释
|
|
|
- *
|
|
|
* @author zhouhao
|
|
|
*/
|
|
|
@Configuration
|
|
@@ -29,20 +30,9 @@ public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
|
|
|
|
|
|
@Bean
|
|
|
@ConditionalOnMissingBean(DynamicDataSourceService.class)
|
|
|
- public DynamicDataSourceService justSupportDefaultDataSourceService(DataSource dataSource) {
|
|
|
+ public InMemoryDynamicDataSourceService inMemoryDynamicDataSourceService(DataSource dataSource) {
|
|
|
DynamicDataSourceProxy dataSourceProxy = new DynamicDataSourceProxy(null, dataSource);
|
|
|
- return new DynamicDataSourceService() {
|
|
|
- @Override
|
|
|
- public DynamicDataSource getDataSource(String dataSourceId) {
|
|
|
- throw new UnsupportedOperationException("dynamic datasource not enable");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public DynamicDataSource getDefaultDataSource() {
|
|
|
- return dataSourceProxy;
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
+ return new InMemoryDynamicDataSourceService(dataSourceProxy);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -50,6 +40,32 @@ public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
|
|
|
return bean;
|
|
|
}
|
|
|
|
|
|
+ @ConditionalOnBean(InMemoryDynamicDataSourceService.class)
|
|
|
+ @Configuration
|
|
|
+ public static class AutoRegisterDataSource implements BeanPostProcessor {
|
|
|
+
|
|
|
+ private InMemoryDynamicDataSourceService dataSourceService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setDataSourceService(InMemoryDynamicDataSourceService dataSourceService) {
|
|
|
+ DataSourceHolder.dynamicDataSourceService = dataSourceService;
|
|
|
+ this.dataSourceService = dataSourceService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ if (bean instanceof DataSource) {
|
|
|
+ dataSourceService.registerDataSource(beanName, ((DataSource) bean));
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
if (bean instanceof DynamicDataSourceService) {
|