前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自定义配置类,读取Application.properties中的参数

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

作者头像
java攻城狮
发布2020-10-10 16:35:05
1.8K0
发布2020-10-10 16:35:05
举报
文章被收录于专栏:个人积累个人积累

场景

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

实际应用

1. 配置参数的添加

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

代码语言:javascript
复制
# 自定义配置 写在最外层,不要写在任意节点下面
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

代码语言:javascript
复制
// 这里根据你在配置类中最外层节点匹配查找`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的方式启动服务)
代码语言:javascript
复制
@Autowired
private HuaweiVCMConfiguration config;


public void test (){
    String host = config.getHuaweiHost();
}

补充

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

代码语言:javascript
复制
@Value("${face.huaweihost}")
private static final String host

//这样可取 但是不可避免的会出现书写错误  代码优雅度也没有那么高 如果参数比较多
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 场景
  • 实际应用
    • 1. 配置参数的添加
      • 2. 创建实体类
        • 3. 在启动类上面添加注解 @EnableConfigurationProperties
          • 4. 读取参数 (main方法中是读不到的,必须以springboot的方式启动服务)
          • 补充
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档