Spring Cloud Spring Boot mybatis distributed micro-service Cloud architecture

Source: Internet
Author: User
Tags assert 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, which each module has its own default configuration, Therefore, if it is not a special application scenario, it is only necessary to complete some property configuration in Application.properties to enable the application of each module.

The use of Application.properties is mentioned in previous articles, mainly for configuring database connections, log-related configurations, and so on. In addition to these configuration content, this article describes some additional features and how to use them in the Application.properties 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=程序猿DD  com.didispace.blog.title=Spring Boot教程  

The corresponding configuration properties are then loaded by @value ("${Property Name}") annotations, as follows:

@Component  public class BlogProperties {      @Value("${com.didispace.blog.name}")      private String name;      @Value("${com.didispace.blog.title}")      private String title;      // 省略getter和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(SpringJUnit4Cla***unner.class)  @SpringApplicationConfiguration(Application.class)  public class ApplicationTests {      @Autowired      private BlogProperties blogProperties;      @Test      public void getHello() throws Exception {          Assert.assertEquals(blogProperties.getName(), "程序猿DD");          Assert.assertEquals(blogProperties.getTitle(), "Spring Boot教程");      }  }  

References between parameters

You can also use a direct reference between the various parameters in Application.properties, as in the following settings:

com.didispace.blog.name=程序猿DD  com.didispace.blog.title=Spring Boot教程  com.didispace.blog.desc=${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》  

The Com.didispace.blog.desc parameter references the name and title attribute defined above, and finally the value of the property is the program ape DD is trying to write the Spring boot tutorial.

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. The Spring Boot property configuration file can be ${random} to produce an int, a long, or string string that supports the random value of the property.

# 随机字符串  com.didispace.blog.value=${random.value}  # 随机int  com.didispace.blog.number=${random.int}  # 随机long  com.didispace.blog.bignumber=${random.long}  # 10以内的随机数  com.didispace.blog.test1=${random.int(10)}  # 10-20的随机数  com.didispace.blog.test2=${random.int[10,20]}  

Code Source please add link description

Spring Cloud Spring Boot mybatis distributed micro-service Cloud architecture

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.