spring boot external config

后端 未结 5 1118
清歌不尽
清歌不尽 2020-12-30 04:59

I am trying to load an external properties file into my spring boot app. initially I used @PropertySource in the config class. but now I want to remove this annotation so th

相关标签:
5条回答
  • 2020-12-30 05:42

    After some more googeling I found this Spring Boot and multiple external configuration files indicating that the following is the correct usage:

    java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/myFile.properties
    

    I was under the impression that the --spring.config.location would load other properties files in the directory specified. according to the post at the link I mentioned this is not the case. based on the link if the directory is specified then that is where the application.properties is searched for. but again the documentation here http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html seems to insinuate that the spring boot app will look on the class path first and if available grab the app name to get additional properties files based on that name.

    however once I specified a file name everything worked fine so I guess I was mistaken.

    0 讨论(0)
  • 2020-12-30 05:44

    In command line you should use below property to mention an additional boot configuration file:

    --spring.config.location="file:/path/to/application.properties"
    

    An alternative would be:

    -Dspring.config.location="file:/path/to/application.properties"
    

    Note that characters are lower case and the word separator is a period ..

    Otherwise you can use an environment variable with key you used already:

    • In a *nix system:

      export SPRING_CONFIG_NAME=file:/path/to/application.properties
      
    • In Windows OS:

      set SPRING_CONFIG_NAME=file:/path/to/application.properties
      
    0 讨论(0)
  • 2020-12-30 05:44
    spring.config.name=spring  
    spring.config.location=classpath:/config/
    

    in side the config folder spring.properties file is available, while running the server the this properties file is not loading

    0 讨论(0)
  • 2020-12-30 05:45

    1) Makesure args pass inside of run method

    public class GemFireTestLoaderApplication {
    
    public static void main(String[] args) {
    
            SpringApplication.run(GemFireTestLoaderApplication.class, args);
            }
    }
    

    2) If you have configureed in xml comment or remove first

    <!-- <context:property-placeholder location="classpath:config.properties" />  -->
    
    <!--   <context:property-placeholder location="file:/data/xxx/vaquarkhan/dataLoader/config.properties" /> -->
    

    Following command you can use to pass properties name

    3.1)

    java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties
    

    3.2)

    java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties
    

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

    0 讨论(0)
  • 2020-12-30 05:54

    It might not be a common issue, but I faced it. You also must have an application.properties inside your classpath even when you replace it with --spring.config.name (I had mine in gitignore due to sensitive information).

    0 讨论(0)
提交回复
热议问题