Springboot读取properties配置文件数据
一.使用@ConfigurationProperties来读取
1、Coffer entity
@Configuration
@ConfigurationProperties(prefix = "coffer")
@PropertySource("classpath:config/coffer.properties")
public class Coffer {
private String brand;
private Double length;
private Double width;
private Double height; //省略了get/set方法
private String[] contains;
private ArrayList<Fruit> fruits;
private HashMap<String,Object> map;
}
2、coffer.properties
coffer.brand=Camel
coffer.length=100.00
coffer.width=80.00
coffer.height=60.00
coffer.contains[0]=Raincoat
coffer.contains[1]=trousers
coffer.contains[2]=hat
coffer.contains[3]=glove
coffer.contains[4]=scarf
coffer.contains[5]=hood
coffer.fruits[0].fruitName=apricot
coffer.fruits[0].fruitColor=yellow
coffer.fruits[1].fruitName=plum
coffer.fruits[1].fruitColor=green
coffer.fruits[2].fruitName=pineapple
coffer.fruits[2].fruitColor=yellow
coffer.fruits[3].fruitName=watermelon
coffer.fruits[3].fruitColor=green
coffer.fruits[4].fruitName=strawberry
coffer.fruits[4].fruitColor=red
coffer.map.name=xiaomao
coffer.map.age=22
coffer.map.gender=female
3、springbootApplicationTest
@SpringBootTest
class SpringbootApplicationTests { @Autowired
private ApplicationContext ioc; @Autowired
private Coffer coffer; @Test
public void springbootTest(){
System.out.println(coffer);
}
}
4、result
Coffer{
brand='Camel',
length=100.0,
width=80.0,
height=60.0,
contains=[Raincoat, trousers, hat, glove, scarf, hood],
fruits=[
Fruit{fruitName='apricot', fruitColor='yellow'},
Fruit{fruitName='plum', fruitColor='green'},
Fruit{fruitName='pineapple', fruitColor='yellow'},
Fruit{fruitName='watermelon', fruitColor='green'},
Fruit{fruitName='strawberry', fruitColor='red'}
],
map={age=22, gender=female, name=xiaomao}}
二、使用@Value来读取
在springTest中无法使用@Value来读取配置属性,需要放到Controller中去读取
@PropertySource("classpath:config/coffer.properties")
@RestController
public class SpringbootController { @Value("${coffer.brand}")
private String brand;
@Value("${coffer.height}")
private Double height; @RequestMapping("/test")
public String springbootTest() {
return brand+"====="+height;
}
三、@ConfigurationProperties和@Value的区别
@Value获取值和@ConfigurationProperties获取值比较 @ConfigurationProperties @Value 功能 批量注入配置文件中的属性 一个个指定
松散绑定(松散语法) 支持 不支持
SpEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持 配置文件yml还是properties他们都能获取到值; 如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value; 如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
Springboot读取properties配置文件数据的更多相关文章
- SpringBoot 读取properties配置文件 @Value使用 中文乱码问题
一,idea中配置文件中文乱码问题 使用idea开发,读取properites配置文件 配置: #app 菜单 #没有限制,所有人都可访问的菜单 menu.unlimited=订单审批,现场尽调,合作 ...
- Java读取Properties配置文件
1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,使用键值对的形式来保存属性集.不过Properties的键和值都是字符串 ...
- spring 框架的xml文件如何读取properties文件数据
spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...
- jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...
- javaweb 读取properties配置文件参数
场景1:在servlet中读取properties配置文件参数 protected void doGet(HttpServletRequest request, HttpServletResponse ...
- SpringBoot读取外部配置文件的方法
SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...
- 读取.properties配置文件
方法1 public class SSOUtils { protected static String URL_LOGIN = "/uas/service/api/login/info&q ...
- java读取properties配置文件总结
java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...
- Java 读取 .properties 配置文件的几种方式
Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配 ...
随机推荐
- LinkedList Stack
- 分析邮件日志从每秒处理3个结果到每秒处理800个结果过程(grep提速)
由于业务需要,对EDM发出的邮件日志进行分析处理,我要做的是预处理,把posfix杂乱无章的日志中找到我需要的数据. 我用javamail发的邮件,发送邮件时获取到是一个messageId,形如:21 ...
- SPEL 表达式解析
Spring Expression Language 解析器 SPEL解析过程 使用 ExpressionParser 基于 ParserContext 将字符串解析为 Expression, Exp ...
- 快速编写 <a> ————CSS3
a{ text-decoration:none; } a:link{ color:white; } a:visited { color:white; } a:hover { color:blue; } ...
- Java ——集合框架 list lambda set map 遍历方法 数据结构
本节重点思维导图 集合框架 有序无序:元素放入的顺序与取出的顺序是否一致,一致即为有序,不一致即无序. List:允许重复.有序 ArrayList:长度可变的数组,遍历速度快 LinkedList: ...
- Html5 Canvas斗地主游戏
过完年来公司,没什么事,主管说研究下html5 游戏,然后主管就给了一个斗地主的demo,随后我就开始看代码, 现在我看了html5以及canvas相关知识和斗地主的demo后,自己用demo上的素材 ...
- maven spark Scala idea搭建maven项目的 pom.xml文件配置
1.pom.xml文件配置,直接上代码. <?xml version="1.0" encoding="UTF-8"?> <project xm ...
- Service-Oriented Architecture,SOA(转)
http://blog.csdn.net/WOOSHN/article/details/8036910 介绍: IT体系结构已非常成熟,它是一种成功处理典型IT问题的方法.体系结构中一个受到很大重视且 ...
- 分享一个linux系统中采用嵌套for循环比较两个数组内容,并输出相同值的shell脚本
#!/bin/bash array1=(1 3 5 6 7 9) array2=(3 4 9) echo array1=${array1[@]} echo array2=${array2[@]} fo ...
- 部署CM集群首次运行报错:Formatting the name directories of the current NameNode.
1. 报错提示 Formatting the name directories of the current NameNode. If the name directories are not emp ...