Springboot自定义的yml文件获取属性方式

731 阅读2分钟

配置文件

app-sync:
  # 临时文件存放目录
  contentPath: ./static
  dec: 121
  dataOfFile: 111
  webToapp:
    # webToApp 用户信息同步
    user:
      baseUrl: www.com.cm
      enable: true
      cron: 0/5 * * ? * *
      getDir: /webToAppFile/userData/
    # webToApp 单位信息同步
    unit:
      # webToApp 单位同步数据接口
      baseUrl: www.com.cm
      # webToApp 是否开启单位同步
      enable: true
      # webToApp 当单位同步开启后 每隔多长时间进行同步 cron表达式
      cron: 0/5 * * ? * *
      # webToApp 当单位同步开启后 文件夹存放目录
      getDir: /webToAppFile/unitData/
    all:
      # webToApp 全表同步数据接口
      baseUrl: www.com.cm
      # webToApp 是否开启全表同步
      enable: true
      # webToApp 当全表同步开启后 每隔多长时间进行同步 cron表达式
      cron: 0/5 * * ? * *
      # webToApp 当全表同步开启后 文件夹存放目录
      getDir: /webToAppFile/allDetailData/
  appToweb:
    # appToWeb 用户信息同步
    user:
      # appToWeb 用户同步数据接口
      baseUrl: www.com.cm
      # appToWeb 是否开启用户同步
      enable: true
      # appToWeb 当用户同步开启后 文件夹存放目录
      getDir: /appToWebFile/userData/
    # appToWeb 用户信息同步
    all:
      # appToWeb 全表同步数据接口
      baseUrl: www.com.cm
      # appToWeb 是否开启反馈同步
      enable: true
      # appToWeb 当全表同步开启后 每隔多长时间进行同步 cron表达式
      cron: 0/5 * * ? * *
      # appToWeb 当全表同步开启后 文件夹存放目录
      getDir: /appToWebFile/all/

获取方式

package com.common.utils;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "app-sync")
public class ConfigYmlUtil {
    private String contentPath;
    private String numOfEncAndDec;
    private String dataOfFile;
    private final WebToApp webToapp = new WebToApp();
    private final AppToWeb appToweb = new AppToWeb();

    public String getContentPath() {
        return contentPath;
    }

    public void setContentPath(String contentPath) {
        this.contentPath = contentPath;
    }

    public String getNumOfEncAndDec() {
        return numOfEncAndDec;
    }

    public void setNumOfEncAndDec(String numOfEncAndDec) {
        this.numOfEncAndDec = numOfEncAndDec;
    }

    public String getDataOfFile() {
        return dataOfFile;
    }

    public void setDataOfFile(String dataOfFile) {
        this.dataOfFile = dataOfFile;
    }

    public WebToApp getWebToapp() {
        return webToapp;
    }

    public AppToWeb getAppToweb() {
        return appToweb;
    }

    /**
     * webToApp
     */
    public static class WebToApp {
        private final BaseProperty user = new BaseProperty();
        private final BaseProperty unit = new BaseProperty();
        private final BaseProperty all = new BaseProperty();

        public BaseProperty getUser() {
            return user;
        }


        public BaseProperty getUnit() {
            return unit;
        }

        public BaseProperty getAll() {
            return allDetail;
        }
    }


    /**
     * AppToWeb
     */
    public static class AppToWeb {
        private final BaseProperty user = new BaseProperty();
        private final BaseProperty all = new BaseProperty();

        public BaseProperty getUser() {
            return user;
        }

        public BaseProperty getAll() {
            return all;
        }
    }

    /**
     * 基础集合属性
     */
    public static class BaseProperty {
        private String baseUrl;
        private String enable;
        private String cron;
        private String getDir;

        public String getBaseUrl() {
            return baseUrl;
        }

        public void setBaseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
        }

        public String getEnable() {
            return enable;
        }

        public void setEnable(String enable) {
            this.enable = enable;
        }

        public String getCron() {
            return cron;
        }

        public void setCron(String cron) {
            this.cron = cron;
        }

        public String getGetDir() {
            return getDir;
        }

        public void setGetDir(String getDir) {
            this.getDir = getDir;
        }
    }

}

注意: 这种需要属性与集合定义的节点保持一致。