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 配 ...
随机推荐
- 在WCF程序中动态修改app.config配置文件
今天在个WCF程序中加入了修改配置文件的功能.我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google搜了一下,在园子里的文章动态修改App.Config 和w ...
- 7、Shiro加密和加盐
这里我们以md5加密方法举例,首先我们写一个main方法测试我们的密码经过md5加密之后的得到什么样的字符串: /** * 书写方法测试Md5Hash将密码“houru”加密之后的密文 * 但是仅仅加 ...
- 1、Shiro简介以及整体架构
1.Shiro概念和作用: 利用shiro可以快速完成权限管理模块的开发 Spring的官网也是用Shiro做安全管理的... Shiro整体架构: 可能你感觉上面的图片很乱,但是你一定要先大体有个印 ...
- Octavia 的实现与分析(OpenStack Rocky)
目录 文章目录 目录 Octavia 基本对象概念 基本使用流程 软件架构 服务进程清单 代码结构 loadbalancer 创建流程分析 network_tasks.AllocateVIP netw ...
- Delphi XE2 之 FireMonkey 入门(34) - 控件基础: TFmxObject: 克隆对象
Delphi XE2 之 FireMonkey 入门(34) - 控件基础: TFmxObject: 克隆对象 有两个和克隆相关的方法: Clone().CloneChildFromStream(). ...
- Gradle之Android Gradle Plugin 主要流程分析(二)
[Android 修炼手册]Gradle 篇 -- Android Gradle Plugin 主要流程分析 预备知识 理解 gradle 的基本开发 了解 gradle task 和 plugin ...
- C#采集:图灵机器人信息
Dictionary<string, string> users = new Dictionary<string, string>(); users.Add("use ...
- [Git] 016 远程仓库篇 第三话 删除远程仓库
1. 来到自己的 GitHub 页面,先点右上角自己的头像,再点 "Your profile" 2. 选择自己的某个远程仓库,我选 "git_skills" 3 ...
- urllib基本库的使用
get方法的学习 1import urllib.request 2import ssl 3#设置全局证书 4ssl._create_default_https_context = ssl._creat ...
- [AGC040B]Two Contests
Description 给出若干条线段 \((L[i], R[i])\) ,把他们分成两个非空的集合,最大化集合内线段交的和. \(n\le 10 ^ 5\) Solution 考虑最小的一个右端点 ...