自定义配置类,读取Application.properties中的参数

4,708 阅读2分钟

场景

在开发过程中,我们可能需要指定一些固定的参数,或者配置一些其它第三方参数。但是在后期应用中,可能需要对改内容进行不定期的修改。为了降低项目重新部署的成本。我们可以将这些内容直接当成配置写在application.yml中,方便后期修好,同时添加一个实体类,方便读取配置参数

实际应用

1. 配置参数的添加

比如我这边对接的是华为的vcm模块,将常用的参数配置在外面

# 自定义配置 写在最外层,不要写在任意节点下面
face:
  huaweihost: https://172.19.59.241
  account: admin
  password: huawei@admin
  uploadhost: https://172.19.59.236:18444
  rootpath: E:\\FileUpload\\

2. 创建实体类

在项目的config文件夹下创建HuaweiVCMConfiguration

// 这里根据你在配置类中最外层节点匹配查找`face`
@ConfigurationProperties(prefix = "face", ignoreUnknownFields = true)
public class HuaweiVCMConfiguration implements InitializingBean {

    private String huaweihost;

    private String account;

    private String password;

    private String uploadhost;

    private String rootpath;

    //错误检查  项目启动即可检查该配置内容是否读取到或者参数名称是否有错误
    @Override
    public void afterPropertiesSet() throws Exception {
        if (StringUtils.isBlank(getHuaweihost())) {
            throw new IllegalStateException("Property \"face.host\" cannot not be blank");
        }
        if (StringUtils.isBlank(getAccount())) {
            throw new IllegalStateException("Property \"face.account\" cannot not be blank");
        }
        if (StringUtils.isBlank(getPassword())) {
            throw new IllegalStateException("Property \"face.password\" cannot not be blank");
        }
        if (StringUtils.isBlank(getUploadhost())) {
            throw new IllegalStateException("Property \"face.uploadhost\" cannot not be blank");
        }
        if (StringUtils.isBlank(getRootpath())) {
            throw new IllegalStateException("Property \"face.rootpath\" cannot not be blank");
        }
    }

    public String getHuaweihost() {
        return huaweihost;
    }

    public void setHuaweihost(String huaweihost) {
        this.huaweihost = huaweihost;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getUploadhost() {
        return uploadhost;
    }

    public void setUploadhost(String uploadhost) {
        this.uploadhost = uploadhost;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRootpath() {
        return rootpath;
    }

    public void setRootpath(String rootpath) {
        this.rootpath = rootpath;
    }
}


3. 在启动类上面添加注解 @EnableConfigurationProperties

非常重要,这个注解不添加会报错 ,如果是单个可以直接写@EnableConfigurationProperties(HuaweiVCMConfiguration.class)

如果是多个@EnableConfigurationProperties({HuaweiVCMConfiguration.class,HuaweiVCMConfiguration.class})

4. 读取参数 (main方法中是读不到的,必须以springboot的方式启动服务)

    @Autowired
    private HuaweiVCMConfiguration config;
    
    
    public void test (){
        String host = config.getHuaweiHost();
    }

补充

可能有些人觉得这样写比较麻烦,但是代码维护起来是很方便的,代码整洁度很高.当然你可以直接采用注解的方式去读取配置内容比如

    @Value("${face.huaweihost}")
    private static final String host
    
    //这样可取 但是不可避免的会出现书写错误  代码优雅度也没有那么高 如果参数比较多