【Spring Boot】配置文件@ConfigurationProperties,读取List、Map参数

List

application.properties

custom.config.config1.folders[0]=/root
custom.config.config1.folders[1]=/home/user1
custom.config.config1.folders[2]=/home/user2

Properties类

@ConfigurationProperties(prefix = "custom.config.config1")
public class Config1Properties{
	private List<String> folders;
	...
}

Map

application.properties

custom.config.config1.map.key1=value1
custom.config.config1.map.key2=value2
custom.config.config1.map.key3=value3
custom.config.config1.map.key4=value4
custom.config.config1.map.key5=value5

Properties类

@ConfigurationProperties(prefix = "custom.config.config1")
public class Config1Properties{
	private Map<String, String> map;
	...
}

Object

application.properties

custom.config.config1.server.host=host1
custom.config.config1.server.port=22
custom.config.config1.server.username=username1
custom.config.config1.server.password=password1

Properties类

@ConfigurationProperties(prefix = "custom.config.config1")
public class Config1Properties{
	private ServerProperties server;
	...
	public static class ServerProperties {
		private String host;
		private int port;
		private String username;
		private String password;
		...
	}
}

Object List

application.properties

custom.config.config1.servers[0].host=host1
custom.config.config1.servers[0].port=22
custom.config.config1.servers[0].username=username1
custom.config.config1.servers[0].password=password1
custom.config.config1.servers[1].host=host2
custom.config.config1.servers[1].port=22
custom.config.config1.servers[1].username=username2
custom.config.config1.servers[1].password=password2

Properties类

@ConfigurationProperties(prefix = "custom.config.config1")
public class Config1Properties{
	private List<ServerProperties> servers;
	...
	public static class ServerProperties {
		private String host;
		private int port;
		private String username;
		private String password;
		...
	}
}
@ConfigurationPropertiesSpring框架中的一个注解,用于将配置文件中的属性值绑定到Java对象上。当我们需要使用Map属性接收配置文件中的属性时,可以通过以下步骤实现: 1. 创建一个Java类,用于接收配置文件中的属性值,并使用@ConfigurationProperties注解标注该类。 2. 在该类中定义一个Map类型的属性,并提供对应的getter和setter方法。 3. 在配置文件中,使用"prefix.key=value"的格式来设置Map属性的值。 下面是一个示例代码: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Map; @Component @ConfigurationProperties(prefix = "example") public class ExampleProperties { private Map<String, String> mapProperty; public Map<String, String> getMapProperty() { return mapProperty; } public void setMapProperty(Map<String, String> mapProperty) { this.mapProperty = mapProperty; } } ``` 在上述示例中,我们使用@ConfigurationProperties注解将ExampleProperties类标记为一个配置类,并通过prefix属性指定了配置文件中属性的前缀为"example"。然后,我们定义了一个名为mapProperty的Map类型属性,并提供了对应的getter和setter方法。 在配置文件(例如application.properties)中,我们可以使用以下方式设置Map属性的值: ```properties example.mapProperty.key1=value1 example.mapProperty.key2=value2 ``` 这样,Spring框架会自动将配置文件中的属性值绑定到ExampleProperties类的mapProperty属性上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值