In SpringBoot, you can place the configuration file outside the jar package, so that you can easily modify the configuration. No repackaging and deployment required.
The following are several ways to specify the configuration file directory: (sorted by effective priority from high to low)
When starting the application, you can use the --spring.config.location
or -Dspring.config.location
command line parameter to specify the path to the configuration file, for example: ( The / after
config cannot be omitted)
java -jar test.jar --spring.config.location=/opt/app/config/
Supplement: FHS defines /opt as "reserved for the installation of additional application packages." So the /opt
folder is selected here to store the application configuration file.
You can set the SPRING_CONFIG_LOCATION
environment variable to specify the path to the configuration file, for example:
export SPRING_CONFIG_LOCATION=/opt/app/config/ java -jar test.jar
Method 1: Create a file named config# in the application (jar package)
startup command execution directory ## folder, and then place the configuration file in that folder.
Method 2: Create a file named config in the directory of the same level as the application (jar package)
folder and place the configuration file in that folder. Add the following lines in your code's configuration file application.properties
or application.yml
:
spring.config.name=application spring.config.location=classpath:/,file:./config/
Directly place the properties or yml
configuration file in the application (jar package) directory at the same level.
config folder, and then put the properties or
yml configuration file.
Method 5:
In the
same level directory of
properties or yml configuration file.
Note: The above methods can be used in combination. For example, you can specify the path to the configuration file in both command line parameters and environment variables. The command line parameters have the highest priority during execution.
2. The priority ordering for the SpringBoot configuration file to take effect:
java -jar
In the command line, pass Setting the
SPRING_CONFIG_LOCATION environment variable to specify the path to the configuration file is secondary priority.
config folder in the same directory as the
java -jar
Note: If the
java -jar command is not executed in the same directory as the project jar package,
config# in the same directory as the project jar package ## The folder is not valid.
The properties
or yml
file in the project jar package specifies the
Put the project jar package directly under the same level.
properties
The file is the fifth priority. Directly put the configuration file into the directory at the same level as the jar package.
sibling config
folder inside the project is the sixth priority. Create a config folder under the classpath, and then put Put the configuration file in.
classpath
within the project is placed at the same level. properties
files have the lowest priority and are placed directly under
SpringBoot defaults to reading a
config/application.properties
file in the same directory as the java -jar
command first.
file created under the src/main/resources
folder within the project has the lowest priority.
The above is the detailed content of How does SpringBoot hang the configuration file outside the jar package?. For more information, please follow other related articles on the PHP Chinese website!