Bootstrap

Spring Boot 读取 yml 并映射至实体

application-base.yml

app:
  # 附件存储路径
  upload-attachments: /data/attachments/
  # 报告导出详情 url - 前端
  score-detail-url: ${app.host.web}/#/process/start?processNo={}
  # api 文件下载 url
  file-download-url: ${app.host.web}/prod-api/sys_file_info/download/{}?fullfilename={}
  # kkfile 文件预览 url
  file-preview-url: ${app.host.web}/preview/onlinePreview?url={}

application-test.yml

app:
  host:
    # 前端 host
    web: http://10.10.10.11:5174
import cn.hutool.core.io.FileUtil;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * 功能描述
 *
 * @author jason
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig implements InitializingBean {

    /**
     * 附件路径
     */
    private String uploadAttachments;

    /**
     * 报告详情点击 url
     */
    private String scoreDetailUrl;

    /**
     * 文件下载 url
     */
    private String fileDownloadUrl;

    /**
     * kkfileview 文件预览 url
     */
    private String filePreviewUrl;

    /**
     * host
     */
    private Host host;

    /**
     * 方便直接获取
     */
    public static AppConfig INSTANCE;

    @Override
    public void afterPropertiesSet() {
        INSTANCE = this;

        // 自动创建文件夹
        FileUtil.mkdir(uploadAttachments);
    }

    @Data
    public static class Host {
        private String web;
        private String api;
        private String file;
    }

}

;