SpringBoot的配置文件有yml和properties两种,看一些文章说yml以数据为中心,比较好。我的以为properties更好用,因此这里以properties格式为例来讲。springboot

咱们都知道@Value 注解能够从配置文件读取一个配置,若是只是配置某个值,好比 某一个域名,配置为xxx.domain = www.xxx.com ,这样直接在代码里用@Value获取,比较方便。app

可是若是是一组相关的配置,好比验证码相关的配置,有图片验证码、手机验证码、邮箱验证码,若是想把验证码的长度作成可配置。是否能像springboot的配置,dom

参照着写成:post

确定是能够的!学习

参照源码是最好的学习方式,下面来看springboot是怎么作的this

server对应着一个配置类:ServerProperties(只粘贴出了部分红员变量,来讲明问题)
 
   
复制代码
@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 
复制代码
 
   
Compression类部分代码:
复制代码
public class Compression { /** * If response compression is enabled. */ private boolean enabled = false;
复制代码

 看事后应该很明白了,之因此能写成server.port=8081,server.display-name=lhyapp,server.compression.enabled=true  ,是由于 ServerProperties 类上使用了url

 @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) 注解,其中prefix 指定配置文件里的前缀, 若是想弄成这样式的 server.compression.enabled = true ,就须要再声名一个类 Compression ,而后在ServerProperties 中引用这个类,属性名对应配置文件中的配置名。

 

 @ConfigurationProperties:

  告诉SpringBoot将本类中的全部属性和配置文件中相关的配置进行绑定;
   prefix = "xxx":配置文件中哪一个下面的全部属性进行一一映射

 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
 @ConfigurationProperties(prefix = "xxx")默认从全局配置文件中获取值;

下边实现上面说的验证码配置,须要的类:

代码:
CoreConfiguration.java
复制代码
@Configuration
@EnableConfigurationProperties(SecurityProperties.class)
public class CoreConfiguration {

    //配置一些bean
    //@Bean
    //public XXXX xxxx(){}
}
复制代码
SecurityProperties.java
复制代码
@ConfigurationProperties(prefix = "myapp")
public class SecurityProperties {

    private ValidateCodeProperties code = new ValidateCodeProperties();

    public ValidateCodeProperties getCode() {
        return code;
    }

    public void setCode(ValidateCodeProperties code) {
        this.code = code;
    }
}
复制代码
ValidateCodeProperties.java
复制代码
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;
    }
}
复制代码
SmsCodeProperties.java
复制代码
public class SmsCodeProperties {

    private int length = 4;

    public int getLength() {
        return length;
    }

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

    @RequestMapping("/length")
    public @ResponseBody String length(){
        int length = securityProperties.getCode().getSms().getLength();
        return String.valueOf(length);
    }
复制代码