美文网首页
配置文件属性读取

配置文件属性读取

作者: 怪诞140819 | 来源:发表于2018-11-12 18:07 被阅读25次

    在系统开发中,我们很多的时候需要根据不同的环境从配置文件中读取属性,我们可以建立不同的配置文件来定义环境,比如application-dev.yml,application-test.yml,application-prod.yml,或者我们可以建立config-server来托管我们的不同环境下的配置,但是这些都不是这篇文章关注的内容,我们关注的是读取配置文件中的内容,例如我们要配置七牛云上传的属性

    1.目录结构

    代码结构

    1.1 总的属性配置

    @ConfigurationProperties(prefix = "kjb.basic")
    public class BasicProperties {
    
        private UploadProperties upload = new UploadProperties();
    
        public UploadProperties getUpload() {
            return upload;
        }
    
        public void setUpload(UploadProperties upload) {
            this.upload = upload;
        }
    }
    
    public class UploadProperties {
    
        QiniuProperties qiniu = new QiniuProperties();
    
        public QiniuProperties getQiniu() {
            return qiniu;
        }
    
        public void setQiniu(QiniuProperties qiniu) {
            this.qiniu = qiniu;
        }
    }
    
    
    public class QiniuProperties {
    
        private String domain;
    
        private String accessKey;
    
        private String secretKey;
    
        private String bucket;
    
        public String getDomain() {
            return domain;
        }
    
        public void setDomain(String domain) {
            this.domain = domain;
        }
    
        public String getAccessKey() {
            return accessKey;
        }
    
        public void setAccessKey(String accessKey) {
            this.accessKey = accessKey;
        }
    
        public String getSecretKey() {
            return secretKey;
        }
    
        public void setSecretKey(String secretKey) {
            this.secretKey = secretKey;
        }
    
        public String getBucket() {
            return bucket;
        }
    
        public void setBucket(String bucket) {
            this.bucket = bucket;
        }
    }
    
    
    @Configuration
    @EnableConfigurationProperties(BasicProperties.class)
    public class BeanConfig {
    }
    
    

    2.配置属性

    kjb:
      basic:
        upload:
          qiniu:
            accessKey: xxx
            secretKey: xxx
            domain: xxx
            bucket: xxx
    

    3.读取属性

    @Service("uploadService")
    public class QiniuUploadServiceImpl implements UploadService {
    
       @Autowired
       private BasicProperties basicProperties;
    
       @Override
       public Token getToken() {
           Auth auth = Auth.create(basicProperties.getUpload().getQiniu().getAccessKey(), basicProperties.getUpload().getQiniu().getSecretKey());
           String upToken = auth.uploadToken(basicProperties.getUpload().getQiniu().getBucket(),null);
           String domain = basicProperties.getUpload().getQiniu().getDomain();
           QiniuToken qiniuToken = new QiniuToken();
           qiniuToken.setDomain(domain);
           qiniuToken.setToken(upToken);
           return qiniuToken;
       }
    }
    

    相关文章

      网友评论

          本文标题:配置文件属性读取

          本文链接:https://www.haomeiwen.com/subject/eowcfqtx.html