All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cattleframework.db.datasource.BasicDataSourceAutoConfiguration Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cattleframework.db.datasource;

import javax.sql.DataSource;

import org.cattleframework.aop.utils.ReflectionsFactory;
import org.cattleframework.aop.utils.SimpleReflectUtils;
import org.cattleframework.db.datasource.handler.DataSourceHandler;
import org.cattleframework.db.datasource.reflect.DataSourceProxy;
import org.cattleframework.exception.CattleException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * 基本数据源自动配置
 * 
 * @author orange
 *
 */
@ConditionalOnProperty(prefix = "cattle.db.shardingsphere", name = "enabled", havingValue = "false", matchIfMissing = true)
@EnableConfigurationProperties(DataSourceProperties.class)
public class BasicDataSourceAutoConfiguration {

    @Bean(name = DataSourceConstants.DATA_SOURCE_BEAN_NAME)
    public DataSource dataSource(DataSourceProperties dataSourceProperties) {
	Class dataSourceHandlerClass = ReflectionsFactory
		.getSubTypesOfByLoadLevel(DataSourceHandler.class);
	if (dataSourceHandlerClass == null) {
	    throw new CattleException("数据源处理类为空");
	}
	DataSourceHandler dataSourceHandler = SimpleReflectUtils.instance(dataSourceHandlerClass);
	DataSource dataSource = dataSourceHandler.getDataSource(dataSourceProperties);
	if (dataSource == null) {
	    throw new CattleException("数据源为空");
	}
	if (dataSourceProperties.isOutputSql()) {
	    return DataSourceProxy.getDataSourceProxy(dataSource);
	}
	return dataSource;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy