lodsve.mybatis.datasource.dynamic.DynamicDataSource Maven / Gradle / Ivy
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package lodsve.mybatis.datasource.dynamic;
import lodsve.core.utils.StringUtils;
import lodsve.mybatis.exception.MyBatisException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 动态数据源.
*
* @author sunhao(sunhao.java @ gmail.com)
* @version 1.0 2017/12/14 下午6:23
*/
public class DynamicDataSource extends AbstractRoutingDataSource implements BeanFactoryAware {
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);
private List dataSourceBeans;
private String defaultDataSource;
private BeanFactory beanFactory;
public DynamicDataSource(List dataSourceBeans, String defaultDataSource) {
this.dataSourceBeans = dataSourceBeans;
this.defaultDataSource = defaultDataSource;
}
@Override
protected Object determineCurrentLookupKey() {
return DataSourceHolder.getInstance().get();
}
@Override
public void afterPropertiesSet() {
if (this.dataSourceBeans == null) {
throw new IllegalArgumentException("Property 'dataSourceBeans' is required");
}
Map