【Spring Boot】配置文件@ConfigurationProperties,读取List、Map参数
程序员文章站
2024-01-09 18:25:28
...
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;
...
}
}