SpringBoot系列之配置文件占位符使用
SpringBoot系列之配置文件占位符使用
Springboot占位符支持的有随机数和配置的值等等,本博客主要介绍的是随机数和获取属性配置值的简单用法
- 随机数获取
支持的写法有:
${random.value}、${random.int}、${random.long}、${random.uuid}
${random.int(10)}、${random.int(1024,65536)} .etc
- 获取属性配置的值
user.userName= root(${user.address.tel})
user.address.tel= 15899988899
ok,写个例子实践一下
user.properties
user.userName= root(${user.address.tel})
user.isAdmin= true
user.regTime= 2019/11/01
user.isOnline= 1
user.maps.k1=${random.int}
user.maps.k2=v2
user.lists=${random.uuid},${random.value}
user.address.tel= 15899988899
user.address.name=上海浦东区
User类
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component//将组件添加到Spring容器
@PropertySource(value = "classpath:user.properties",encoding = "utf-8")
@ConfigurationProperties(prefix = "user")//属性进行映射
public class User {
private String userName;
private boolean isAdmin;
private Date regTime;
private Long isOnline;
private Map<String,Object> maps;
private List<Object> lists;
private Address address;
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", isAdmin=" + isAdmin +
", regTime=" + regTime +
", isOnline=" + isOnline +
", maps=" + maps +
", lists=" + lists +
", address=" + address +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean admin) {
isAdmin = admin;
}
public Date getRegTime() {
return regTime;
}
public void setRegTime(Date regTime) {
this.regTime = regTime;
}
public Long getIsOnline() {
return isOnline;
}
public void setIsOnline(Long isOnline) {
this.isOnline = isOnline;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address类::
package com.example.springboot.properties.bean;
/**
* <pre>
*
* </pre>
*
* @author nicky
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期: 2019年11月03日 修改内容:
* </pre>
*/
public class Address {
private String tel;
private String name;
@Override
public String toString() {
return "Address{" +
"tel='" + tel + '\'' +
", name='" + name + '\'' +
'}';
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
写个junit测试类获取
package com.example.springboot.properties;
import com.example.springboot.properties.bean.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
@SpringBootTest
class SpringbootPropertiesConfigApplicationTests {
@Autowired
User user;
@Test
public void testConfigurationProperties(){
System.out.println(user);
}
}
User{userName='root(15899988899)', isAdmin=false, regTime=Fri Nov 01 00:00:00 CST 2019, isOnline=1, maps={k2=v2, k1=366784341}, lists=[2c9f4a24-23d9-4507-b446-f8e92c82608f, 0c61b1f7321714f374ba16f88c1bb587], address=Address{tel='15899988899', name='上海浦东区'}}
SpringBoot系列之配置文件占位符使用的更多相关文章
- 3springboot:springboot配置文件(配置文件占位符、Profile、配置文件的加载位置)
1.配置文件占位符 RaandomValuePropertySourcr:配置文件可以使用随机数 ${random.value} ${random.int} ${random.long ...
- Springboot配置文件占位符
一.配置文件占位符 1.application.properties server.port=8088 debug=false product.id=ID:${random.uuid} product ...
- SpringBoot系列之配置文件加载位置
SpringBoot系列之配置文件加载位置 SpringBoot启动会自动扫描如下位置的application.properties或者application.yml文件作为Springboot的默认 ...
- 3、springboot配置文件占位符
RandomValuePropertySource:配置文件中可以使用随机数 ${random.value}.${random.int}.${random.long}.${random.int(10) ...
- SpringBoot学习(四)——配置文件占位符
RandomValuePropertySource:配置文件中可以使用随机数 ${Random.value} ${random.int}, ${random.long}, ${random.int( ...
- SpringBoot——配置文件占位符
在配置文件中采用: ${random.int} 获取一个随机值.
- SpringBoot系列之学习教程汇总
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 一.配置篇 SpringBoot系列之@PropertySource读取yaml文件 >> source down ...
- SpringBoot系列之从入门到精通系列教程
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 Spring框架:作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多, ...
- 聊聊 SpringBoot 中的两种占位符:@*@ 和 ${*}
前言 在 SpringBoot 项目中,我们经常会使用两种占位符(有时候还会混用),它们分别是: @*@ ${*} 如果我们上网搜索「SpringBoot 的占位符 @」,大部分答案会告诉你,Spri ...
随机推荐
- 百度BAE专业版申购SSL证书
这几天开发了一个小程序,本来想放到BAE基础版的,但是基础版现在不能再新增项目了,想了一下,出点血,配了个专业版.但是专业版的SSL证书是需要配的,而小程序必须使用https,所有必须申请一个证书.在 ...
- [Go] golang中的包管理
在配置了环境变量$GOPATH后,比如下面这个路径 export GOPATH=/mnt/f/ubuntu/goProject 在这个路径下面会有这几个目录 在src目录下放着我的源码比如: 在同一个 ...
- fiddler 进行Android/IOS代理配置抓包
1.准备:Android+IOS设备 下载:fiddler抓包工具,不是最新版的链接: 链接:https://pan.baidu.com/s/1BaBfu2H4xgpsh1wmkfC8aQ ...
- mysql windows 安装5.7
E:\MySQL\MySQLServer57\bin>net start mysqlMySQL 服务无法启动. 解决办法: 手动在安装目录创建文件 my.ini [mysql] # 设置mysq ...
- IM开发基础知识补课(七):主流移动端账号登录方式的原理及设计思路
1.引言 在即时通讯网经常能看到各种高大上的高并发.分布式.高性能架构设计方面的文章,平时大家参加的众多开发者大会,主题也都是各种高大上的话题——什么5G啦.AI人工智能啦.什么阿里双11分分钟多少万 ...
- css实现左右两个div等高
提出问题: 现在有两个div,但是两个div里面内容多少不确定,可能左边多,可能右边多,css要如何设置可以保证左右两边的div等高呢? 解决方案: 每个div使用display:table-cell ...
- Java入门系列之StringBuilder、StringBuffer(三)
前言 上一节我们讲解了字符串的特性,除了字符串类外,还有两个我们也会经常用到的类,那就是StringBuffer和StringBuilder.因为字符串不可变,所以我们每次对字符串的修改比如通过连接c ...
- Implement Property Value Validation in Code 在代码中实现属性值验证(XPO)
This lesson explains how to set rules for business classes and their properties. These rules are val ...
- 38-docker managed volume
docker managed volume 与 bind mount 在使用上的最大区别是不需要指定 mount 源,指明 mount point 就行了.还是以 httpd 容器为例: 我们通过 - ...
- 解决brew update很慢
一般遇到这种问题,就跟墙有关啦,需要更换源. 用清华的源就非常好,去清华镜像的官网看一下说明,https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/ $ ...