1. External configuration
Spring boot supports external configurations so that the same application code can be used in different environments. You can use properties files, YAML files, environment variables, or command line parameters for external configuration. You can use @value annotations to inject property values directly into a defined bean, access the property through spring's environment abstraction, or bind to a structured object by @configurationproperties.
Spring Boot uses a special Propertysource order, which is designed to allow for reasonable coverage of values. The properties are selected in the following order:
1) Devtools Global properties set in the home directory, such as ~/.spring-boot-devtools.properties when Devtools settings are available;
2) @testpropertysource annotations in unit tests
3) @springboottest#properties Annotation properties in unit tests
4) command-line arguments
5) attributes in Spring_application_json (JSON embedded in environment variables or system properties)
6) ServletConfig Initialization parameters
7) ServletContext Initialization parameters
8) Jndi Properties in Java:comp/env
9) Java System Properties, i.e. system.getproperties ()
10) operating system environment variables
One) Randomvaluepropertysource, only attributes in random.*
Profile-specific application properties outside the jar package, such as application-(Profile). Properties and Yaml variants
Profile-specific application properties within the jar package, such as Application.properties and YAML variants
) application properties outside the jar package, such as Application.properties and YAML variants
) application properties within the jar package, such as Application.properties and YAML variants
16) @propertysource annotations in the @configuration class
17) Default properties (specified by Springapplication.setdefaultproperties)
Example: Suppose you develop @component and use a Name property:
import Org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Componentpublicclass Mybean {@Value (" ${name}")private String name;
View Code
In the app's classpath (for example, in a jar package), the Application.properties file provides a default property value of name. When running in a new environment, the Application.properties file can be supplied outside the jar package, replacing name in the jar package. For a one-time test, you can run at the specified command line, such as Java–jar App.jar--name= "Spring"
Spring boot-(3) Spring boot feature 2