Druid多数据源

项目中使用多数据源,共用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 ();
    }

你可能感兴趣的:(Druid多数据源)