[英]How does JPA entityManager choose Connection Pool in Spring Boot Application?
I have 2 DAO - the first works with @Bean DataSource + JDBC .我有 2 个DAO - 第一个与 @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 .第二个与entityManager 一起使用。 application.properties configuration is the following: application.properties配置如下:
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 .当我启动我的Spring Boot 应用程序并且 spring 初始化我的bean 时,我使用第二个DAO 从数据库中获取一些信息。
I am using second DAO -> entityManager in this case.在这种情况下,我使用第二个DAO -> entityManager 。
I expects that entityManager uses configuration from application.properties .我希望entityManager使用application.properties 中的配置。
Indeed, entityManager uses configration from bean DataSource .实际上, entityManager使用来自 bean DataSource 的配置。
How does It work?它是如何工作的?
ps database properties in application.properties look like used. application.properties 中的ps数据库属性看起来像使用过。
Actually I think that I should use one ConnectionPool for my application.实际上,我认为我应该为我的应用程序使用一个ConnectionPool 。
I can configure DataSource as @Bean and provide entityManager and jdbcTemplate with It.我可以将DataSource配置为 @Bean 并为entityManager和jdbcTemplate提供它。
Should I choose another solution?我应该选择其他解决方案吗? Or Is this idea quite suitable?或者这个想法很合适吗?
It's because of the importance.这是因为重要性。 @Configuration
has higher precedence than application.properties. @Configuration
优先级高于 application.properties。 First spring-boot searches for @Bean
definition, if it's not found, then it checks application.properties. spring-boot 首先搜索@Bean
定义,如果没有找到,则检查application.properties。 Generally those definitions are equivalent.通常,这些定义是等效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.