Skip to content

Commit

Permalink
增加动态切换表 hsweb.datasource.table.static-mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Aug 14, 2018
1 parent 50640e7 commit 03a852b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.hswebframework.web.datasource.exception.DataSourceNotFoundException;
import org.hswebframework.web.datasource.switcher.DataSourceSwitcher;
import org.hswebframework.web.datasource.switcher.DefaultDataSourceSwitcher;
import org.hswebframework.web.datasource.switcher.TableSwitcher;

/**
* 用于操作动态数据源,如获取当前使用的数据源,使用switcher切换数据源等
Expand All @@ -23,6 +24,8 @@ public final class DataSourceHolder {
*/
static volatile DynamicDataSourceService dynamicDataSourceService;

static volatile TableSwitcher tableSwitcher;

public static void checkDynamicDataSourceReady() {
if (dynamicDataSourceService == null) {
throw new UnsupportedOperationException("dataSourceService not ready");
Expand All @@ -36,6 +39,13 @@ public static DataSourceSwitcher switcher() {
return dataSourceSwitcher;
}

/**
* @return 表切换器, 用于动态切换系统功能表
*/
public static TableSwitcher tableSwitcher() {
return tableSwitcher;
}

/**
* @return 默认数据源
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import org.hswebframework.web.datasource.service.InSpringContextDynamicDataSourceService;
import org.hswebframework.web.datasource.service.InSpringDynamicDataSourceConfigRepository;
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.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
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.Configuration;

Expand All @@ -21,14 +24,20 @@
*/
@Configuration
@ImportAutoConfiguration(AopDataSourceSwitcherAutoConfiguration.class)
public class DynamicDataSourceAutoConfiguration implements BeanPostProcessor {
public class DynamicDataSourceAutoConfiguration {

@Bean
@ConditionalOnMissingBean(SqlExecutor.class)
public SqlExecutor sqlExecutor() {
return new DefaultJdbcExecutor();
}

@Bean
@ConfigurationProperties(prefix = "hsweb.datasource.table")
public DefaultTableSwitcher defaultTableSwitcher() {
return new DefaultTableSwitcher();
}

@Bean
@ConditionalOnMissingBean(DynamicDataSourceConfigRepository.class)
public InSpringDynamicDataSourceConfigRepository inSpringDynamicDataSourceConfigRepository() {
Expand All @@ -43,20 +52,28 @@ public InSpringContextDynamicDataSourceService inMemoryDynamicDataSourceService(
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;
}
};
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.hswebframework.web.datasource.switcher;

import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.ThreadLocalUtils;

import java.util.HashMap;
import java.util.Map;

/**
* @author zhouhao
* @since 3.0.0-RC
*/
public class DefaultTableSwitcher implements TableSwitcher {

private Map<String, String> staticMapping = new HashMap<>();

@Override
public void use(String source, String target) {
getMapping().put(source, target);
}

private Map<String, String> getMapping() {
return ThreadLocalUtils.get(DefaultTableSwitcher.class.getName() + "_current", HashMap::new);
}

@Override
public String getTable(String name) {
return getMapping()
.getOrDefault(name, staticMapping.getOrDefault(name, name));
}

@Override
public void reset() {
ThreadLocalUtils.remove(DefaultTableSwitcher.class.getName() + "_current");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.hswebframework.web.datasource.switcher;

/**
* 表切换器
*
* @author zhouhao
* @since 3.0.0-RC
*/
public interface TableSwitcher {
void use(String source, String target);

String getTable(String name);

void reset();
}

0 comments on commit 03a852b

Please sign in to comment.