Search in sources :

Example 1 with DataSource

use of org.axe.annotation.persistence.DataSource in project Axe by DongyuCai.

the class DataSourceHelper method init.

@Override
public void init() {
    synchronized (this) {
        Set<Class<?>> dataSourceFactoryClassSet = ClassHelper.getClassSetBySuper(BaseDataSource.class);
        Map<String, Class<?>> dataSourceFactoryClassMap = new HashMap<>();
        for (Class<?> dataSourceFactoryClass : dataSourceFactoryClassSet) {
            if (dataSourceFactoryClass.isAnnotationPresent(DataSource.class)) {
                String dataSourceName = dataSourceFactoryClass.getAnnotation(DataSource.class).value();
                if (StringUtil.isEmpty(dataSourceName))
                    throw new RuntimeException("find the empty name DataSource:" + dataSourceFactoryClass);
                if (dataSourceFactoryClassMap.containsKey(dataSourceName))
                    throw new RuntimeException("find the same name DataSource:" + DATA_SOURCE.get(dataSourceName).getClass() + "===" + dataSourceFactoryClass);
                dataSourceFactoryClassMap.put(dataSourceName, dataSourceFactoryClass);
            }
        }
        DATA_SOURCE = new HashMap<>();
        String jdbcDatasource = ConfigHelper.getJdbcDatasource();
        String[] split = jdbcDatasource.split(",");
        for (String dataSourceNameConfig : split) {
            //默认数据源取得是配置数据源列表中的第一个
            if (StringUtil.isNotEmpty(dataSourceNameConfig)) {
                if (DEFAULT_DATASOURCE_NAME == null) {
                    DEFAULT_DATASOURCE_NAME = dataSourceNameConfig;
                }
                if (dataSourceFactoryClassMap.containsKey(dataSourceNameConfig)) {
                    BaseDataSource dataSource = ReflectionUtil.newInstance(dataSourceFactoryClassMap.get(dataSourceNameConfig));
                    DATA_SOURCE.put(dataSourceNameConfig, dataSource);
                }
            }
        }
    }
}
Also used : BaseDataSource(org.axe.interface_.persistence.BaseDataSource) HashMap(java.util.HashMap) BaseDataSource(org.axe.interface_.persistence.BaseDataSource) DataSource(org.axe.annotation.persistence.DataSource)

Aggregations

HashMap (java.util.HashMap)1 DataSource (org.axe.annotation.persistence.DataSource)1 BaseDataSource (org.axe.interface_.persistence.BaseDataSource)1