Spring Boot 在以编程方式配置数据源时不选择 spring.datasource.tomcat.* ? [英] Spring Boot doesn't pick spring.datasource.tomcat.* while programmatically configuring datasources?

查看:43
本文介绍了Spring Boot 在以编程方式配置数据源时不选择 spring.datasource.tomcat.* ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在配置两个数据源,并尝试设置池属性,根据文档,我应该使用 spring.datasource.tomcat.*,这似乎不适用于我正在执行的配置.我做错了什么?或者我错过了什么?

I am configuring two data sources, and trying to set pooling properties and as per docs I should use spring.datasource.tomcat.*, that doesn't seem to work with the configuration that I am doing. What am I doing wrong? or what am I missing?

我的 application.properties :

My application.properties :

spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validationQuery=SELECT 1 
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username
spring.datasource.password

spring.read.datasource.tomcat.test-on-borrow=true
spring.read.datasource.tomcat.validationQuery=SELECT 1 
spring.read.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.read.datasource.username
spring.read.datasource.password

下面是我的配置类:我有一个类似的读取数据源(用于不同的存储库/实体)

Below is my configuration class : I have a similar one for read data source (for different repo/entities)

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager",
basePackages = "com.test.feature.repo.internal")
public class DataSourceConfig {

    @Primary
    @Bean("dataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name="entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, 
            @Qualifier("dataSource")DataSource dataSource){
        return builder.dataSource(dataSource).packages("com.test.feature.entity.internal").persistenceUnit("defaultPersistenceUnit").build();
    }

    @Primary
    @Bean(name="transactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory){
        return new JpaTransactionManager(entityManagerFactory);
    }
}

如果我尝试使用 spring.datasource.test-on-borrow=true,那么这有效.

If I try to use spring.datasource.test-on-borrow=true, then this works.

我真的很想知道为什么 .tomcat.* 样式不起作用?我该怎么做才能让它发挥作用?

I really want to know why the .tomcat.* style doesn't work? And what I can I do to make that work?

即使有人将我重定向到一些有用的阅读材料来理解这一点,我也会很高兴.:)

Even if someone redirects me to some helpful reading material for understanding this, I will be glad. :)

推荐答案

该文档是关于自动配置的,而您并未使用它.如果您正在编写自定义代码来设置 DataSource,您还需要负责配置的绑定.

That documentation is about the auto-configuration and you're not using it. If you are writing custom code to setup the DataSource, you are in charge of the binding of the configuration as well.

你上面的代码有一个@ConfigurationPropeties("spring.datasource").如果您删除它,您自己的代码中将不会考虑任何 spring.datasource.* 属性.

Your code above has a @ConfigurationPropeties("spring.datasource"). If you remove that, none of the spring.datasource.* properties would be taken into account in your own code.

文档的这一部分 解释了基本属性(spring.datasource)和数据源绑定(spring.datasource.xyz.*)之间的区别.

This section of the doc explains the difference between basic properties (spring.datasource) and data source binding (spring.datasource.xyz.*).

无论如何,如果您自己创建 DataSource(为什么?),那么请使用单独的命名空间.重用 spring.datasource 命名空间非常令人困惑,因为用户期望自动配置提供的功能将得到尊重.他们不会,因为您正在编写自己的配置.

Regardless, if you are creating the DataSource yourself (why?) then use a separate namespace. Reusing the spring.datasource namespace is quite confusing as a user is expecting that the features the auto-configuration provides will be honoured. And they won't since you're writing your own config.

这篇关于Spring Boot 在以编程方式配置数据源时不选择 spring.datasource.tomcat.* ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆