@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
public static Object getBean(String name) throws BeansException {
try{
return applicationContext.getBean(name);
}catch(BeansException e){
return null;
}
}
}
DBConfig:
/**
* @author: yxz
* @version: 1.0
* @date:
*/
public class DBConfig {
private CommonHibernateDao mainHibernateDao = null;
private CommonJdbcDao mainJdbcDao = null;
private List<CommonHibernateDao> hibernateDaos = null;
private List<CommonJdbcDao> jdbcDaos = null;
private Map<String ,Integer> dbKeys = new HashMap<>();
private static boolean useContext = false;
private static final ThreadLocal<Integer> CURRENT_INDEX = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 0;
}
};
public static void clear(){
useContext = true;
CURRENT_INDEX.remove();
}
public void setDbKeys(Map<String, Integer> dbKeys) {
this.dbKeys = dbKeys;
}
public int getDbIndex(String dbKey){
if(this.dbKeys==null){
return -1;
}
if(this.dbKeys.containsKey(dbKey)){
return this.dbKeys.get(dbKey);
}
return -1;
}
public void setSessionFactory(SessionFactory sessionFactory) {
mainHibernateDao = new CommonHibernateDaoImpl();
mainHibernateDao.setSessionFactory(sessionFactory);
hibernateDaos = new ArrayList<CommonHibernateDao>();
hibernateDaos.add(mainHibernateDao);
if (mainJdbcDao == null) {
DataSource dataSource = SessionFactoryUtils.getDataSource(sessionFactory);
if (dataSource != null) {
setDataSource(dataSource);
}
}
}
public void setDataSource(DataSource dataSource) {
mainJdbcDao = new CommonJdbcDaoImpl();
mainJdbcDao.setDataSource(dataSource);
mainJdbcDao.setDataBaseType(checkDataBaseType(dataSource));
jdbcDaos = new ArrayList<CommonJdbcDao>();
jdbcDaos.add(mainJdbcDao);
}
public void setSessionFactorys(List<SessionFactory> sessionFactorys) {
List<DataSource> dataSources = new ArrayList<>();
boolean noDs = (jdbcDaos==null || jdbcDaos.size() == 0);
if (sessionFactorys.size() > 0) {
hibernateDaos = new ArrayList<CommonHibernateDao>();
for (int i = 0; i < sessionFactorys.size(); i++) {
CommonHibernateDao commonHibernateDao = new CommonHibernateDaoImpl();
commonHibernateDao.setSessionFactory(sessionFactorys.get(i));
hibernateDaos.add(commonHibernateDao);
if (i == 0) {
mainHibernateDao = commonHibernateDao;
}
if (noDs) {
DataSource dataSource = SessionFactoryUtils.getDataSource(sessionFactorys.get(i));
if (dataSource != null) {
dataSources.add(dataSource);
}
}
}
if (noDs) {
setDataSources(dataSources);
}
}
}
public void setDataSources(List<DataSource> dataSources) {
if (dataSources.size() > 0) {
jdbcDaos = new ArrayList<CommonJdbcDao>();
for (int i = 0; i < dataSources.size(); i++) {
CommonJdbcDao commonJdbcDao = new CommonJdbcDaoImpl();
commonJdbcDao.setDataSource(dataSources.get(i));
commonJdbcDao.setDataBaseType(checkDataBaseType(dataSources.get(i)));
jdbcDaos.add(commonJdbcDao);
if (i == 0) {
mainJdbcDao = commonJdbcDao;
}
}
}
}
/**
* 判断数据库类型
*
* @param dataSource
* @return
*/
DataBaseType checkDataBaseType(DataSource dataSource) {
Connection connection = null;
try {
connection = dataSource.getConnection();
String driveClass = connection.getMetaData().getDriverName().toLowerCase();
if (driveClass.contains("mysql")) {
return DataBaseType.MYSQL;
} else if (driveClass.contains("oracle")) {
return DataBaseType.ORACLE;
} else {
return DataBaseType.UNDEFINE;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(connection!=null){
try{
connection.close();
}catch(Exception ignore){
}
}
}
return DataBaseType.UNKNOWN;
}
CommonJdbcDao getJdbcDao(int dbIndex) {
if (dbIndex < 0) {
dbIndex = CURRENT_INDEX.get();
if(dbIndex==0){
return mainJdbcDao;
}
}
if (dbIndex >= jdbcDaos.size()) {
throw new DatabaseException("无效的数据源索引号");
}
return jdbcDaos.get(dbIndex);
}
CommonHibernateDao getHibernateDao(int dbIndex) {
if (dbIndex < 0) {
dbIndex = CURRENT_INDEX.get();
if(dbIndex==0){
return mainHibernateDao;
}
}
if (dbIndex >= hibernateDaos.size()) {
throw new DatabaseException("无效的数据源索引号");
}
return hibernateDaos.get(dbIndex);
}
/**
* 切换数据库
*
* @param dbIndex
*/
int switchTo(int dbIndex) {
int hSize = hibernateDaos.size();
int jSize = jdbcDaos.size();
if (dbIndex < 0 || (dbIndex >= jSize && dbIndex >= hSize)) {
throw new DatabaseException("无效的数据源索引号");
}
int oldDB = CURRENT_INDEX.get();
CURRENT_INDEX.set(dbIndex);
return oldDB;
}
/**
* 切换到主库
*/
@Deprecated
void switchToMain() {
reset();
}
/**
* 切换到主库
*/
void reset() {
CURRENT_INDEX.set(0);
}
}