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.properties
You 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.desc
The 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)