简体   繁体   中英

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:

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

My application-prod.yml looks like:

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

  • 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. Therefore to solve this issue i created another config application-customization.yml and link it in prod config via include (see in example above). 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 . 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.

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. In your case, you probably want -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.

Way 2: put @PropertySource("file:/some/folder/application.properties") on a class annotated with @Configuration (note that @SpringBootApplication is meta-annotated with @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; two, there are some properties which you cannot set with an @PropertySource file (because the file is read too late in the startup process). 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; it might require faffing about with app-specific web.xml files. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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