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. input标签(图像域和隐藏域)

    图像域(图像提交按钮): <input type="image" name="..." src="imageurl"  /> 隐 ...

  2. lecture4特征提取-七月在线-cv

    霍夫变换 http://blog.csdn.net/sudohello/article/details/51335237 http://blog.csdn.net/glouds/article/det ...

  3. 矩阵快速幂(以HDU1757为例)

    对于数据量大的求余运算,在有递推式的情况下,可以构造矩阵求解. A - A Simple Math Problem Lele now is thinking about a simple functi ...

  4. PTA——简单计算器

    PTA 7-20 简单计算器 #include<stdio.h> int main() { int a,b; char c; scanf("%d",&a); w ...

  5. C++学习(十五)(C语言部分)之 数组二

    数组大纲 多维数组 二维数组 重点 (三位以上基本不会用到) 都是用来存数据 一个班有20个人 可以用一维数组存20个人的年龄 int age[20]; 一个年级10个班 每个班20人 int age ...

  6. 【HDOJ1043】【康拓展开+BFS】

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 Eight Time Limit: 10000/5000 MS (Java/Others)    Memo ...

  7. maven 总结整理(二)——download source code

    当我们用maven下载jar包时,有时希望下载jar包的源代码,此时可以在pom.xml文件中,进行设置. <build>    <finalName>WebProject&l ...

  8. sql优化学习(一)

    找到一套好的sql优化视频,地址如下: http://www.ppvke.com/10355.html p.s. 我在百度搜索半天都是些乱七八糟的东西,我用google搜索后,立刻就发现上面的视频地址 ...

  9. spring获取jdbc链接底层原理

    获取连接池的连接二种逻辑 1.一个事务中,一个连接 (底层逻辑:threadlocal存储  里面是map: key是数据源,value:链接) map存储应该是为多数据源使用的2.没有事务的serv ...

  10. linux 控制结构

    一.if 注: 格式1.格式2:一个条件一个命令: 格式3:一个条件两个命令: 格式4:两个条件三个命令,注意条件的写法. 例1: #!/bin/sh#ifTest#to show the metho ...