SpringBoot系列之@PropertySource用法简介

继上篇博客:SpringBoot系列之@Value和@ConfigurationProperties用法对比之后,本博客继续介绍一下@PropertySource注解的用法,通过上一篇博客的知识,可以知道@Value和@ConfigurationProperties都可以用于获取配置文件的属性值,不过有个细节容易被忽略,那就是,这两个注解在Springboot项目中都是获取默认配置文件的属性值,也就是application.yml或者application.properties的属性值

不过我们想要配属性的话,肯定都不能全都往默认配置文件里堆的,如果想引用其它配置文件的属性值,就可以使用本博客介绍的@PropertySource注解

新建一个user.properties的配置文件:

user.userName= root
user.isAdmin= true
user.regTime= 2019/11/01
user.isOnline= 1
user.maps.k1=v1
user.maps.k2=v2
user.lists=list1,list2
user.address.tel= 15899988899
user.address.name=上海

使用@PropertySource("classpath:user.properties")获取对应的properties文件,再用@ConfigurationProperties(prefix = "user")进行属性映射

package org.muses.jeeplatform.bean;

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
@PropertySource("classpath:user.properties")
@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 org.muses.jeeplatform.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 org.muses.jeeplatform;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.muses.jeeplatform.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@org.springframework.boot.test.context.SpringBootTest
public class SpringBootTest { @Autowired
User user; @Test
public void testConfigurationProperties(){
System.out.println(user);
} }

读取成功,就不用将配置写在默认的application.properties里了

User{userName='root', isAdmin=false, regTime=Fri Nov 01 00:00:00 CST 2019, isOnline=1, maps={k1=v1, k2=v2}, lists=[list1, list2], address=Address{tel='15899988899', name='上海市'}}

注意:对于@PropertySource注解,默认是不支持yml配置文件读取的,需要修改重写才可以

SpringBoot系列之@PropertySource用法简介的更多相关文章

  1. SpringBoot系列之@PropertySource读取yaml文件

    SpringBoot系列之@PropertySource支持yaml文件读取 最近在做实验,想通过@PropertySource注解读取配置文件的属性,进行映射,习惯上用properties都是测试没 ...

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

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

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

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

  4. SpringBoot源码学习系列之@PropertySource不支持yaml读取原因

    然后,为什么@PropertySource注解默认不支持?可以简单跟一下源码 @PropertySource源码: 根据注释,默认使用DefaultPropertySourceFactory类作为资源 ...

  5. SpringBoot系列之外部配置用法简介

    SpringBoot系列之外部配置用法简介 引用Springboot官方文档的说法,官方文档总共列举了如下用法: 1.Devtools global settings properties on yo ...

  6. SpringBoot系列之@Conditional注解用法简介

    SpringBoot系列之@Conditional注解用法简介 引用Spring官方文档的说法介绍一下@Conditional注解:Spring5.0.15版本@Conditional注解官方文档 @ ...

  7. SpringBoot系列之集成Thymeleaf用法手册

    目录 1.模板引擎 2.Thymeleaf简介 2.1).Thymeleaf定义 2.2).适用模板 3.重要知识点 3.1).th:text和th:utext 3.2).标准表达式 3.3).Thy ...

  8. SpringBoot系列之日志框架介绍及其原理简介

    SpringBoot系列之日志框架介绍及其原理简介 1.常用日志框架简介 市面上常用日志框架:JUL.JCL.jboss-logging.logback.log4j.log4j2.slf4j.etc. ...

  9. SpringBoot系列之日志框架使用教程

    目录 1.SpringBoot日志级别 1).日志级别简介 2).默认日志级别 3).配置日志级别 4).日志分组设置 2.SpringBoot日志格式设置 1).默认格式原理简介 2).默认日志格式 ...

随机推荐

  1. InnoDB On-Disk Structures(四)--Doublewrite Buffer (转载)

    转载.节选于 https://dev.mysql.com/doc/refman/8.0/en/innodb-doublewrite-buffer.html The doublewrite buffer ...

  2. phpMyAdmin开启IP地址登录

    根本没有其他文章说的那么麻烦,又是修改配置文件,又是修改首页文件.在根目录下有个libraries文件夹,进去有个config.default.php文件,修改里面的AllowArbitrarySer ...

  3. 女朋友会 Python 是多么可怕的一件事!

    ​ 阅读文本大概需要 8 分钟. 1 目 标 场 景 女朋友会 Python 是多么可怕的一件事! 一位朋友告诉忽略了一件事,假设女朋友会 Python 的话,那岂不是要翻车?如果是这样的话,女朋友发 ...

  4. 第2次作业-titanic数据集练习

    一.读入titanic.xlsx文件,按照教材示例步骤,完成数据清洗. titanic数据集包含11个特征,分别是: Survived:0代表死亡,1代表存活Pclass:乘客所持票类,有三种值(1, ...

  5. mySql中Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题

    报错信息 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'a.id' ...

  6. UTXO和Account模型一个都不能少

    UTXO对于非区块链从业人员来说可能比较陌生,UTXO的全称是Unspent Transaction Output,这中本聪在比特币中的一个天才设计.而Account模型就很常见,也很容易理解,你银行 ...

  7. JDK1.8源码分析02之阅读源码顺序

    序言:阅读JDK源码应该从何开始,有计划,有步骤的深入学习呢? 下面就分享一篇比较好的学习源码顺序的文章,给了我们再阅读源码时,一个指导性的标志,而不会迷失方向. 很多java开发的小伙伴都会阅读jd ...

  8. Git - Git推送本地分支到远程分支报错(! [rejected] non-fast-forward)的解决办法

    一般都是冲突造成的,解决方案执行如下命令(dev为分支名称): git fetch origin dev  #获取远程 dev 分支的修改 git merge origin dev       #合并 ...

  9. C# winfrom调用摄像头扫描二维码(完整版)

    前段时间看到一篇博客,是这个功能的,参考了那篇博客写了这个功能玩一玩,没有做商业用途.发现他的代码给的有些描述不清晰的,我就自己整理一下发出来记录一下. 参考博客链接:https://www.cnbl ...

  10. C#中获取指定目录下所有目录的名称、全路径和创建日期

    场景 指定一个路径,根据这个父级路径获取此目录下所有目录的名称.全路径.创建日期等信息. 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众 ...