You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the DataSource is refresh scope(provided by spring-cloud), I cannot inject the JdbcTemplate to my component. Probably, JdbcTemplateConfiguration(auto-configure) not work.
@RestControllerstaticclassSettingsController {
privatefinalJdbcOperationsoperations;
SettingsController(JdbcOperationsoperations) {
this.operations = operations;
}
@GetMapping("/settings")
publicList<Map<String, Object>> getSettings() {
returnoperations.queryForList("select * from settings order by key");
}
}
log
...
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-06-20 19:23:27.503 ERROR 35308 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.demo.RefreshableDsDemoApplication$SettingsController required a bean of type 'org.springframework.jdbc.core.JdbcOperations' that could not be found.
...
Thanks for the sample. The problem is that using @RefreshScope results in two DataSource beans in the context, dataSource and scopedTarget.dataSource. This causes the @ConditionalOnSingleCandidate auto-configuration of JdbcTemplate to back off:
JdbcTemplateAutoConfiguration:
Did not match:
- @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) did not find a primary bean from beans 'scopedTarget.dataSource', 'dataSource' (OnBeanCondition)
I think we need to ignore scoped targets when searching for a single candidate.
wilkinsona
changed the title
Cannot inject JdbcTemplate when DataSource is refresh scope
@ConditionalOnSingleCandidate does not match when there is a single candidate that is a scoped bean
Jul 2, 2020
When the
DataSource
is refresh scope(provided by spring-cloud), I cannot inject theJdbcTemplate
to my component. Probably,JdbcTemplateConfiguration
(auto-configure) not work.Reproduce project
How to reproduce
Please run the
RefreshableDsDemoApplicationTests
.The text was updated successfully, but these errors were encountered: