Sping boot entry to combat introduction (III): Spring Boot Property configuration

Source: Internet
Author: User

This article is the third chapter of the Sping boot entry to the actual Combat series introductory article.   Describes the property configuration for spring boot. Traditional spring Web App custom properties are typically added by adding a demo.properties profile (file name customization) and then in the XML configuration through
<util:properties id= "Demoprops" location= "Classpath:demo.properties"/>

introduce a property file. then define a bean to read these properties, Bean configuration:

<bean class= "Org.springframework.beans.factory.config.MethodInvokingFactoryBean" >    <property name= " Staticmethod "value=" package name. Demoutil.init "/>    <property name=" Arguments ">        <list>            <ref bean=" Demoprops "/>        </list>    </property></bean>

Bean definition:

public class Demoutil {     private static properties properties;     static void Init (properties props) {          properties = props;     }     public static string GetValue (String key) {          return Properties.getproperty (key);}     }

Other places use the Demoutil.getvalue () method to access the value of a specific property.

Spring boot allows you to configure properties in multiple places, including properties files, YAML files, environment variables, System Properties, command-line parameters, and so on, which are added to environment by spring boot and can be @ValueAnnotations Environmentinstance, or @ConfigurationPropertiesThe annotated class to access. Property Load Priority order:
  1. If there is a property that uses the Devtools,devtools global setting (User directory ~/.spring-bootdevtools.properties)
  2. Annotated @testpropertysource of the test class
  3. Test class properties Annotations @SpringBootTest #properties
  4. Command-line arguments
  5. Attributes in Spring_application_json (environment variables or system properties)
  6. ServletConfig Initialization Parameters
  7. ServletContext Initialization Parameters
  8. Jndi parameter java:comp/env
  9. Java System Properties System.getproperties ()
  10. Operating system Environment variables
  11. Randomvaluepropertysource properties of the configuration random.*
  12. APPLICTAION-{PROFILE}.PROPERTIES,APPLICTAION-{PROFILE}.YML configuration file outside the jar package
  13. APPLICTAION-{PROFILE}.PROPERTIES,APPLICTAION-{PROFILE}.YML configuration file inside the jar package
  14. APPLICTAION.PROPERTIES,APPLICTAION.YML configuration file outside the jar package
  15. APPLICTAION.PROPERTIES,APPLICTAION.YML configuration file inside the jar package
  16. @PropertySource annotations on the @Configuration class specify the configuration file
  17. Default property: Springapplication.setdefaultproperties
The above attribute configuration, in addition to the Red label, other general applications less. The configuration with the lower ordinal number is higher than the ordinal high configuration, that is, if the same property configuration exists, the configuration with the low ordinal number overrides the high-order configuration. Configuration Properties1. Command line arguments when you start the Spring boot app, you can specify command-line arguments, such as:
Java-jar Springboot-demo-properties.jar--my.name=command_line_devlink

The value of this parameter overrides the property configuration value of the same name elsewhere in the application. the command-line arguments are placed behind the Xx.jar.

command-line parameter configuration 2 can be disabled by springapplication.setaddcommandlineproperties (false). Java System Properties You can also specify Java System Properties when you start the Spring boot app, such as:
Java-dmy.name=system_properties_devlink-jar Springboot-demo-properties.jar

Java System Properties are placed after the Java command.

3. Operating system environment variables configured too java_home should understand what the environment variables are. Some operating systems may not support the. Delimited property name, which can be concatenated instead of an underscore.   Spring Boot Treats myname, my.name, my_name as equivalent. 4. Apply a property profile (. properties file or. yml file). properties file Property Configuration format:
MY.NAME=DEVLINKMY.LIST[0]=AAA  //Configuration list MY.LIST[1]=BBB

. yml file Property configuration format:

my:  name:devlink list  :   //configuration lists     -AAA     -BBB

In Yml, there must be a space after the colon between the property name and the value.

Apply Attribute Profile location:
    1. A subdirectory in the current directory where the jar package is located/config
    2. The current directory where the jar package resides
    3. Classpath subdirectories under the root directory/config
    4. Classpath root directory
The lower priority of the sequence number is higher than the priority of the ordinal, i.e. the configuration outside the jar package takes precedence over the configuration within the jar package. In the same directory, the. properties file takes precedence over the. yml file. Application-{profile}.properties priority is higher than application.properties. Accessing Properties1. Add @Value ("${xx}") annotation to the properties on the class. Such as:
@Value ("${my.name}") Private String myName;

You can specify default values, such as @Value ("${my.gender:f}"), when My.gender is not configured, the value "F" is used by default

2. Accessed through the @configurationproperties annotated class. As defined:
@Configuration @configurationproperties (prefix = "my") public class Myconfig {    private String name;    Getter/setter;}

Then, in the bean that needs to be accessed, the Myconfig instance is injected through @autowired, and the My.name property value is accessed through the GetName () method.

3. Access through the environment instance. Such as:
@Autowiredprivate Environment env;

Then call Env.getproperty ("My.name") to access it.

This article source: https://github.com/ronwxy/springboot-learning/tree/master/springboot-demo-properties

Sping boot entry to combat introduction (III): Spring Boot Property configuration

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.