SpringBoot的配置文件有yml和properties两种,看一些文章说yml以数据为中心,比较好。个人觉得properties更好用,所以这里以properties格式为例来说。

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

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

参照着写成:

肯定是可以的!

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

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 类上使用了

 @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);
    }
复制代码