Spring boot-(3) Spring boot feature 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.