springboot druid 加载外部数据库连接

直接上代码,就三个类,在不使用外部配置的情况下也能读取默认的配置文件

import com.alibaba.druid.filter.config.ConfigFilter;
import com.alibaba.druid.filter.encoding.EncodingConvertFilter;
import com.alibaba.druid.filter.logging.CommonsLogFilter;
import com.alibaba.druid.filter.logging.Log4j2Filter;
import com.alibaba.druid.filter.logging.Log4jFilter;
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.wall.WallFilter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author linxh
 * @description 外部数据源连接转换器
 * @date 2021/11/18
 */
public class ExtralDruidDataSourceWrapper extends DruidDataSource implements InitializingBean {
        
    @Override
    public void afterPropertiesSet() throws Exception {
        //这个判断条件没啥用,看自身使用情况,ExtralMysqlConnectionInfo这个类是自己写用来读取外部数据源信息的
        if (ExtralMysqlConnectionInfo.loadExtralSwitch()) {
            super.setUrl(ExtralMysqlConnectionInfo.getMysqlUrl());
            super.setUsername(ExtralMysqlConnectionInfo.getMysqlUsername());
            super.setPassword(ExtralMysqlConnectionInfo.getMysqlPassword());
       super.setDriverClassName(ExtralMysqlConnectionInfo.getMysqlDriverClassName());
        }
    }

    @Autowired(
            required = false
    )
    public void addStatFilter(StatFilter statFilter) {
        super.filters.add(statFilter);
    }

    @Autowired(
            required = false
    )
    public void addConfigFilter(ConfigFilter configFilter) {
        super.filters.add(configFilter);
    }

    @Autowired(
            required = false
    )
    public void addEncodingConvertFilter(EncodingConvertFilter encodingConvertFilter) {
        super.filters.add(encodingConvertFilter);
    }

    @Autowired(
            required = false
    )
    public void addSlf4jLogFilter(Slf4jLogFilter slf4jLogFilter) {
        super.filters.add(slf4jLogFilter);
    }

    @Autowired(
            required = false
    )
    public void addLog4jFilter(Log4jFilter log4jFilter) {
        super.filters.add(log4jFilter);
    }

    @Autowired(
            required = false
    )
    public void addLog4j2Filter(Log4j2Filter log4j2Filter) {
        super.filters.add(log4j2Filter);
    }

    @Autowired(
            required = false
    )
    public void addCommonsLogFilter(CommonsLogFilter commonsLogFilter) {
        super.filters.add(commonsLogFilter);
    }

    @Autowired(
            required = false
    )
    public void addWallFilter(WallFilter wallFilter) {
        super.filters.add(wallFilter);
    }
}
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

/**
 * @author linxh
 * @description 创建外部数据源
 * @date 2021/11/18
 */
public class ExtralDruidDataSourceBuilder extends DruidDataSourceBuilder {

    public static ExtralDruidDataSourceBuilder create() {
        return new ExtralDruidDataSourceBuilder();
    }

    /**
     * For build multiple DruidDataSource, detail see document.
     */
    public DruidDataSource build() {
        return new ExtralDruidDataSourceWrapper();
    }
}

 

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 配置多数据源
 *
 * @author 
 */
@Configuration
public class DataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.druid")
    public DataSource dataSource(){
        return ExtralDruidDataSourceBuilder.create().build();
    }

}

 

上一篇:15-nacos-快速入门


下一篇:alibaba druid数据库连接池详解