I\'m trying to Autowire a database by
@Autowired
private DataSource dataSource;
I have one datasource in my application.yml
I solved by adding qualifier above the property:
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
Try this it worked for me, use @Primary like this
@Primary
@Bean(name ="prodDataSource")
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "prodJdbc")
public JdbcTemplate prodJdbcTemplate(@Qualifier("prodDataSource") DataSource prodDataSource){
return new JdbcTemplate(prodJdbcTemplate);
}
@Bean(name = "devDataSource")
@ConfigurationProperties(prefix = "spring.datasource.dev")
public DataSource devDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "devJdbc")
public JdbcTemplate devJdbcTemplate(@Qualifier("devDataSource") DataSource devDataSource) {
return new JdbcTemplate(devDataSource);
}
And then use @autowire like this
@Autowired
@Qualifier("prodJdbc")
private JdbcTemplate prodJdbcTemplate;
I hope this can help you or someone else :)
Please note that Spring Boot autoconfigured beans are not supported 100% yet, see https://youtrack.jetbrains.com/issue/IDEA-139669 for progress and possible workaorunds.