欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Druid多数据源

程序员文章站 2022-07-15 10:13:47
...

项目中使用多数据源,共用druid常用配置,重写username,password,url,connectionProperties

@ConfigurationProperties("spring.datasource.druid")
public class ADruidDataSource extends DruidDataSource implements InitializingBean

{
    @Autowired
    private Environment env;

    public ADruidDataSource () {
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        super.setUsername(this.env.getProperty("spring.datasource.druid.A.username"));
        super.setPassword(this.env.getProperty("spring.datasource.druid.A.password"));
        super.setUrl(this.env.getProperty("spring.datasource.druid.A.url"));
        super.setConnectionProperties(this.env.getProperty("spring.datasource.druid.A.connectionProperties"));
    }
}


@ConfigurationProperties("spring.datasource.druid")
public class BDruidDataSource extends DruidDataSource implements InitializingBean

{
    @Autowired
    private Environment env;

    public BDruidDataSource () {
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        super.setUsername(this.env.getProperty("spring.datasource.druid.B.username"));
        super.setPassword(this.env.getProperty("spring.datasource.druid.B.password"));
        super.setUrl(this.env.getProperty("spring.datasource.druid.B.url"));
        super.setConnectionProperties(this.env.getProperty("spring.datasource.druid.B.connectionProperties"));
    }
}
    @Bean
    public DataSource aDruidDataSource () {
        return new ADruidDataSource ();
    }

    @Bean
    public DataSource bDruidDataSource () {
        return new BDruidDataSource ();
    }