com.wuzh.commons.mybatis.datasource.DruidDataSourceConfig Maven / Gradle / Ivy
/*
* Copyright 2015-2022 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 com.wuzh.commons.mybatis.datasource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.wuzh.commons.mybatis.constants.DataSourceConstants;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
/**
* 类DruidDataSourceConfig的实现描述:Alibaba Druid 数据源配置
*
* @author 伍章红 2020-12-29 17:13:55
* @version v2.3.6
* @since JDK 1.8
*/
@Configuration
@ConditionalOnClass(DruidDataSource.class)
public class DruidDataSourceConfig {
@Primary
@Bean(name = DataSourceConstants.BEAN_NAME_WRITE, initMethod = "init", destroyMethod = "close")
@ConfigurationProperties(DataSourceConstants.PROPERTIES_PREFIX_WRITE)
@ConditionalOnMissingBean(name = DataSourceConstants.BEAN_NAME_WRITE)
public DataSource writeDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = DataSourceConstants.BEAN_NAME_READ, initMethod = "init", destroyMethod = "close")
@ConfigurationProperties(DataSourceConstants.PROPERTIES_PREFIX_READ)
@ConditionalOnMissingBean(name = DataSourceConstants.BEAN_NAME_READ)
public DataSource readDataSource() {
return DruidDataSourceBuilder.create().build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy