Introduction to the

SpringBoot application default scan the classpath:, the classpath: / config file: file: the config/directory of the application * configuration files, from low to high priority.

  • You can use a custom namespring.config.name="custom"
  • You can customize scan directoriesspring.config.location="/Users/tengchong/Desktop/", the scan directory will change to:
    1. classpath:
    2. classpath:/config
    3. file:
    4. file:config/
    5. file:/Users/tengchong/Desktop/

Priority from low to high

The jar package deployment

Jar deployment allows you to specify the directory where the configuration files are located at startup via spring.config.location, and spring.profiles. Active specifies the environment

  • Start the command
java -jar easy-frame.jar --Dspring.config.location=/Users/tengchong/Desktop/ --spring.profiles.active=prod
Copy the code

You can see the information about the scan configuration file in the log output at startup

13:09:51 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Activated activeProfiles prod
13:09:51 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Profiles already activated, '[dev]' will not be applied
13:09:51 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Loaded config file 'jar:file:/Users/tengchong/Desktop/easy-frame.jar! /BOOT-INF/classes! /application.yml' (classpath:/application.yml)
13:09:51 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Loaded config file 'file:./application-prod.yml' (file:./application-prod.yml) for profile prod
13:09:51 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Loaded config file 'jar:file:/Users/tengchong/Desktop/easy-frame.jar! /BOOT-INF/classes! /application-prod.yml' (classpath:/application-prod.yml) for profile prod
Copy the code

Three configuration files are loaded in logs (the loading sequence has nothing to do with the priority) :

  1. classes! /application.ymlCommon configuration in jar
  2. file:./application-prod.yml /Users/tengchong/Desktop/The PROD environment in the directory is configured accordingly
  3. classes! /application-prod.ymlThe PROD environment in the JAR is configured accordinglyclasses! /application-prod.ymlThe configuration of duplicate parts will befile:./application-prod.ymlCovered withapplication.ymlConfiguration of thespring.profiles.active=devSo here’s the hintProfiles already activated, '[dev]' will not be appliedThis can be ignored because the parameters inspring.profiles.activeThe priority is higher than this value.

War file deployment

You can customize parameters by setting JAVA_OPTS for war package deployment. The following uses apache-tomcat-8.5.40 as an example

  • Modify thebin/catalina.sh
vi ./bin/catalina.sh
Copy the code
  • Add arguments at about 250 lines
JAVA_OPTS="$JAVA_OPTS -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dspring.config.location=/Users/tengchong/Desktop/ -Dspring.profiles.active=prod"
Copy the code
  • Save the Settings and start Tomcat
./bin/startup.sh
Copy the code

The following information is displayed in the log

13:23:53 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Activated activeProfiles prod
13:23:53 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Loaded config file 'file:/Users/tengchong/Desktop/application-prod.yml' (file:/Users/tengchong/Desktop/application-prod.yml) for profile prod
Copy the code

Found no scanning the classpath: application startup, the classpath: / config file: file: the config/directory only scanned the spring. The config. The location specified directory, For reasons unknown temporarily will be applied in the application. The yml also placed/Users/tengchong/Desktop/can successfully started

Such as knows no scanning under tomcat ` classpath:, the classpath: / config file: file: the config/directory ` reason please let us know, thanks

13:26:51 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Activated activeProfiles prod
13:26:51 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Profiles already activated, '[dev]' will not be applied
13:26:51 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Loaded config file 'file:/Users/tengchong/Desktop/application.yml' (file:/Users/tengchong/Desktop/application.yml)
13:26:51 demo [main] DEBUG o.s.b.c.c.ConfigFileApplicationListener - Loaded config file 'file:/Users/tengchong/Desktop/application-prod.yml' (file:/Users/tengchong/Desktop/application-prod.yml) for profile prod
Copy the code

Application. Yml is configured with Spring.profiles. Active =dev. ‘[dev] will not be applied here can ignore or delete/Users/tengchong/Desktop/application. The spring in the yml. Profiles. The active = dev, Because spring.profiles. Active in JAVA_OPTS has a higher priority than this one.

If knows no scanning the classpath: under tomcat, the classpath: / config file: file: please let us know the reason for the config/directory, thank you

Please point out any mistakes, thank you