springboot使用@ConfigurationProperties加载yml配置文件之字符串类型

本文介绍如何在SpringBoot中灵活配置和使用不同环境下的外部系统接口地址,通过使用@ConfigurationProperties注解实现配置的自动绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有如下的场景,现在系统a需要调用其他系统的接口,接口地址分测试环境和生产环境,那么我们在java代码里面写的调用地址肯定不能写死了,合理的解决方法是把调用地址写在配置文件里面,然后配置文件分为测试环境用的配置文件和生产环境用的配置文件,这样就可以了。在以前使用spring的时候,我们可以在配置文件里面这样子来做:

<context:property-placeholder location="classpath:param.properties" />
<bean id="param" class="com.roadjava.common.Param">
		<property name="uploads" value="${roadjava.uploads}" />
		<property name="sendsmsurl" value="${roadjava.sendsmsurl}" />
		<property name="domainNameUrl" value="${roadjava.domainNameUrl}" />
	</bean>

在程序中我们通过直接注入Param类的对象就能使用param.properties的值了,这没毛病。另外还有一种写法就是通过$Value注解,如下:

@Service 
public class FraudServiceImpl implements FraudService {
	@Value("${json.path}")
	private String jsonPath;
	@Value("${result.path1}")
	private String resultPath1;
	@Value("${result.path2}")
	private String resultPath2;
	@Value("${codecluster}")
	private String codecluster;
}	

当然了,你还是要使用一点xml的配置来告诉spring去哪里找配置文件:

<context:property-placeholder location="classpath:param.properties" />

这两种方法都是没问题的,但是springboot怎么来做呢?比如我的配置都写在application.yml中,在java代码中怎么取到我自己定义的配置呢?比如application.yml中有如下配置内容:

httpurls:
    launchQuery: http://10.12.13.14:8081/antiCheat/RequestComputation
    getResultByRequestId: http://10.12.13.14:8081/antiCheat/QueryResponse
    queryDetails: http://10.12.13.14:8081/antiCheat/StatisticMpcRequest
    resDetails: http://10.12.13.14:8081/antiCheat/StatisticMpcResponse
    allResMoney: http://10.12.13.14:8081/antiCheat/StatisticSumMpcResponse

我们应该怎么使用?自然也是同样的思想,我希望把这些变量注入到一个类中,这个类我写好了,如下,先贴出代码之后再做解释:

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

@Configuration
@ConfigurationProperties(prefix="httpurls")
public class AntiFraudParamConfiguration{
	 private String launchQuery;
	 private String getResultByRequestId;
	 private String queryDetails;
	 private String resDetails;
	 private String allResMoney;
	public String getLaunchQuery() {
		return launchQuery;
	}
	public void setLaunchQuery(String launchQuery) {
		this.launchQuery = launchQuery;
	}
	public String getGetResultByRequestId() {
		return getResultByRequestId;
	}
	public void setGetResultByRequestId(String getResultByRequestId) {
		this.getResultByRequestId = getResultByRequestId;
	}
	public String getQueryDetails() {
		return queryDetails;
	}
	public void setQueryDetails(String queryDetails) {
		this.queryDetails = queryDetails;
	}
	public String getResDetails() {
		return resDetails;
	}
	public void setResDetails(String resDetails) {
		this.resDetails = resDetails;
	}
	public String getAllResMoney() {
		return allResMoney;
	}
	public void setAllResMoney(String allResMoney) {
		this.allResMoney = allResMoney;
	}
	 
}

解释:

一、@Configuration注解的作用和@Controller、@Service 注解一样,都用来生成被spring管理的bean

二、被@ConfigurationProperties(prefix="httpurls")注解的类回去classpath下的application.yml中去找以httpurls开头的配置项

三、类的属性要和需要注入的配置的名称保持完全一致。

四、第四点也是最容易犯错误的一点,就是application.yml文件的书写格式,格式是:

属性名: 属性值,在“:”和属性值之间一定要有个空格,这是yaml语言的写法,记得一定要有个控制。不然实体类无法获取到值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值