|
@@ -6,11 +6,14 @@ import org.hswebframework.web.datasource.config.InSpringDynamicDataSourceConfig;
|
|
import org.hswebframework.web.datasource.service.InSpringContextDynamicDataSourceService;
|
|
import org.hswebframework.web.datasource.service.InSpringContextDynamicDataSourceService;
|
|
import org.hswebframework.web.datasource.service.InSpringDynamicDataSourceConfigRepository;
|
|
import org.hswebframework.web.datasource.service.InSpringDynamicDataSourceConfigRepository;
|
|
import org.hswebframework.web.datasource.switcher.DataSourceSwitcher;
|
|
import org.hswebframework.web.datasource.switcher.DataSourceSwitcher;
|
|
|
|
+import org.hswebframework.web.datasource.switcher.DefaultTableSwitcher;
|
|
|
|
+import org.hswebframework.web.datasource.switcher.TableSwitcher;
|
|
import org.springframework.beans.BeansException;
|
|
import org.springframework.beans.BeansException;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
@@ -21,7 +24,7 @@ import javax.sql.DataSource;
|
|
*/
|
|
*/
|
|
@Configuration
|
|
@Configuration
|
|
@ImportAutoConfiguration(AopDataSourceSwitcherAutoConfiguration.class)
|
|
@ImportAutoConfiguration(AopDataSourceSwitcherAutoConfiguration.class)
|
|
-public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
|
|
|
|
|
|
+public class DynamicDataSourceAutoConfiguration {
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
@ConditionalOnMissingBean(SqlExecutor.class)
|
|
@ConditionalOnMissingBean(SqlExecutor.class)
|
|
@@ -29,6 +32,12 @@ public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
|
|
return new DefaultJdbcExecutor();
|
|
return new DefaultJdbcExecutor();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Bean
|
|
|
|
+ @ConfigurationProperties(prefix = "hsweb.datasource.table")
|
|
|
|
+ public DefaultTableSwitcher defaultTableSwitcher() {
|
|
|
|
+ return new DefaultTableSwitcher();
|
|
|
|
+ }
|
|
|
|
+
|
|
@Bean
|
|
@Bean
|
|
@ConditionalOnMissingBean(DynamicDataSourceConfigRepository.class)
|
|
@ConditionalOnMissingBean(DynamicDataSourceConfigRepository.class)
|
|
public InSpringDynamicDataSourceConfigRepository inSpringDynamicDataSourceConfigRepository() {
|
|
public InSpringDynamicDataSourceConfigRepository inSpringDynamicDataSourceConfigRepository() {
|
|
@@ -43,20 +52,28 @@ public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
|
|
return new InSpringContextDynamicDataSourceService(repository, dataSourceProxy);
|
|
return new InSpringContextDynamicDataSourceService(repository, dataSourceProxy);
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
|
|
|
- return bean;
|
|
|
|
- }
|
|
|
|
|
|
+ @Bean
|
|
|
|
+ public BeanPostProcessor switcherInitProcessor() {
|
|
|
|
+ return new BeanPostProcessor() {
|
|
|
|
+ @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 DynamicDataSourceService) {
|
|
|
|
- DataSourceHolder.dynamicDataSourceService = ((DynamicDataSourceService) bean);
|
|
|
|
- }
|
|
|
|
- if (bean instanceof DataSourceSwitcher) {
|
|
|
|
- DataSourceHolder.dataSourceSwitcher = ((DataSourceSwitcher) bean);
|
|
|
|
- }
|
|
|
|
- return bean;
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
|
+ if (bean instanceof DynamicDataSourceService) {
|
|
|
|
+ DataSourceHolder.dynamicDataSourceService = ((DynamicDataSourceService) bean);
|
|
|
|
+ }
|
|
|
|
+ if (bean instanceof DataSourceSwitcher) {
|
|
|
|
+ DataSourceHolder.dataSourceSwitcher = ((DataSourceSwitcher) bean);
|
|
|
|
+ }
|
|
|
|
+ if (bean instanceof TableSwitcher) {
|
|
|
|
+ DataSourceHolder.tableSwitcher = ((TableSwitcher) bean);
|
|
|
|
+ }
|
|
|
|
+ return bean;
|
|
|
|
+ }
|
|
|
|
+ };
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|