Reputation: 353
I have 2 DAO - the first works with @Bean DataSource + JDBC. Configuration is the following:
@Bean("dataSource")
@Singleton
public DataSource getDataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName("...");
basicDataSource.setUrl("...");
basicDataSource.setUsername(...);
basicDataSource.setPassword(...);
...
return basicDataSource;
}
The second works with entityManager. application.properties configuration is the following:
spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
...
When I starts my Spring Boot Application and spring initializes my beans, I use the second DAO to get some information from database.
I am using second DAO -> entityManager in this case.
I expects that entityManager uses configuration from application.properties.
Indeed, entityManager uses configration from bean DataSource.
How does It work?
p.s. database properties in application.properties look like used.
Actually I think that I should use one ConnectionPool for my application.
I can configure DataSource as @Bean and provide entityManager and jdbcTemplate with It.
Should I choose another solution? Or Is this idea quite suitable?
Upvotes: 0
Views: 1378
Reputation: 26076
It's because of the importance. @Configuration
has higher precedence than application.properties. First spring-boot searches for @Bean
definition, if it's not found, then it checks application.properties. Generally those definitions are equivalent.
Upvotes: 2