Spring Boot Property Configuration

Source: Internet
Author: User
Tags dname

The configuration of the properties is often used in projects, and Spring Boot has a much simplified configuration of properties relative to the Spring framework configuration file.

One: Attribute configuration priority

1. Command-line arguments 2. Jndi attribute 3.Java from Java:comp/env System Properties (System.getproperties () 4. Operating system environment variable 5.RandomValuePropertySource configuration random.* property value 6.jar Package external application-{profile}.properties or APPLICATION.YML ( With spring.profile) configuration file 7.jar application-{profile}.properties or application.yml (with Spring.profile) inside the package Application.properties or Application.yml (without spring.profile) outside of the configuration file 8.jar package Config file 9.jar application.properties or application.yml (without spring.profile) profile in the package [email protected] The @propertysource11 on the annotation class. Default properties specified by Springapplication.setdefaultproperties

(1.1) Command line parameters

java -jar myApp.jar --name="Spring Boot" --server.port=8081pass parameters by way. Command-line arguments are set after Myapp.jar.

(1.2) Java EE contextual environment variables

(1.3) Java System variables

java -Dname="ws" -jar myApp.jar, set the Name property to WS

If you set Java-dname= "ws"-jar app.jar--name= "boot" because the command line parameters are high priority, the Name property is the boot

(1.4) Operating system environment variables

Operating system environment variables such as Java_home,maven_home that are configured when installing Java and MAVEN are environment variables for the operating system.

(1.5) Random values

# random string com.ws.blog.value=${random.value}# random intcom.ws.blog.number=${random.int}# random longcom.ws.blog.bignumber=${ Random number within random.long}# 10 Com.ws.blog.test1=${random.int (10)}# 10-20 random number com.ws.blog.test2=${random.int[10,20]}

(1.6) @PropertySource

This annotation allows you to specify a specific property configuration text

Two: application.properties file load Order

1. The/config subdirectory under the current path. 2. Current path. 3./config sub-path under Classpath path. 4. Classpath Path

Three: Configuring attribute classes

(3.1) configuration with @value ("${xxx}")

1 @Configuration2  Public classMyconfig {3@Value ("${ws.name}")4     PrivateString name;5@Value ("${ws.age}")6     PrivateInteger age;7 8      PublicString GetName () {9         returnname;Ten     } One  A      Public voidsetName (String name) { -          This. Name =name; -     } the  -      PublicInteger getage () { -         returnAge ; -     } +  -      Public voidsetage (Integer age) { +          This. Age =Age ; A     } at}

(3.2) with @configurationproperties configuration

First, add the following dependencies to the POM

<Dependency>    <groupId>Org.springframework.boot</groupId>    <Artifactid>Spring-boot-configuration-processor</Artifactid>    <Optional>True</Optional></Dependency>

Add the following configuration to the Application.properties file


Configuring the Myconfig Class

Importorg.springframework.boot.context.properties.ConfigurationProperties;Importorg.springframework.context.annotation.Configuration;Importjava.util.List, @Configuration @configurationproperties (prefix= "WS") Public classMyconfig {PrivateString name; PrivateInteger age; PrivateList<string>listaddress;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }     PublicList<string>getlistaddress () {returnlistaddress; }     Public voidSetlistaddress (list<string>listaddress) {         This. listaddress =listaddress; }}

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.