简体   繁体   English

如何在 Spring Boot 中设置外部配置的路径

[英]How to set path of external config in Spring Boot

My question is another question about configuration in spring boot (v 2.2.x) but I have one significant difference in my config structure in comparison to configuration in existing posts on SO about configs (ie spring boot external config ) I have following application configurations:我的问题是关于 spring boot (v 2.2.x) 中配置的另一个问题,与关于 configs (即spring boot external config ) 的现有帖子中的配置相比,我的配置结构有一个显着差异我有以下应用程序配置:

--src\
     --main\
            --resources\
                -- application.yml
                -- application-local.yml
                -- application-dev.yml
                -- application-prod.yml

My application-prod.yml looks like:我的 application-prod.yml 看起来像:

spring:
  profiles: 
    active: prod
    include:
      customization
  
logging:
  level:
    root: INFO
    org.springframework: INFO
    org.hibernate.SQL: INFO
    org.hibernate.type: INFO
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss} - %msg%n'
    file: '%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n'
  file: ./logs/goals-service.log

My prod config have to use (this is a business requirement) external configuration with setting of我的 prod 配置必须使用(这是业务要求)外部配置和设置

  • db connection,数据库连接,
  • security settings安全设定
  • endpoints and credentials of other services.其他服务的端点和凭据。

I would like to use standard approach and ability of spring to automatically config beans , i do not want to manually load some property file and configure all beans manually.我想使用标准方法和 spring 自动配置 bean 的能力,我不想手动加载一些属性文件并手动配置所有 bean。 Therefore to solve this issue i created another config application-customization.yml and link it in prod config via include (see in example above).因此,为了解决这个问题,我创建了另一个配置 application-customization.yml 并通过包含将其链接到 prod 配置中(参见上面的示例)。 And here i faced a problem: i am unable to select path to application-customization.yml via command line argument (-Dspring.config.location or any it variations) , but i could load customization settings when i placed my external config in directory that is used to start app (this is a behaviour of spring to search configs), and app in this case works fine .在这里我遇到了一个问题:我无法通过命令行参数(-Dspring.config.location 或任何它的变体)选择 application-customization.yml 的路径,但是当我将外部配置放在目录中时我可以加载自定义设置用于启动应用程序(这是 spring 搜索配置的行为),并且在这种情况下应用程序工作正常 I would like to pass path of where app should search my application-customization.yml, and one more thing i can'not use symlinks from to link from actual config location to ./application-customization.yml.我想传递应用程序应该在哪里搜索我的 application-customization.yml 的路径,还有一件事我不能使用符号链接从实际配置位置链接到 ./application-customization.yml。

Remove that "include" from your application-dev, because I have no idea how it will interact with anything that I'm going to say.从您的应用程序开发中删除该“包含”,因为我不知道它将如何与我要说的任何内容进行交互。 Then there are (at least) two simple ways to do what you want.然后(至少)有两种简单的方法可以做你想做的事。 (There are also some complicated ways -- overkill for your situation, and indeed most situations -- like overriding this or defining one of these .) (也有一些复杂的方式-大材小用您的情况,确实大多数情况下-像重写这个或定义的一个这些。)

Way 1: pass -Dspring.config.location=X to the JVM, where X is a comma-separated list of locations containing files Spring should read.方式 1:将-Dspring.config.location=X传递给 JVM,其中 X 是包含 Spring 应读取的文件的位置的逗号分隔列表。 In your case, you probably want -Dspring.config.location=file:/some/folder/,classpath:/ ;在您的情况下,您可能需要-Dspring.config.location=file:/some/folder/,classpath:/ ; the first location will ensure you fulfill your business requirement and the second will ensure the app also reads the application-dev.yml inside its own jar.第一个位置将确保您满足您的业务需求,第二个位置将确保应用程序也读取其自己的 jar 中的 application-dev.yml。

Way 2: put @PropertySource("file:/some/folder/application.properties") on a class annotated with @Configuration (note that @SpringBootApplication is meta-annotated with @Configuration ).方式2:将@PropertySource("file:/some/folder/application.properties")放在用@Configuration注释的类上(注意@SpringBootApplication是用@Configuration元注释的)。 This has two issues: one, the file in the @PropertySource is read last and the properties therein do not override properties which were read earlier;这有两个问题:一,最后读取@PropertySource的文件,并且其中的属性不会覆盖之前读取的属性; two, there are some properties which you cannot set with an @PropertySource file (because the file is read too late in the startup process).二、有些属性不能@PropertySource文件设置(因为在启动过程中读取文件太晚)。 I don't think either issue will bother you, but I wanted to note them for other readers.我不认为任何一个问题都会打扰你,但我想为其他读者注意它们。

Note on Tomcat (and presumably other containers): using way 1 there is a bit complicated, since spring.config.location is a system property;注意 Tomcat(可能还有其他容器):使用方式 1 有点复杂,因为spring.config.location是系统属性; it might require faffing about with app-specific web.xml files.它可能需要处理特定于应用程序的 web.xml 文件。 EDIT 2020-09-04: Or you could do this (specifically the third snippet in that code, which I repeat, modifying the property name and value, in case that answer ever vanishes:编辑2020年9月4日:或者你可以做这个(特别是在该代码,这是我重复第三代码段,修改属性名称和值,如果这个问题的答案永远消失:

public class ServletInitializer extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(MyApplication.class).properties("spring.config.location: /my/config1/,/my/config2/");
        }
}

)... which is better. )... 哪个更好。

This answer has mostly been a regurgitation of the excellent official documentation on this and related questions.这个答案主要是对有关此问题和相关问题的优秀官方文档的反刍。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM