Spring Boot how to set spring.config.location in application.properties for yaml

34,639

Solution 1

Since you are using spring boot 1.4.2, everything is simple. You just need specify additional argument for your application, such as java -jar name-of-application-version.jar --spring.config.location=file:///C:/properties/application.yml. A more common method to achieve this is defining additional option for JVM like java -Dspring.config.location=file:///d:/private-bootstrap.yml -jar name-of-application-version.jar. Both of these two methods sould work, cause they work fine on my desktop.

Solution 2

You can use the spring.config.location property while invoking your Spring Boot application. Below is an example wherein you have 4 property files of custom name in custom locations.

java -Dspring.config.location=file:/gfs/config/post/post.properties,/gfs/config/post/post_pwd.properties,/myDbConfig/config/db-config.properties,/myserver/config/db-passwd.properties  -jar myapp-1.0-SNAPSHOT.jar

Within your code, you can access them by:

@Component
public class AppProperties {

    @Value("${post.user.name}")
    private String username;
}
Share:
34,639
DANIEL K
Author by

DANIEL K

Updated on September 05, 2020

Comments

  • DANIEL K
    DANIEL K over 3 years

    I use spring boot 1.4.2 and now on try to set properties.

    I have four cases about using .properties and .yml to external(inside resources) and local file system (outside project folder).

    The .properties external (inside resource folder) works well both @Value(${property.name}) and @PropertySource last one use value attribute can load from the file system like below.

    @Component
    @PropertySource(value="file:C:\\properties\\application-dev.properties")
    @PropertySource(value="file:C:\\properties\\application-test.properties")
    public class ExternalProperties {
       // ...
    }
    

    But .yml isn't that works well under resource folder when the file name 'application.yml'

    The .yml can be loaded by @ConfigurationProperties and need 'propdeps-plugin'

    my .yml file

    environments:
        dev:
            url: http://dev.bar.com
            name: Developer Setup
        prod:
            url: http://foo.bar.com
            name: My Cool App
    

    This code works well

    @Component
    // external(inside resources)
    @ConfigurationProperties(prefix="environments")
    // outside project folder
    @ConfigurationProperties(locations={"file:C:\\properties\\application.yml"}, prefix = "environments")
    public class YamlProperties {
    
        private Map<String, String> dev = new HashMap<>();
        private Map<String, String> prod = new HashMap<>();
    
        public Map<String, String> getDev() {
            return this.dev;
        }
    
        public Map<String, String> getProd() {
            return this.prod;
        }
    }
    

    That code has problem of deprecated location attribute (I read many articles but can't figure out clearly) so I need find API doc and find 'ConfigFileApplicationListener' from this section.

    # SPRING CONFIG - using environment property only (ConfigFileApplicationListener)
    spring.config.location= # Config file locations.
    spring.config.name=application # Config file name.
    

    So write above property on application.properties like this.

    spring.config.location=file:C:\\properties\\application.yml
    spring.config.name=application
    

    and reloading the .yml property (I didn't try excute jar. I use the war and test through Controller)

    @Component
    @ConfigurationProperties(prefix="environments")
    public class YamlProperties {
    
         private Map<String, String> dev = new HashMap<>();
         private Map<String, String> prod = new HashMap<>();
    
         public Map<String, String> getDev() {
             return this.dev;
         }
    
         public Map<String, String> getProd() {
             return this.prod;
         }
    }
    

    This code didn't load the .yml from local file system C: drive but when add application.yml file in the resource folder then works well.

    So how to set the spring.config.location for load .yml

    And I want to know about why the location attribute works yet although deprecated since 1.4 ver.

    and wondering how to use the ConfigFileApplicationListener I can't follow up the code it's hard to understand give some tip~!

    EDIT:

    I miss understood about this that when the war start again and make context again using the local file system properties. This is not the collectly linked it but remain for future step.

    The tomcat restart than war deploy the new one so I have the file on local system it contain properties if I change this file's data that could be udated properties context when tomcat restart.

    Why I keep try this work that I use public github account and protect connect db info something else to. I get this stuff and go on next issue like git-encrpt, spring-cloude, nginx, docker. Thank you for any help really helpful.

    • Gemini Keith
      Gemini Keith over 7 years
      You'd better try bootstrap.yml configuration and specify whatever configuration you want there. Such as spring.config.location. If you are using bootstrap.yml, please keep in mind to add dependency of org.springframework.cloud:spring-cloud-context. For simplicity, spring-cloud-context will load bootstrap.yml automatically and configurations there could be loaded automatically too. Another way to do this is specifying from command line arguments such as -Dspring.config.location= or just --spring.config.location=. Arguments in former format should appear before -jar option.
    • Gemini Keith
      Gemini Keith over 7 years
      Acutally, yml extension is normally used and well known in spring framework not yaml. And the siplest way to achieve this is naming your bootstrap configuration file as bootstrap.yml in your resources folder. Example https://github.com/soiff-spring/spring-boot-example.git could do some help.
    • DANIEL K
      DANIEL K over 7 years
      thanks for spring cloud info I'll try next step and I update name yaml to yml but still not works. thank you for new info @GeminiKeith
    • Gemini Keith
      Gemini Keith over 7 years
      Since you are using spring boot 1.4.2, everything is simple. You just need specify additional argument for your application, such as java -jar name-of-application-version.jar --spring.config.location=file:///C:/properties/application.y‌​ml. A more common method to achieve this is defining additional option for JVM like java -Dspring.config.location=file:///d:/private-bootstrap.yml -jar name-of-application-version.jar. Both of these two methods sould work, cause they work fine on my desktop.
  • DANIEL K
    DANIEL K over 7 years
    Thank you for your help @GeminiKeith How about war situation? Does it works fine on real deploy to tomcat? Actually I use public github also I knew any other good solution here but try first simple ways. After build war file it contain the bean that has the property info on local file system. How about jar file save the info that inject from command line? I just understood the -jar that used to development situation.
  • DANIEL K
    DANIEL K over 7 years
    I have some miss understood my comment it was refreshed properties context info when tomcat restart. so the jar, war have no save permanently the context info. I want make clear what I mention thanks. @GeminiKeith
  • Gemini Keith
    Gemini Keith over 7 years
    It's actually work for a war to be deployed under tomcat. However, if you want to persistent your configuration and make it available next startup, you need to do much more work. And it's not as simple as you think, you'd better post your requirement as another question to discuss. A good solution is spring cloud config. In that way, you do not need specify so much settings in arguments instead of specifying them in spring cloud config.