Spring boot 获取配置文件

方法一

package com.tongtech.space.common.configs;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author 
 * @Date: 2020/8/19 16:04
 */
@Component
@ConfigurationProperties(prefix = "ask")
public class BeiJingAskConfig {

	/**
	 * 获取我的办件本信息地址
	 */
	private String url;
	/**
	 * #获取办件基本信息接口地址
	 */
	private String infoUrl;
	/**
	 * 办件token
	 */
	private String token;
	/**
	 * 办件areacode
	 */
	private String areacode;


	//省略get set 方法
}


//使用的时候 
@Resource
private   BeiJingAskConfig  config;

方法二

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class PropertyUtil {
	@Autowired
	private Environment env;
	
	private static Environment props;
	
	@PostConstruct
	public synchronized void init() {
		props = env;
	}
	
    public static String getProperty(String key){
        return props.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        return props.getProperty(key, defaultValue);
    }

}

//使用的时候
	String testaa = PropertyUtil.getProperty("back.url.recommendurl");

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值