Spring Boot? Profile Details: Custom attributes, random numbers, multi-environment configuration, etc.

Source: Internet
Author: User
Tags access properties assert log log spring boot tutorial

Custom properties and loading

When we use spring boot, we often need to define some of our own properties, which we can define directly as follows:

Application-dev.yml

Com.didispace.blog:  Name: Program APE DD  title:spring boot tutorial  desc: ${com.didispace.blog.name} is trying to write the ${ Com.didispace.blog.title} "# Random string  value: ${random.value}# random int number  : ${random.int}# random Long  bignumber : Random number within ${random.long}# 10  test1: ${random.int (10)}# 10-20 random number  test2: ${random.int[10,20]}

  

Package Com.wls.integrateplugs.property;import Org.springframework.beans.factory.annotation.value;import org.springframework.stereotype.component;/** * @author Program APE DD * @version 1.0.0 * @date 16/5/5 12:16. * @blog http://blog.didispace.com */@Componentpublic class Blogproperties {@Value ("${com.didispace.blog.name}") pri    vate String name;    @Value ("${com.didispace.blog.title}") private String title;    @Value ("${com.didispace.blog.desc}") Private String desc;    @Value ("${com.didispace.blog.value}") Private String Value;    @Value ("${com.didispace.blog.number}") private Integer number;    @Value ("${com.didispace.blog.bignumber}") Private Long bignumber;    @Value ("${com.didispace.blog.test1}") Private Integer test1;    @Value ("${com.didispace.blog.test2}") Private Integer test2;    Public String GetName () {return name;    } public void SetName (String name) {this.name = name;    } public String GetTitle () {return title; } public void Settitle (String title) {this.title = title;    } public String GetDesc () {return desc;    } public void Setdesc (String desc) {THIS.DESC = desc;    } public String GetValue () {return value;    The public void SetValue (String value) {this.value = value;    } public Integer GetNumber () {return number;    } public void Setnumber (Integer number) {this.number = number;    } public Long Getbignumber () {return bignumber;    } public void Setbignumber (Long bignumber) {this.bignumber = BigNumber;    } public Integer GetTest1 () {return test1;    } public void SetTest1 (Integer test1) {this.test1 = test1;    } public Integer GetTest2 () {return test2;    } public void SetTest2 (Integer test2) {this.test2 = Test2; }}

  

by convention, the unit tests are passed to verify that the properties in the Blogproperties have been loaded according to the configuration file. @Value("${属性名}") The corresponding configuration properties are then loaded with annotations, as follows:

Unit Test
Package Com.wls.test.integrateplugs.property;import Com.wls.integrateplugs.property.blogproperties;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.junit.Assert;import Org.junit.test;import Org.junit.runner.runwith;import org.springframework.beans.factory.annotation.Autowired; Import Org.springframework.boot.test.context.springboottest;import org.springframework.test.context.junit4.springjunit4classrunner;/** * * @author program APE DD * @version 1.0.0 * @blog/HTTP/    blog.didispace.com * */@RunWith (Springjunit4classrunner.class) @SpringBootTestpublic class Blogpropertiestest {    Private static final Log log = Logfactory.getlog (Blogpropertiestest.class);    @Autowired private blogproperties blogproperties;        @Test public void Test1 () throws Exception {assert.assertequals ("program ape DD", Blogproperties.getname ());        Assert.assertequals ("Spring boot Tutorial", Blogproperties.gettitle ()); Assert.assertequals ("program Ape DD is trying to write the Spring boot tutorial", BlogpRoperties.getdesc ());        Log.info ("Random number Test output:");        Log.info ("Random string:" + Blogproperties.getvalue ());        Log.info ("Random int:" + blogproperties.getnumber ());        Log.info ("Random long:" + blogproperties.getbignumber ());        Log.info ("Random 10 or Less:" + blogproperties.gettest1 ());    Log.info ("Random 10-20:" + blogproperties.gettest2 ()); }}

  

References between parameters

application.propertiesYou can also use a direct reference between the various parameters in, as in the following settings:

 

Com.didispace.blog.name= Program Ape ddcom.didispace.blog.title=spring Boot Tutorial com.didispace.blog.desc=${ Com.didispace.blog.name} is working hard to write "${com.didispace.blog.title}"

  

com.didispace.blog.descThe parameter references the name and attributes defined above title , and finally the value of the property is 程序猿DD正在努力写《Spring Boot教程》 .

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. Spring Boot's property configuration file can be used ${random} to produce an int value, a long value, or a string string to support a random value of a property.

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

  

setting property values from the command line

Believe that users who have used spring boot for a period of time must know this command: java -jar xxx.jar --server.port=8888 by using the –server.port property to set the port of the Xxx.jar app to 8888.

When run at the command line, the consecutive two minus -- signs are the application.properties identifiers that assign values to the property values in. So, the command, which is equivalent to the addition of a property in, is visible in the java -jar xxx.jar --server.port=8888 application.properties server.port=8888 sample project and can be verified by either deleting the value or setting the value using the command line.

Modifying property values from the command line is a good convenience, but is it not safe to change the parameters of the application run from the command line? Yes, so spring boot also provides a nice way to mask the command line access properties, which only needs to be set to block: SpringApplication.setAddCommandLineProperties(false) .

Multi-environment configuration

When we develop spring boot applications, the same set of programs is usually applied and installed into several different environments, such as: development, testing, production, etc. 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.

In spring boot, the multi-environment profile file name needs to be in application-{profile}.properties a format that {profile} corresponds to your environment identity, 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 spring.profiles.active property, and its value corresponds to the {profile} value.

such as: The spring.profiles.active=test application-test.properties configuration file contents will be loaded

Below, a sample experiment is performed with different service ports configured in different environments.

    • Create a different profile for each environment, application-dev.properties application-test.propertiesapplication-prod.properties

    • All three files are set with different server.port properties, such as: Dev environment set to 1111,test environment set to 2222,prod environment set to 3333

    • Application.properties spring.profiles.active=dev , which means that the dev environment is set by default

    • Test loading of different configurations

      • Execution java -jar xxx.jar , you can observe that the service port is set to the 1111 default development environment (DEV)
      • Execution java -jar xxx.jar --spring.profiles.active=test , you can observe that the service port is set to 2222 , that is, the configuration of the test environment (test)
      • Execution java -jar xxx.jar --spring.profiles.active=prod , you can observe that the service port is set to 3333 , that is, the configuration of the production environment (PROD)

According to the above experiment, we can summarize the multi-environment configuration ideas as follows:

    • application.propertiesThe common content is configured in and set to the spring.profiles.active=dev development environment as the default configuration
    • application-{profile}.propertiesConfigure different contexts in each environment
    • Activating configuration of different environments by command-line mode

Spring Boot? Profile Details: Custom attributes, random numbers, multi-environment configuration, etc.

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.