org.zodiac.datasource.jdbc.util.DataSourceSpringUtil Maven / Gradle / Ivy
The newest version!
package org.zodiac.datasource.jdbc.util;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.commons.util.spring.Springs;
import org.zodiac.datasource.config.DataSourceConfigInfo;
import org.zodiac.datasource.jdbc.config.DataSourceRuleInfo;
public abstract class DataSourceSpringUtil {
private DataSourceSpringUtil() {
}
private static AtomicReference applicationContextRef;
/**
* 获取DBCP2数据源绑定信息。
*
* @param environment 环境上下文
* @return 数据源绑定信息
*/
public static Map getDbcp2DataSourcePoolBindings(Environment environment) {
return Springs.getBindings(environment,
SystemPropertiesConstants.Zodiac.SPRING_DATASOURCE_DBCP2_POOL_PREFIX);
}
/**
* 获取Druid数据源绑定信息。
*
* @param environment 环境上下文
* @return 数据源绑定信息
*/
public static Map getDruidDataSourcePoolBindings(Environment environment) {
// return Binder.get(environment)
// .bind(SystemPropertiesConstants.Zodiac.SPRING_DATASOURCE_POOL_PREFIX, Bindable.mapOf(String.class,
// Object.class))
// .orElse(Collections.emptyMap());
return Springs.getBindings(environment,
SystemPropertiesConstants.Zodiac.SPRING_DATASOURCE_DRUID_POOL_PREFIX);
}
/**
* 获取BeeCP数据源绑定信息。
*
* @param environment 环境上下文
* @return 数据源绑定信息
*/
public static Map getBeeDataSourcePoolBindings(Environment environment) {
return Springs.getBindings(environment,
SystemPropertiesConstants.Zodiac.SPRING_DATASOURCE_BEE_POOL_PREFIX);
}
/**
* 获取HikariCP数据源绑定信息。
*
* @param environment 环境上下文
* @return 数据源绑定信息
*/
public static Map getHikariDataSourcePoolBindings(Environment environment) {
return Springs.getBindings(environment,
SystemPropertiesConstants.Zodiac.SPRING_DATASOURCE_HIKARI_POOL_PREFIX);
}
/**
* 获取数据源规则绑定信息
*
* @param environment 环境上下文
* @return 数据源绑定信息
*/
public static Map getDataSourceRuleBindings(Environment environment) {
// return Binder.get(environment).bind(SystemPropertiesConstants.Zodiac.SPRING_DATASOURCE_JDBC_RULES_PREFIX,
// Bindable.mapOf(String.class, DataSourceRuleInfo.class)).orElse(Collections.emptyMap());
return Springs.bindAndGetProperties(environment,
SystemPropertiesConstants.Zodiac.SPRING_DATASOURCE_JDBC_RULES_PREFIX,
Bindable.mapOf(String.class, DataSourceRuleInfo.class), Collections.emptyMap());
}
/**
* 绑定数据源
*
* @param environment 环境上下文
* @param dataSourceName 数据源名称
* @param dataSourceInstance 数据源实例
*/
public static void bindDataSourceInstance(Environment environment, String dataSourceName,
DataSource dataSourceInstance) {
// Binder.get(environment).bind(dataSourceName, Bindable.ofInstance(dataSourceInstance));
Springs.bindProperties(environment, dataSourceName, Bindable.ofInstance(dataSourceInstance));
}
// /**
// * 获取数据源配置
// *
// * @param applicationContext
// * @return
// */
// public static DataSourceProperties getDataSourceProperties(ApplicationContext applicationContext) {
// return applicationContext.getBean(DataSourceProperties.class);
// }
/**
* 获取数据源配置。
*
* @param applicationContext {@link ApplicationContext}
* @return 数据源信息
*/
public static DataSourceConfigInfo getDataSourceConfigInfo(ApplicationContext applicationContext) {
return Springs.getBean(applicationContext, DataSourceConfigInfo.class);
}
/**
* 设置应用上下文。
*
* @param applicationContext 应用上下文
*/
public static void setApplicationContext(ApplicationContext applicationContext) {
applicationContextRef.compareAndSet(null, applicationContext);
}
/**
* 获取应用上下文。
*
* @return {@link ApplicationContext}
*/
public static ApplicationContext getApplicationContext() {
return applicationContextRef.get();
}
/**
* Get default datasource for dynamic datasource.
*
* @return 名称
*/
public static String getDynamicDefaultDataSource() {
return getApplicationContext().getEnvironment()
.getProperty(SystemPropertiesConstants.Zodiac.SPRING_DATASOURCE_CONFIG_DYNAMIC_ENABLED);
}
}