由于我们系统旧环境没有用springboot自带的多数据源功能,使用了自己配置的多数据源。
添加自定义数据源注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DynDatasource {
Module value() default Module.DEF;
}
注解扫描
public CheckoutDatasourceAspect() {
}
@Around("@within(com.midea.cloud.dynamicds.aop.anno.DynDatasource)")
public Object checkoutDatasource(ProceedingJoinPoint joinPoint) throws Throwable {
Object var5;
try {
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
DynDatasource dynDatasource = (DynDatasource)methodSignature.getMethod().getDeclaredAnnotation(DynDatasource.class);
if (dynDatasource == null) {
dynDatasource = (DynDatasource)methodSignature.getDeclaringType().getDeclaredAnnotation(DynDatasource.class);
}
CheckModuleHolder.checkout(dynDatasource.value());
Object object = joinPoint.proceed();
var5 = object;
} catch (Throwable var9) {
throw var9;
} finally {
CheckModuleHolder.release();
}
return var5;
}
}
扫描配置包下的内容,自动加载
@Configuration
@EnableConfigurationProperties({DynamicDatasourceConfigProperties.class})
@ComponentScan({"com.midea.cloud.dynamicds"})
public class DynamicDatasourceAutoConfiguration {
@Autowired
private DynamicDatasourceConfigProperties dynamicDatasourceConfigProperties;
public DynamicDatasourceAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass({DynamicDatasource.class})
public DataSource dataSource() {
DynamicDatasource dynamicDatasource = new DynamicDatasource();
dynamicDatasource.setDynamicDatasourceConfigProperties(this.dynamicDatasourceConfigProperties);
return dynamicDatasource;
}
}
根据启动注解扫描加载对应的配置信息
获取yml文件中指定的配置信息
@ConfigurationProperties(
prefix = "cloud.scc.dynamic"
)
public class DynamicDatasourceConfigProperties {
private Module defaultModule;
private Map<Module, Map<String, Object>> datasouces;
public DynamicDatasourceConfigProperties() {
}
public Module getDefaultModule() {
return this.defaultModule;
}
public void setDefaultModule(Module defaultModule) {
this.defaultModule = defaultModule;
}
public Map<Module, Map<String, Object>> getDatasouces() {
return this.datasouces;
}
public void setDatasouces(Map<Module, Map<String, Object>> datasouces) {
this.datasouces = datasouces;
}
}
这里读取配置新的详情,重写springframework中的AbstractDataSource类,读取数据库详细配置信息
public abstract class AbstractDataSource implements DataSource {
protected final Log logger = LogFactory.getLog(this.getClass());
public AbstractDataSource() {
}
public int getLoginTimeout() throws SQLException {
return 0;
}
public void setLoginTimeout(int timeout) throws SQLException {
throw new UnsupportedOperationException("setLoginTimeout");
}
public PrintWriter getLogWriter() {
throw new UnsupportedOperationException("getLogWriter");
}
public void setLogWriter(PrintWriter pw) throws SQLException {
throw new UnsupportedOperationException("setLogWriter");
}
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(this)) {
return this;
} else {
throw new SQLException("DataSource of type [" + this.getClass().getName() + "] cannot be unwrapped as [" + iface.getName() + "]");
}
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface.isInstance(this);
}
public Logger getParentLogger() {
return Logger.getLogger("global");
}
}
配置启动类扫描注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({DynamicDatasourceAutoConfiguration.class})
public @interface EnableDynamicDatasource {
}
根据注解自动加载对应的数据库。
public class CheckModuleHolder {标签:return,springboot,DynDatasource,数据源,配置,Module,public,SQLException,class From: https://www.cnblogs.com/cp13215158435/p/18135572
public static ThreadLocal<Module> moduleThreadLocal = new ThreadLocal();
public CheckModuleHolder() {
}
public static void checkout(Module module) {
moduleThreadLocal.set(module);
}
public static void release() {
moduleThreadLocal.remove();
}
public static Module get() {
return (Module)moduleThreadLocal.get();
}
}