Spring Cloud Spring Boot mybatis Distributed microservices Cloud Architecture (iii) attribute profiles detailed (1)

Source: Internet
Author: User
Tags spring boot tutorial

It is believed that many people choose spring boot primarily because it takes into account the power of spring and the ease with which it can be developed quickly. In the spring boot use process, the most intuitive feeling is not the original integration of the spring application of a large variety of XML configuration content, instead of it is introduced in the pom.xml modular Starter POMs , each module has its own default configuration, so if not a special application scenario, It is only necessary to complete some of the properties in the application.properties configuration to open the application of each module.

In previous articles, there was a reference to application.properties the use, mainly used to configure database connections, log-related configuration and so on. In addition to these configuration content, this article describes some application.properties additional features and how to use them in the configuration.

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:

Com.didispace.blog.name= Program ape DD  com.didispace.blog.title=spring Boot Tutorial  

@Value("${属性名}")the corresponding configuration properties are then loaded with annotations, as follows:

@Component    Public class blogproperties {        @Value ("${com.didispace.blog.name}")       Private String name;      @Value ("${com.didispace.blog.title}")      Private  String title;         // omit getter and setter     }  

By convention, the unit tests are passed to verify that the properties in the Blogproperties have been loaded according to the configuration file.

@RunWith (Springjunit4classrunner.class) @SpringApplicationConfiguration (application.class)   Public classapplicationtests {@AutowiredPrivateblogproperties blogproperties; @Test Public voidGethello () throws Exception {assert.assertequals (Blogproperties.getname (),"Program APE DD"); Assert.assertequals (Blogproperties.gettitle (),"Spring Boot Tutorial"); }    }  
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 dd  com.didispace.blog.title=Spring Boot tutorial  Com.didispace.blog.desc=${com.didispace.blog.name} is trying 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 int  com.didispace.blog.number =${random. int }  # random long  com.didispace.blog.bignumber=${random.    The random number within the long} # 10 is  com.didispace.blog.test1=${random.  int(ten)}  20 random number  com.didispace.blog.test2=${ Random. int [ten]}  

Spring Cloud Spring Boot mybatis distributed micro-service Cloud Architecture (iii) attribute profiles detailed (1)

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.