springboot使用@ConfigurationProperties封装配置文件

业务场景:
把配置文件的信息,读取并自动封装成实体类,可以使用@ConfigurationProperties,把同类的配置信息自动封装成实体类。
1、在pom.xml中添加依赖包

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

2、创建配置文件(application.properties)

wx.appid = wx111111
wx.redirectUri = https://www.baidu.com/
wx.templateId = 1
wx.first = 模板标题
wx.remark = 模板备注
wx.color = #000000

sms.appid = 111111
sms.appkey = bd3bfba026f711eaac3b005056b84de4
sms.templateId = 1
sms.sign = Jeff

3、创建测试类1(WxSettings.java)

package com.jeff.config;

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

@Component
@ConfigurationProperties(prefix = "wx")
public class WxSettings {

	private String appid;
	private String redirectUri;
	private Integer templateId;
	private String first;
	private String remark;
	private String color;

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getRedirectUri() {
		return redirectUri;
	}

	public void setRedirectUri(String redirectUri) {
		this.redirectUri = redirectUri;
	}

	public Integer getTemplateId() {
		return templateId;
	}

	public void setTemplateId(Integer templateId) {
		this.templateId = templateId;
	}

	public String getFirst() {
		return first;
	}

	public void setFirst(String first) {
		this.first = first;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	@Override
	public String toString() {
		return "WxSettings [appid=" + appid + ", redirectUri=" + redirectUri + ", templateId=" + templateId + ", first="
				+ first + ", remark=" + remark + ", color=" + color + "]";
	}

}

4、创建测试类2(SmsSettings.java)

package com.jeff.config;

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

@Component
@ConfigurationProperties(prefix = "sms")
public class SmsSettings {

	private String appid;
	private String appkey;
	private Integer templateId;
	private String sign;

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getAppkey() {
		return appkey;
	}

	public void setAppkey(String appkey) {
		this.appkey = appkey;
	}

	public Integer getTemplateId() {
		return templateId;
	}

	public void setTemplateId(Integer templateId) {
		this.templateId = templateId;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	@Override
	public String toString() {
		return "SmsSettings [appid=" + appid + ", appkey=" + appkey + ", templateId=" + templateId + ", sign=" + sign
				+ "]";
	}

}

5、创建测试类(MyController.java)

package com.jeff.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jeff.config.SmsSettings;
import com.jeff.config.WxSettings;

@RestController
public class MyController {

	@Autowired
	private WxSettings wx;
	@Autowired
	private SmsSettings sms;

	@RequestMapping("myTest")
	public String myTest() {
		System.out.println(wx.toString());
		System.out.println(sms.toString());
		return "success";
	}

}

6、打开浏览器访问 http://localhost:8080/myTest,控制台输出结果
在这里插入图片描述
在这里插入图片描述

### 如何在Spring Boot读取配置文件 #### 使用`@Value`注解读取属性 为了从`application.properties`或`application.yml`文件中读取特定的键值对,可以使用`@Value`注解。此方法适用于简单的场景,在定义字段上直接注入所需的配置项。 ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyConfigReader { @Value("${my.custom.property}") private String myCustomProperty; } ``` 该方式能够便捷地获取单个配置参数[^1]。 #### 利用`Environment`接口访问环境变量和配置数据 另一种更为灵活的方式是通过依赖注入获得`org.springframework.core.env.Environment`实例来动态查询配置信息: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContextInitializer; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class EnvironmentBasedConfigReader implements ApplicationContextInitializer<ConfigurableEnvironment> { private final Environment env; @Autowired public EnvironmentBasedConfigReader(Environment env){ this.env = env; } public void printProperties(){ System.out.println(env.getProperty("spring.datasource.url")); } } ``` 这种方式不仅限于`.properties`文件中的设置,还包括命令行参数、JVM系统属性等多种来源的数据[^2]。 #### 创建自定义Java Bean绑定多个配置项 对于拥有较多关联性强选项的情况,推荐创建专门用于封装这些配置的对象,并借助`@ConfigurationProperties`实现批量加载。这需要配合`@EnableConfigurationProperties`激活支持功能。 ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix="app.datasource") public class DataSourceSettings { private String url; private int maxConnections; // getters and setters... } ``` 上述代码片段展示了如何将前缀为`app.datasource.`下的所有匹配条目映射到指定对象员变量之上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值