简体   繁体   English

Spring Boot application.yml 全局设置数据源属性,被忽略

[英]Spring Boot application.yml set datasource property globally, being ignored

I have a Spring Boot 1.5 app which is configured with an application.yml file.我有一个配置了application.yml文件的 Spring Boot 1.5 应用application.yml I need to manage the connection pool which is default - Tomcat.我需要管理默认的连接池 - Tomcat。

The problem is that the application.yml has a datasources property for several datasources.问题是application.yml有多个数据源的datasources属性。 My global datasource.max-active=10 (UPDATE: datasource.tomcat.max-active=10 is ignored too) is being completely ignored (I created a test to see what datasource is being injected, and default maxActive in them is set to 100).我的全局datasource.max-active=10 (更新: datasource.tomcat.max-active=10也被忽略)被完全忽略(我创建了一个测试来查看正在注入的数据源,并且它们中的默认maxActive设置为100)。 I have to add it to every datasource separately to make the pool work the way I need it to.我必须将它分别添加到每个数据源,以使池按照我需要的方式工作。

The application.yml looks like this (this is only part of it), and it creates datasource with maxActive=10 , but there is bunch of repetitions: application.yml看起来像这样(这只是其中的一部分),它使用maxActive=10创建数据源,但是有一堆重复:

spring:
  .... #bunch of stuff, deleted for simplicity
  datasource: #Added by me, ignored by Spring
    max-active: 10 #Added by me, ignored by Spring
datasources:
  datasource1:
    url: jdbc:mysql://url:port1
    max-active: 10 #Added by me, works
  datasource2:
    url: jdbc:mysql://url:port2
    max-active: 10 #Added by me, works

Question: what is the correct way to set the max-active property globally to avoid this repetition?问题:全局设置max-active属性以避免这种重复的正确方法是什么? Thanks.谢谢。

When you are using multiple data sources, spring boot does not provide a default data source auto-configuration.当您使用多个数据源时,spring boot 不提供默认的数据源自动配置。 This also means you have to provide connection pool properties for each data source.这也意味着您必须为每个数据源提供连接池属性。 I don't think it's possible to set it globally.我认为不可能在全球范围内设置它。 Instead, you can use the placeholder to set it once and use it everywhere.相反,您可以使用占位符设置一次并在任何地方使用它。

custom:
 max-active: 10 
spring:
datasources:
  datasource1:
    url: jdbc:mysql://url:port1
    max-active: ${custom.max-active}
  datasource2:
    url: jdbc:mysql://url:port2
    max-active: ${custom.max-active}

You can find more info here: How to setup multiple connection pools when multiple datasources are used in Spring Boot?您可以在此处找到更多信息: 在 Spring Boot 中使用多个数据源时如何设置多个连接池?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring启动会忽略application.yml中的数据源URL - Spring boot ignores datasource url in application.yml 在 application.yml 中访问 Maven 属性“developers”(Spring Boot) - Access Maven property “developers” in application.yml (Spring Boot) Spring 引导 2 忽略在 application.yml 中设置的 HikariCP 属性 - Spring Boot 2 is ignoring HikariCP properties set in application.yml spring 启动应用程序.yml 中的环境变量 - Environment variable in spring boot application.yml Spring Boot application.yml 和 @Value 不起作用 - Spring Boot application.yml and @Value is not working 在 spring boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别? - What is the difference between putting a property on application.yml or bootstrap.yml in spring boot? 如何使用 docker run 命令为 Spring Boot 应用程序覆盖 application.yml 中的 spring 属性? - How can you override a spring property in application.yml for a Spring Boot application using the docker run command? 从 application.yml 读取属性时忽略 Spring 配置文件 - Spring profile is ignored when reading properties from application.yml Spring boot application.yml 中的 Spring Kafka SSL 设置 - Spring Kafka SSL setup in Spring boot application.yml Spring Boot - 从 application.yml 和 application.properties 注入 - Spring Boot - inject from application.yml and application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM