The Spring boot program reads the configuration from Application.properties or Application.yaml by default, so how can I configure the configuration information externally and conveniently?
Check the official website, you can get the following scenarios:
Specify by command line
Springapplication will convert command-line option parameters to configuration information by default
For example, the command parameter at startup specifies:
java -jar myproject.jar --server.port = 9000
Specifying configuration items from the command line has the highest priority, but you can disable them by setaddcommandlineproperties
SpringApplication.setAddCommandLineProperties(false).
External configuration file
The spring program loads the application.properties configuration file from the following paths in priority order
- /config directory under the current directory
- Current directory
- The/config catalogue in Classpath
- Classpath and Directory
Therefore, it is very simple to have an external configuration file, create a new config folder in the directory where the Jar is located, and then put the configuration file, or directly in the configuration file in the Jar directory
Custom configuration Files
What if you don't want to use application.properties as a configuration file? No problem at all.
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Or
java -jar -Dspring.config.location=D:\config\config.properties springbootrestdemo-0.0.1-SNAPSHOT.jar
Of course, you can also specify in the code
@SpringBootApplication@PropertySource (value={"File:config.properties"})public class springbootrestdemoapplication { public static void Main(string[] args) {springapplication. Run (springbootrestdemoapplication. class, args); }}
Read different configurations by profile different environments
Configurations for different environments set up a configuration file, for example:
- Configuration configuration in the dev environment is in application-dev.properties;
- Configuration in the PROD environment is configured in Application-prod.properties.
Specify which file to use in Application.properties
spring.profiles.active = dev
Of course, you can also specify it manually at run time:
java -jar myproject.jar --spring.profiles.active = prod
Reference:
1 See also externalized Configuration
Jadepeng
Source: Jqpeng's technical notepad--http://www.cnblogs.com/xiaoqi
Your support is the greatest encouragement to bloggers, thank you for your serious reading.
The copyright of this article is owned by the author, welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
The Spring boot configuration file is placed outside the jar