springboot2 读取 自定义yml

版本环境:springboot版本2.1.1.RELEASE

1、在application.yml 同级创建自定义yml,必须以application- 开头

application-interface.yml 中 内容如下 

hikvision:
    zyml: # 资源目录
        qyxxjk: # 区域信息接口
            # 获取根区域信息
            gqyxx: /api/resource/v1/regions/root
            # 查询区域列表v2
            qylbv2: /api/irds/v2/region/nodesByParams

2、在application.yml中进行配置 

多个自定义yml文件,中间用逗号隔开 

# Spring配置
spring:
  # 资源信息
  messages:
    # 国际化资源文件路径
    basename: i18n/messages
  profiles: 
    active: druid,interface

 3、创建配置类接收yml中的值

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * 第三方接口管理类
 */
@Configuration
public class InterfaceProperties {
    @Value("${hikvision.zyml.qyxxjk.gqyxx}")
    private String gqyxx;
    @Value("${hikvision.zyml.qyxxjk.qylbv2}")
    private String qylbv2;

    public String getGqyxx() {
        return gqyxx;
    }

    public void setGqyxx(String gqyxx) {
        this.gqyxx = gqyxx;
    }

    public String getQylbv2() {
        return qylbv2;
    }

    public void setQylbv2(String qylbv2) {
        this.qylbv2 = qylbv2;
    }
}

4、其他类中使用

    @Autowired
    private InterfaceProperties interfaceProperties;
System.out.println("---------------"+interfaceProperties.getGqyxx()+"--"+interfaceProperties.getQylbv2());

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章