【Properties】在Properties中配置List
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的更多相关文章
- Spring中配置和读取多个Properties文件--转
public class PropertiesFactoryBeanextends PropertiesLoaderSupportimplements FactoryBean, Initializin ...
- application.properties多环境配置文件、jar包外部配置文件、配置项加密、程序中配置使用
一.简介 spring boot项目application.properties文件存放及使用介绍 二.方法一多环境配置文件 我们一般都会有多个应用环境,开发环境.测试环境.生产环境,各个环境的配置会 ...
- Hibernate 中配置属性详解(hibernate.properties)
Hibernate能在各种不同环境下工作而设计的, 因此存在着大量的配置参数.多数配置参数都 有比较直观的默认值, 并有随 Hibernate一同分发的配置样例hibernate.properties ...
- SpringBoot在logback.xml中读取application.properties中配置的日志路径
1.在springboot项目中使用logback记录日志,在logback.xml中配置日志存储位置时读取application.properties中配置的路径,在 logback.xml中配置引 ...
- maven 打包时动态替换properties资源文件中的配置值
pom build节点下面添加resource配置: <resources> <resource> <directory>src/main/resources/&l ...
- Java中如何获取spring中配置的properties文件内容
有2种方式: 一. 1.通过spring配置properties文件 [java] <bean id="propertyConfigurer" class=&qu ...
- Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。
Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可.
- 使用 application.properties 中配置的属性,举例:@Value("${server.port}")
使用 application.properties 中配置的属性:@Value 注解. @RestController public class HelloWorldController { @Val ...
- Java 读取application.properties配置文件中配置
实际开发中若需要读取配置文件application.properties中的配置,代码如下.例:读取配置文件中name属性配置值: 代码如下: import org.springframework.c ...
- log4j.properties 详解与配置步骤(转)
找的文章,供参考使用 转自 log4j.properties 详解与配置步骤 一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR ...
随机推荐
- FTP 文件传输协议的两种模式:主动模式和被动模式
https://www.cnblogs.com/i-shu/articles/4905266.html FTP支持两种模式,一种方式叫做Standard (也就是 PORT方式,主动方式),一种是 P ...
- 20155219 2016-2017-2 《Java程序设计》第2周学习总结
20155219 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 类型:整数,字节(需要逐字节处理数据时,如图像处理,编码处理等,会使用byte类型),浮点数 ...
- 同一个页面引用不同版本jquery库
(如有打扰,请忽略)阿里云ECS大羊群,2U4G低至1.4折,限实名新用户,需要的点吧https://promotion.aliyun.com/ntms/act/vm/aliyun-group/tea ...
- 2017.4.7 Sprng MVC工作流程描述图
图一: 图二: Spring工作流程描述 1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获: 2. Dispa ...
- 【Wannafly挑战赛14C可达性】【Tarjan缩点】
链接:https://www.nowcoder.com/acm/contest/81/C来源:牛客网 题目描述 给出一个 0 ≤ N ≤ 105 点数.0 ≤ M ≤ 105 边数的有向图,输出一个尽 ...
- vue的理解
vue提供的MVVM框架模式的数据双向绑定,实现了HTML和js的代码分离,提高代码的维护性 vue.js的核心思想包括:数据驱动和组件化思想. 如果没有中间的ViewModel则关系图编程下面所示: ...
- MySQL数据库-数据表、以及列的增删改查
1.创建一个表 CREATE(创建) TABLE(表) ENGINE(引擎) ENGINE=INNODB(引擎)还有很多类引擎,这里只是简单的提一下INNODB引擎,INNODB引擎支持事务(回滚), ...
- iterm2字符输入换行遮挡问题
来源:http://wonderffee.github.io/blog/2013/08/15/solve-new-line-problem-in-terminal/ .bash_profile中 进行 ...
- Centos7安装teamviewer 32/64位
centos7安装teamviewer teamviewer安装包,自己去官网下吧,不用找了,估摸着就没有Centos7 64位的 所以直接下centos teamviewer 32位那个rpm包吧 ...
- day 59 Bootstrap学习
---恢复内容开始--- 简介 栅格系统用于通过一系列的行(row)与列(column)的组合来创建页面布局,你的内容就可以放入这些创建好的布局中.下面就介绍一下 Bootstrap 栅格系统的工作原 ...