my.properties

master.pool[0].id=poolId001
master.pool[0].endpoint=http://192.168.1.101:8080/v1
master.pool[1].id=poolId002
master.pool[1].endpoint=http://192.168.1.102:8080/v1
master.pool[2].id=poolId003
master.pool[2].endpoint=http://192.168.1.103:8080/v1

对应的Java POJO

package com.ssslinppp.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import java.util.List; @ConfigurationProperties(prefix = "master")
@PropertySource("classpath:my.properties")
@Component
public class PropertiesList {
private List<PoolConfiguration> pool; public static class PoolConfiguration { private String id; private String endpoint; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getEndpoint() {
return endpoint;
} public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
} @Override
public String toString() {
return "PoolConfiguration{" +
"id='" + id + '\'' +
", endpoint='" + endpoint + '\'' +
'}';
}
} public String endpoint(String poolId) {
if (CollectionUtils.isEmpty(pool))
return null; for (PoolConfiguration config : pool) {
if (config.getId().equals(poolId)) {
return config.getEndpoint();
}
}
return null;
} public List<PoolConfiguration> getPool() {
return pool;
} public void setPool(List<PoolConfiguration> pool) {
this.pool = pool;
} @Override
public String toString() {
return "PropertiesList{" +
"pool=" + pool +
'}';
}
}

测试

package com.ssslinppp.controller;

import com.ssslinppp.commons.properties.PropertiesGet;
import com.ssslinppp.model.PropertiesList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/prop")
public class PropController { @Autowired
private PropertiesList propertiesList; @RequestMapping("/printProps")
public String printProps(){
System.out.println(propertiesList.toString());
return propertiesList.toString();
} }

输出

PropertiesList{pool=[PoolConfiguration{id='poolId001', endpoint='http://192.168.1.101:8080/v1'}, PoolConfiguration{id='poolId002', endpoint='http://192.168.1.102:8080/v1'}, PoolConfiguration{id='poolId003', endpoint='http://192.168.1.103:8080/v1'}]}

【Properties】在Properties中配置List的更多相关文章

  1. Spring中配置和读取多个Properties文件--转

    public class PropertiesFactoryBeanextends PropertiesLoaderSupportimplements FactoryBean, Initializin ...

  2. application.properties多环境配置文件、jar包外部配置文件、配置项加密、程序中配置使用

    一.简介 spring boot项目application.properties文件存放及使用介绍 二.方法一多环境配置文件 我们一般都会有多个应用环境,开发环境.测试环境.生产环境,各个环境的配置会 ...

  3. Hibernate 中配置属性详解(hibernate.properties)

    Hibernate能在各种不同环境下工作而设计的, 因此存在着大量的配置参数.多数配置参数都 有比较直观的默认值, 并有随 Hibernate一同分发的配置样例hibernate.properties ...

  4. SpringBoot在logback.xml中读取application.properties中配置的日志路径

    1.在springboot项目中使用logback记录日志,在logback.xml中配置日志存储位置时读取application.properties中配置的路径,在 logback.xml中配置引 ...

  5. maven 打包时动态替换properties资源文件中的配置值

    pom build节点下面添加resource配置: <resources> <resource> <directory>src/main/resources/&l ...

  6. Java中如何获取spring中配置的properties文件内容

    有2种方式: 一. 1.通过spring配置properties文件 [java]  <bean id="propertyConfigurer"      class=&qu ...

  7. Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。

    Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可.

  8. 使用 application.properties 中配置的属性,举例:@Value("${server.port}")

    使用 application.properties 中配置的属性:@Value 注解. @RestController public class HelloWorldController { @Val ...

  9. Java 读取application.properties配置文件中配置

    实际开发中若需要读取配置文件application.properties中的配置,代码如下.例:读取配置文件中name属性配置值: 代码如下: import org.springframework.c ...

  10. log4j.properties 详解与配置步骤(转)

    找的文章,供参考使用 转自 log4j.properties 详解与配置步骤 一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR ...

随机推荐

  1. matlab学习(1)strsplit与strtok

    strsplit函数用法: <1>默认使用空格符分割,返回一个cell数组 <2>也可以指定第二个参数进行分割 <3>第二个参数也可以时包含多个分隔符的元胞数组 & ...

  2. SQLI DUMB SERIES-1

    less-1 (1) 可以看到提示输入ID,而且less-1题目也有提到GET,因此试试以下操作: http: 结果: http: 结果: 现在可知,“ ' ”并没有被过滤,因此可以进行以下操作: h ...

  3. CodeForces 727C

    zsy: Guess the Array Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...

  4. Win-Lin双系统重装Windows找回Linux启动

    第一系统Windows,第二系统Linux:Ubuntu18.10: 1. 重新安装Windows系统后,使用Ubuntu的安装光盘,或启动U盘启动电脑:2. 选择:Try Ubuntu ;3. 进入 ...

  5. Blender 曲线操作

    Curve (Bézier Versus NURBS)https://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Curve_and_Path_Mode ...

  6. python------模块定义、导入、优化 ------->Yaml, l模块

    一. yaml模块 用来做配置文件. 需要pip安装该包. 二. ConfigParser模块 用来生成和修改常见配置文件,在python3.x版本中更名为configparser. (什么是配置文件 ...

  7. git pull refusing to merge unrelated histories

    1. 简介 最近的项目开发环境我换到实验室的机器上了,毕竟是台式机,速度杠杠的.于是出现了一个问题,台式机上面的代码我笔记上的代码同步的问题.于是想到了Git这个强大的工具.关于git的简介我就不说了 ...

  8. 非系统服务如何随系统启动时自动启动(rc.local加了可执行权限,仍然没有生效)

    我们知道,例如我们直接yum 安装的httpd mysqld之类的服务可以直接systemctl enable mysql使服务自动启动,但是,我们应该关心的是但是的那部分 例如nginx,我的话,我 ...

  9. mysql之 CentOS系统针对mysql参数优化

    内核相关参数(/etc/sysctl.conf)  以下参数可以直接放到sysctl.conf文件的末尾: net.core.somaxconn = 65535 net.core.netdev_max ...

  10. node api 之:fs

    fs.readFile() 函数会缓存整个文件. 为了最小化内存占用,尽可能优先使用 fs.createReadStream().