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系列之配置文件占位符使用的更多相关文章

  1. 3springboot:springboot配置文件(配置文件占位符、Profile、配置文件的加载位置)

    1.配置文件占位符 RaandomValuePropertySourcr:配置文件可以使用随机数     ${random.value}    ${random.int}  ${random.long ...

  2. Springboot配置文件占位符

    一.配置文件占位符 1.application.properties server.port=8088 debug=false product.id=ID:${random.uuid} product ...

  3. SpringBoot系列之配置文件加载位置

    SpringBoot系列之配置文件加载位置 SpringBoot启动会自动扫描如下位置的application.properties或者application.yml文件作为Springboot的默认 ...

  4. 3、springboot配置文件占位符

    RandomValuePropertySource:配置文件中可以使用随机数 ${random.value}.${random.int}.${random.long}.${random.int(10) ...

  5. SpringBoot学习(四)——配置文件占位符

    RandomValuePropertySource:配置文件中可以使用随机数 ${Random.value}  ${random.int}, ${random.long}, ${random.int( ...

  6. SpringBoot——配置文件占位符

    在配置文件中采用: ${random.int} 获取一个随机值.

  7. SpringBoot系列之学习教程汇总

    对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 一.配置篇 SpringBoot系列之@PropertySource读取yaml文件     >> source down ...

  8. SpringBoot系列之从入门到精通系列教程

    对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 Spring框架:作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多, ...

  9. 聊聊 SpringBoot 中的两种占位符:@*@ 和 ${*}

    前言 在 SpringBoot 项目中,我们经常会使用两种占位符(有时候还会混用),它们分别是: @*@ ${*} 如果我们上网搜索「SpringBoot 的占位符 @」,大部分答案会告诉你,Spri ...

随机推荐

  1. Appium(二):Node.js下载与安装、非GUI版本appium下载与安装、GUI版本appium下载与安装

    1. 下载并安装Node.JS 进入官网:https://nodejs.org/en/. 由于我们是新手嘛,所以肯定是越稳定越好啦,所以选择下载LTS版本. 进入文件下点击文件就进入安装界面了,点击n ...

  2. go 语言学习图解

  3. Objective-C中 #include 和 #import 的区别

    由于 Objective-C 兼容 C 语言,所以导入文件时, 可以使用 #include,也可以使用 #import (Objective-C 新增的) 如: #include <stdio. ...

  4. LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees

    LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees 题目: 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两 ...

  5. 2019年全国高校计算机能力挑战赛初赛C++语言解答

    1; 题目一 16.电商促销某种商品时,希望通过打折鼓励消费者组团消费.已知,团队消费金额=该团的人数和*商品单价.打折规则如下:当组团消费者数量不满50人时,商品消费金额没有折扣:消费者数量大于等于 ...

  6. DOM介绍以及使用方法

    DOM的基本讲解 一.DOM (Document Object Model)文档对象模型 1.有属性有方法 var person = { name:'派大星', fav:function(){ } } ...

  7. Linux系统目录结构知识

    1.Linux目录结构: 逻辑上所有的目录只有一个顶点 /(根),所有目录的起点.根下面是一个类似倒挂的树的层次结构. 目录的结构和分区设备是没有关系的,也就是不同的目录可以跨越不同的磁盘设备或分区. ...

  8. Linux平台 Oracle 19c RAC安装Part3:DB配置

    Linux平台 Oracle 19c RAC安装Part3:DB配置 四.DB(Database)配置 4.1 解压DB的安装包 4.2 DB软件配置 4.3 ASMCA创建磁盘组 4.4 DBCA建 ...

  9. ActiveMQ学习总结------Spring整合ActiveMQ 04

    通过前几篇的学习,相信大家已经对我们的ActiveMQ的原生操作已经有了个深刻的概念, 那么这篇文章就来带领大家一步一步学习下ActiveMQ结合Spring的实战操作 注:本文将省略一部分与Acti ...

  10. 理解并运用TP5.1-Facade

    1.内容介绍 深入解析tp5.1与laravel 中Facade底层原理实现 1. 什么是Facade 2. 为什么需要有什么好处 3.  Facade实现原理 4. 功能实现. 5. 容器注入 2. ...