introduction
SpringBoot is a very popular framework in Java development and is known for its simplified configuration and quick launch of applications. During the development process, we often need to load different configuration files according to different environments (such as development environment, test environment, production environment). SpringBoot provides flexible configuration file management mechanisms, allowing us to easily achieve this requirement.
--=/csdn-automatic-triplet/csdn/
Dockerfile
CMD mkdir -p ./logs/gc && java $JAVA_OPTS -jar ./ --=/csdn-automatic-triplet/csdn/ --=$PROFILE $PARAMS
Mount Directory
volumes: - /data/kwan/volumes/csdn/:/csdn-automatic-triplet/csdn/
1. SpringBoot configuration file basics
In SpringBoot, the default configuration files are or, and they are located in the project's src/main/resources directory. These files contain the application configuration information, such as database connections, service ports, etc.
2. Use Dynamically specify the configuration file
SpringBoot allows us to specify the location of the configuration file through properties. This property can be set in a variety of ways, including command line parameters, environment variables, system properties, etc.
2.1 Command line parameters
When starting a SpringBoot application, you can specify the location of the configuration file through command line parameters. For example:
java -jar --=classpath:/another-location/
Hereclasspath:
Indicates that the configuration file is located under the classpath./another-location/
is a directory relative to the classpath.
2.2 Environment variables
We can also set the location of the configuration file as an environment variable, and then read this environment variable when starting the application:
export SPRING_CONFIG_LOCATION=classpath:/another-location/ java -jar
2.3 System Properties
In addition to environment variables, we can also set them through system properties.:
java -=classpath:/another-location/ -jar
3. Search location of configuration file
SpringBoot searches multiple locations in a certain order when looking for configuration files. By default, it searches for configuration files at:
- In the current directory
/config-wrapper
Subdirectory - under classpath
/config-wrapper
Bag - Current directory
- classpath root directory
By setting, we can change this search order and specify where SpringBoot first looks for the configuration file.
4. Activation of configuration files
In addition to specifying the location of the configuration file, we can also activate specific configuration files. SpringBoot allows us to passProperties to activate one or more configuration files.
For example, if we haveand
There are two configuration files, we can specify which configuration file to activate at startup:
java -jar --=prod
5. Use of external configuration files
In a microservice architecture, we may store configuration files in external systems such as configuration centers or distributed configuration services. SpringBoot supports integration with these external systems to obtain configuration information dynamically.
5.1 Spring Cloud Config
Spring Cloud Config is a solution for configuration management of distributed systems. It allows us to store configuration information on a remote server and refresh the configuration dynamically.
5.2 Using Config Server
Through Spring Cloud Config Server, we can create a configuration server that provides the HTTP interface for configuration information. SpringBoot applications can dynamically obtain configuration information through this interface.
6. Dynamic refresh of configuration files
SpringBoot supports dynamic refresh of configuration files, which means that without restarting the application, we can change the configuration files and take effect immediately.
6.1 Using @RefreshScope
We can use it on Spring components@RefreshScope
Note, so that when the configuration file changes, Spring reloads the configuration of these components.
6.2 Manually refresh the configuration
In addition to automatic refresh, we can also manually refresh the configuration by sending HTTP requests to a specific endpoint:
curl -X POST http://localhost:8080/actuator/refresh
7. Summary
Dynamically specifying configuration files is an important feature in SpringBoot application management, which allows us to flexibly manage configuration information according to different environments and needs. Through properties, we can easily specify the location of the configuration file. Combined with tools such as Spring Cloud Config, we can further implement centralized management of configurations and dynamic refresh.
Through the above method, we can ensure that SpringBoot applications can correctly load and use configuration information in different environments, thereby improving application flexibility and maintainability.
The above is the detailed content of the SpringBoot project dynamically specifying configuration files. For more information about SpringBoot dynamically specifying configuration files, please follow my other related articles!