Springboot读取配置文件

Springboot读取配置文件(用于可配置接口的地址)

1.开发环境

IDEA + Springboot2.0.2 + maven + yml配置文件

2.需要的文件

1.配置文件:application.yml

2.工具类:SoapUtils.java

3.注入容器:YMLConfig.java

3.实现

application.yml:

# 接口地址
commonurl:
  recognitionWebServiceUrl: http://199.99.99.115:10001/WebService.asmx

YMLConfig.java:

@Component
@ConfigurationProperties(prefix = "commonurl")
@Data
public class YMLConfig {
    /**
     * 获取配置文件内容的字段
     */
    private String recognitionWebServiceUrl;

    public String getRecognitionWebServiceUrl() {
        return recognitionWebServiceUrl;
    }

    public void setRecognitionWebServiceUrl(String recognitionWebServiceUrl) {
        this.recognitionWebServiceUrl = recognitionWebServiceUrl;
    }
}

SoapUtils.java:

@Component
@Slf4j
public class SoapUtils {
    public static YMLConfig gogalConfig;
    /**
     * ws前缀
     */
    public static String RECOGNITION_WEB_SERVICE_PREFIX;
    /**
     * 注入
     */
    @Autowired
    private YMLConfig ymlConfig;
    /**
       * 静态方法想使要使用一个非静态对象,需要做一个初始化【重要】
       */
    @PostConstruct
    public void init() {
        gogalConfig = ymlConfig;
        RECOGNITION_WEB_SERVICE_PREFIX = ymlConfig.getRecognitionWebServiceUrl();
    }
     /**
     * 使用配置文件内容
     */
     public static String synchronize() throws IOException {
        return RECOGNITION_WEB_SERVICE_PREFIX;
    }
}

这样就可以进行快乐地调用啦!

感谢SpringBoot工具类直接读取配置文件内容_zuozhiyoulaisam的专栏-CSDN博客给予的帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值