SpringBoot profiles have yml and properties are two, to see some articles say yml data-centric, better. Personally I feel that with better properties, so here is an example to properties format.

We all know @Value notes can be read from a configuration profile, if only configure a value, such as a particular domain name, configured to xxx.domain = www.xxx.com, so get used @Value directly in the code, compare Convenience.

But if it is a group of related configuration, such as code-related configuration, there are pictures verification code, phone code, email address Verification Code, if you want to make a verification code length is configurable. Whether as springboot configuration,

Referring to the written as:

Definitely yes!

Reference source is the best way to learn, let's look springboot is how to do

category corresponds to a configuration server: ServerProperties (paste only a partial member variables, to illustrate the problem)
 
   
Copy the code
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties
        implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered {

    /**
     * Server HTTP port.
     */
    private Integer port;

    @NestedConfigurationProperty  
    private Compression compression = new Compression();

  //省略其他成员变量、getter 、setter 
Copy the code
 
   
Compression-based part of the code:
Copy the code
public class Compression {

    /**
     * If response compression is enabled.
     */
    private boolean enabled = false;
Copy the code

 After reading should be very clear, and the reason can be written server.port = 8081, server.display-name = lhyapp, server.compression.enabled = true, because  ServerProperties  the class uses

 @ConfigurationProperties (prefix = "server", ignoreUnknownFields = true) annotation, which specifies the prefix prefix configuration file, if you want something like this type of server.compression.enabled = true, then we need a class reputation Compression, then ServerProperties  referenced in this class, attribute name corresponds to the name of the configuration profile.

 

 @ConfigurationProperties:

  Tell SpringBoot all the attributes and configuration files in this class for the corresponding configuration bind;
   prefix = "xxx": profile in which all of the following attributes one mapping

 only this component is a container assembly, in order to provide container the @ConfigurationProperties function;
 @ConfigurationProperties (prefix = "XXX") get the value from the global default configuration file;

Below to achieve the above said verification code configuration, you need the class:

Code: 
CoreConfiguration.java
Copy the code
@Configuration
@EnableConfigurationProperties(SecurityProperties.class)
public class CoreConfiguration {

    //配置一些bean
    //@Bean
    //public XXXX xxxx(){}
}
Copy the code
SecurityProperties.java
Copy the code
@ConfigurationProperties(prefix = "myapp")
public class SecurityProperties {

    private ValidateCodeProperties code = new ValidateCodeProperties();

    public ValidateCodeProperties getCode() {
        return code;
    }

    public void setCode(ValidateCodeProperties code) {
        this.code = code;
    }
}
Copy the code
ValidateCodeProperties.java
Copy the code
public class ValidateCodeProperties {

    private SmsCodeProperties sms = new SmsCodeProperties();

    private ImageCodeProperties image = new ImageCodeProperties();

    public SmsCodeProperties getSms() {
        return sms;
    }

    public void setSms(SmsCodeProperties sms) {
        this.sms = sms;
    }

    public ImageCodeProperties getImage() {
        return image;
    }

    public void setImage(ImageCodeProperties image) {
        this.image = image;
    }
}
Copy the code
SmsCodeProperties.java
Copy the code
public class SmsCodeProperties {

    private int length = 4;

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}
Copy the code
在application.properties 里配置
myapp.code.sms.length = 10
使用配置:
Copy the code
  @Autowired
    private SecurityProperties securityProperties;

    @RequestMapping("/length")
    public @ResponseBody String length(){
        int length = securityProperties.getCode().getSms().getLength();
        return String.valueOf(length);
    }
Copy the code