Reprinted from:
https://blog.csdn.net/qq_21387171/article/details/53876721
https://blog.csdn.net/catoop/article/details/50548009
http://blog.didispace.com/springbootproperties/ Custom properties and loading
In Application.properties, you can customize some properties and then load the corresponding configuration properties by @value ("${Property Name}") annotations. references between Parameters
You can also use a direct reference between the various parameters in Application.properties, as in the following settings:
Com.didispace.blog.name= Program APE DD
com.didispace.blog.title=spring boot tutorial
com.didispace.blog.desc=${ Com.didispace.blog.name}
using random numbers
In some cases, some parameters we need to hope that it is not a fixed value, such as keys, service ports, etc. The Spring Boot property configuration file can be ${random} to produce an int, a long, or string string that supports the random value of the property.
# random string
com.didispace.blog.value=${random.value}
# random int
Com.didispace.blog.number=${random.int}
# Random Long
Com.didispace.blog.bignumber=${random.long}
# 10 or less random number
com.didispace.blog.test1=${ Random.int (Ten)}
# 10-20 random number
com.didispace.blog.test2=${random.int[10,20]}
setting property values from the command line
When running at the command line, you can use Java-jar Xxx.jar--server.port=8888, which is equivalent to adding a property server.port=8888 in Application.properties.
Modifying property values from the command line provides a good convenience, but it is not safe to change the parameters of the application run from the command line. Yes, so spring boot also provides the ability to mask command-line access properties, which can be masked only by this setting: Springapplication.setaddcommandlineproperties (False). Multi-environment configuration
When developing a spring boot application, the same set of programs is usually applied and installed into several different environments, such as development, testing, production, and so on. Each environment's database address, server port and so on configuration will be different, if you are packaging for different environments to frequently modify the configuration file, it will be a very cumbersome and prone to error.
For a multi-environment configuration, the basic idea of a variety of project building tools or frameworks is consistent, and the Spring boot is no exception, or simpler, by configuring multiple profiles of different environments and then packaging them to specify what needs to be packaged.
The multi-Environment profile file name in spring boot needs to meet the application-{profile}.properties format, where {profile} corresponds to your environment ID, such as:
Application-dev.properties: Development environment
application-test.properties: Test Environment
application-prod.properties: Production environment
As to which specific configuration file will be loaded, it needs to be set in the Application.properties file by the Spring.profiles.active property, whose value corresponds to the {profile} value. The contents of the Application-test.properties configuration file will be loaded as Spring.profiles.active=test
So in general, application.properties configures generic content and sets Spring.profiles.active=dev to the development environment as the default configuration, Application-{profile}. The properties are configured with different contexts, and the configuration of different environments is activated by command-line mode.
you can use--spring.config.location=xxx.properties to specify an external configuration file
Any class that is managed by spring implements the interface Environmentaware override method Setenvironment can get the variables in the system environment variables and application configuration files when the project starts.
@Configuration public
class Mywebappconfigurer implements Environmentaware {
private relaxedpropertyresolver Propertyresolver;
@Value ("${spring.datasource.url}")
private String myurl;
/**
* This method is just a way to test the implementation of the Environmentaware interface and read the environment variables.
*/
@Override public
void setenvironment (Environment env) {
logger.info (Env.getproperty ("Java_home "));
Logger.info (Myurl);
String str = env.getproperty ("Spring.datasource.url");
Logger.info (str);
Propertyresolver = new Relaxedpropertyresolver (env, "spring.datasource.");
String url = propertyresolver.getproperty ("url");
Logger.info (URL);
}
}
Note that the overridden method setenvironment is executed when the system is started. Environment inherits from Propertyresolver.
You can also use this method:
@Inject
Private Environment env;
Env.getproperty ("xxx.xxx");
In addition to the Spring.profiles.active property configuration, you can also use the following methods:
Using @propertysource annotations on the startup class
Or use @profile annotations to specify a specific environment:
Public interface SendMessage {public
void Send ();
}
Dev environment Implementation class
@Component
@Profile ("dev") public
class Devsendmessage implements SendMessage {
@ Override public
Void Send () {
System.out.println (">>>>>>>>dev Send () <<< <<<<< ");
}
}
STG environment Implementation Class
@Component
@Profile ("STG") public
class Stgsendmessage implements SendMessage {
@ Override public
Void Send () {
System.out.println (">>>>>>>>stg Send () <<< <<<<< ");
}
}