问题:
spring 配置多环境,在加载的时候可以配置多环境,使用不同的 数据库地址
但是,如果一个静态工具类中引用 指定的配置文件时,配置文件就无法配置多环境了。
如下:
public class ConfigUtil {
public ConfigUtil(){}
private static Properties props = new Properties();
static{
try {
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key){
return props.getProperty(key);
}
public static void updateProperties(String key,String value) {
props.setProperty(key, value);
}
}
config.properties
getui.user.appId=4sTgoIu4yr5UXqGBF5
getui.user.appKey=c0e4WQsC1Ge4PopNC8
getui.user.masterSecret=jMOq1168doVGl73UD9
getui.member.appId=ZhVkZA6Zf8Mp6ct2E6
getui.member.appKey=thC7Dmjo5LyPHNDBs0T5
getui.member.masterSecret=IQUgrA4ARt0pd2gOBX6
config.test.properties
getui.user.appId=4sTgoIu4yr5qGBF5
getui.user.appKey=c0e4WQsC14PopNC8
getui.user.masterSecret=jMOq11oV73UD9
getui.member.appId=ZhVkZA6Zf8Mp6ct2E6
getui.member.appKey=thC7Dmjo5LyPHNDBs0T5
getui.member.masterSecret=IQUgrA4ARt0pd2gOBX6
这种情况就无法选择 不同的配置文件
提供一个思路:
配置文件的名字可以使用系统环境变量来覆盖默认值,这样在启动时提供jvm启动参数或者修改系统环境变量,都可以使用不同配置文件
示例:
public static class ConfigUtil {
public ConfigUtil(){}
private static Properties props = new Properties();
private static final String DEFAULT_PROPERTIES = "config.properties";
static{
try {
String fileName = System.getProperty("systemname.config.properties");
if (StringUtils.isBlank(fileName)) {
fileName = DEFAULT_PROPERTIES;
}
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key){
return props.getProperty(key);
}
public static void updateProperties(String key,String value) {
props.setProperty(key, value);
}
}
一个配置文件,名字相同的配置2个,不启用的用#屏蔽
可以通过spring切面注解实现
建议使用spring boot来配置多环境,直接在配置文件中使用key-value来指定
1.动态配置多数据源
数据源的名称常量和一个获得和设置上下文环境的类,主要负责改变上下文数据源的名称
复制代码
public class DataSourceContextHolder {
public static final String DATA_SOURCE_A = "dataSourceA";
public static final String DATA_SOURCE_B = "dataSourceB";
public static final String DATA_SOURCE_C = "dataSourceC";
public static final String DATA_SOURCE_D = "dataSourceD";
// 线程本地环境
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
// 设置数据源类型
public static void setDbType(String dbType) {
// System.out.println("此时切换的数据源为:"+dbType);
contextHolder.set(dbType);
}
// 获取数据源类型
public static String getDbType() {
return (contextHolder.get());
}
// 清除数据源类型
public static void clearDbType() {
contextHolder.remove();
}
}
复制代码
2.建立动态数据源类,注意,这个类必须继承AbstractRoutingDataSource,且实现方法determineCurrentLookupKey,该方法返回一个Object,一般是返回字符串:
复制代码
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
System.out.println("此时获取到的数据源为:"+DataSourceContextHolder.getDbType());
return DataSourceContextHolder.getDbType();
}
}
复制代码
为了更好的理解为什么会切换数据源,我们来看一下AbstractRoutingDataSource.java源码,源码中确定数据源部分主要内容如下
复制代码
/**
* Retrieve the current target DataSource. Determines the
* {@link #determineCurrentLookupKey() current lookup key}, performs
* a lookup in the {@link #setTargetDataSources targetDataSources} map,
* falls back to the specified
* {@link #setDefaultTargetDataSource default target DataSource} if necessary.
* @see #determineCurrentLookupKey()
*/
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
return dataSource;
}
复制代码
上面这段源码的重点在于determineCurrentLookupKey()方法,这是AbstractRoutingDataSource类中的一个抽象方法,而它的返回值是你所要用的数据源dataSource的key值,有了这个key值,resolvedDataSource(这是个map,由配置文件中设置好后存入的)就从中取出对应的DataSource,如果找不到,就用配置默认的数据源。
看完源码,应该有点启发了吧,没错!你要扩展AbstractRoutingDataSource类,并重写其中的determineCurrentLookupKey()方法,来实现数据源的切换:
3.编写spring的配置文件配置多个数据源
复制代码
<!-- 配置数据源 dataSourceA-->
<!-- 初始化连接大小 -->
<property name="initialSize" value="20" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="500" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="1000" />
<!-- 监控数据库 -->
<!--<property name="filters" value="stat,log4j"/>-->
<property name="filters" value="stat" />
</bean>
<!-- 配置数据源 dataSourceB -->
<bean name="dataSourceB" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_new_url}" />
<property name="username" value="${jdbc_new_username}" />
<property name="password" value="${jdbc_new_password}" />
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="0" />
<property name="maxWait" value="10000" />
<property name="validationQuery" value="${validationQuery3}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="stat" />
</bean>
<!-- 配置数据源 dataSourceC -->
<bean name="dataSourceC" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_mysql_url}" />
<property name="username" value="${jdbc_mysql_username}" />
<property name="password" value="${jdbc_mysql_password}" />
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
<property name="validationQuery" value="${validationQuery2}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="stat" />
</bean>
<!-- 多数据源配置 -->
<bean id="dataSource" class="top.suroot.base.datasource.DynamicDataSource">
<!-- 默认使用dataSourceA的数据源 -->
<property name="defaultTargetDataSource" ref="dataSourceA"></property>
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSourceA" key="dataSourceA"></entry>
<entry value-ref="dataSourceB" key="dataSourceB"></entry>
<entry value-ref="dataSourceC" key="dataSourceC"></entry>
<!--<entry value-ref="dataSourceD" key="dataSourceD"></entry>-->
</map>
</property>
复制代码
在这个配置中第一个property属性配置目标数据源,中的key-type必须要和静态键值对照类DataSourceConst中的值的类型相 同;中key的值必须要和静态键值对照类中的值相同,如果有多个值,可以配置多个< entry>标签。第二个property属性配置默认的数据源。
4.动态切换是数据源
DataSourceContextHolder.setDbType(DataSourceContextHolder.DATA_SOURCE_B);
以上讲的是怎么动态切换数据源,下面要说下自定义注解、aop方式自动切换数据源 ,感兴趣的可以继续往下看看
5.配置自定义注解
复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface DynamicDataSourceAnnotation {
//dataSource 自定义注解的参数
String dataSource() default DataSourceContextHolder.DATA_SOURCE_A;
}
复制代码
6.配置切面类
复制代码
@Aspect
@Component
@Order(1)
public class DynamicDataSourceAspect {
@Before("@annotation(top.suroot.base.aop.DynamicDataSourceAnnotation)") //前置通知
public void testBefore(JoinPoint point){
//获得当前访问的class
Class<?> className = point.getTarget().getClass();
DynamicDataSourceAnnotation dataSourceAnnotation = className.getAnnotation(DynamicDataSourceAnnotation.class);
if (dataSourceAnnotation != null ) {
//获得访问的方法名
String methodName = point.getSignature().getName();
//得到方法的参数的类型
Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();
String dataSource = DataSourceContextHolder.DATA_SOURCE_A;
try {
Method method = className.getMethod(methodName, argClass);
if (method.isAnnotationPresent(DynamicDataSourceAnnotation.class)) {
DynamicDataSourceAnnotation annotation = method.getAnnotation(DynamicDataSourceAnnotation.class);
dataSource = annotation.dataSource();
System.out.println("DataSource Aop ====> "+dataSource);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataSourceContextHolder.setDbType(dataSource);
}
}
@After("@annotation(top.suroot.base.aop.DynamicDataSourceAnnotation)") //后置通知
public void testAfter(JoinPoint point){
//获得当前访问的class
Class<?> className = point.getTarget().getClass();
DynamicDataSourceAnnotation dataSourceAnnotation = className.getAnnotation(DynamicDataSourceAnnotation.class);
if (dataSourceAnnotation != null ) {
//获得访问的方法名
String methodName = point.getSignature().getName();
//得到方法的参数的类型
Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();
String dataSource = DataSourceContextHolder.DATA_SOURCE_A;
try {
Method method = className.getMethod(methodName, argClass);
if (method.isAnnotationPresent(DynamicDataSourceAnnotation.class)) {
DynamicDataSourceAnnotation annotation = method.getAnnotation(DynamicDataSourceAnnotation.class);
dataSource = annotation.dataSource();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(dataSource != null && !DataSourceContextHolder.DATA_SOURCE_A.equals(dataSource)) DataSourceContextHolder.clearDbType();
}
}
}
复制代码
7.在切入点添加自定义的注解
复制代码
@Service("baseService")
@DynamicDataSourceAnnotation
public class BaseServiceImpl implements BaseService {
@DynamicDataSourceAnnotation(dataSource = DataSourceContextHolder.DATA_SOURCE_B)
public void changeDataSource() {
System.out.println("切换数据源serviceImple");
}
}
复制代码
8.当然注解扫描、和aop代理一定要在配置文件中配好
<!-- 自动扫描(bean注入) -->
<context:component-scan base-package="top.suroot.*" />
<!-- AOP自动代理功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
可以通过maven的profile来控制你打包时选取的配置文件的特殊字符,这个不用代码实现。