Using 2 beans of the same type: javax.sql.DataSource in Spring

前端 未结 4 1030
南笙
南笙 2021-01-01 19:45

I am developing a Spring Boot based application in which I would like to create 2 beans: One will point to \'Oracle\' database; the other will point to Hive. I\'ve declar

4条回答
  •  旧时难觅i
    2021-01-01 20:22

    Give different names to your beans when using @Bean:

    @Bean(name="bonecpDS")
    public BoneCPDataSource metadataDataSource() {
        //...
    }
    
    @Bean(name="hiveDS")
    public BasicDataSource hiveDataSource() {
        //...
    }
    

    Then, when injecting the bean, use @Qualifier and specify the name of the bean:

    @Component
    public class FooComponent {
        @Autowired
        @Qualifier("bonecpDS")
        DataSource boneCPDataSource;
    }
    

提交回复
热议问题