两个注解都可以读取properties文件或者xml文件,二者可以单独使用也可以结合使用。

一、 @PropertySource结合@Value读取指定配置文件

1、准备一个properties文件放在项目下

demo.name=zhangsan
demo.age=25
demo.address=hangzhou

2、使用@PropertySource读取数据

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Data
@Component
@PropertySource("classpath:demo.properties")
public class ReadConfByValue {@Value("${demo.name}")private String name;@Value("${demo.age}")private String age;@Value("${demo.address}")private String address;}

3、查看效果
让读取到的数据在springboot启动时控制台显示

import com.gao.readconf.ReadConfByValue;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
@Log4j2
public class InitRunner implements CommandLineRunner {@Autowiredprivate ReadConfByValue conf;@Overridepublic void run(String... args) throws Exception {log.info(conf.toString());}
}

4、验证yml文件能否读取
准备一个demo.yml文件

demo:name: lisiage: 130address: shenzhen

将@PropertySource(“classpath:demo.yml”)改为,运行报错。源码给了value的说明,指明@PropertySource仅支持读取properties文件与xml文件。

二、 @PropertySource结合Environment读取配置文件

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;@Component
@Log4j2
@PropertySource("classpath:demo.properties")
public class ReadConfByEnvironment {@Autowiredpublic Environment environment;@Beanpublic void toReadEnvConf(){log.error("**********通过Env获取数据*********");log.error(environment.getProperty("demo.name"));log.error(environment.getProperty("demo.age"));log.error(environment.getProperty("demo.address"));log.error("**********End*********");}
}

三、 @PropertySource读取多个properties文件

@Data
@Component
@PropertySource(value = {"classpath:demo.properties","classpath:demoadd.properties"})
public class Read2ConfByValue {@Value("${demo.name}")private String name;@Value("${demo.age}")private String age;@Value("${demo.address}")private String address;@Value("${demo1.birth}")private String birth;@Value("${demo1.phone}")private String phone;@Value("${demo1.hobby}")private String hobby;
}

修改对应的InitRunner并运行项目

四、@PropertySource 结合 @ConfigurationProperties读取配置文件

1、准备另一个properties文件demoadd.properties:

demo1.birth=2020.9.2
demo1.phone=13020122365
demo1.hobby=codingdemo2.name=lisi
demo2.age=120
demo2.address=beijing

2、@PropertySource(“classpath:demoadd.properties”)作用是指定文件,@ConfigurationProperties(prefix=“demo1”)限定前缀为demo1,前缀为demo2的字段读取不到。两者结合使用时相当于读取到@PropertySource指定的文件后,再通过@ConfigurationProperties进行数据过滤,仅获取指定前缀的数据。

@Data
@Component
@PropertySource("classpath:demoadd.properties")
@ConfigurationProperties(prefix="demo1")
public class ReadConfByConfigurationProperties {private String birth;private String phone;private String hobby;}

3、修改InitRunner,启动项目

@Component
@Log4j2
public class InitRunner implements CommandLineRunner {@Autowiredprivate ReadConfByValue conf;@Autowiredprivate ReadConfByConfigurationProperties readconf;@Overridepublic void run(String... args) throws Exception {log.info(conf.toString());log.info(readconf.toString());}
}

4、如何读取多前缀数据?

从源码中看出,prefix并不是数组形式,注释中也说明了A valid prefix,表明不支持多前缀。

五、 @ConfigurationProperties单独使用

1、不通过@PropertySource指定固定文件,仅指定前缀,仍能读取到demoadd.properties中的数据

@Data
@Component
//@PropertySource("classpath:demoadd.properties")
@ConfigurationProperties(prefix="demo1")
public class ReadConfByConfigurationProperties {private String birth;private String phone;private String hobby;
}

2、那如果指定的prefix重复呢?
修改demo.properties文件,添加demo1属性,执行项目

demo.name=zhangsan
demo.age=25
demo.address=hangzhoudemo1.birth=1000.8.12
demo1.phone=18812568532
demo1.hobby=playing

本以为会报错,,谁知结果没变,还是正常输出

这不会是所有properties文件都读一遍,保留最后/或者第一次读取的文件中数据吧,
把demo.properties名字改了个遍结果也还是一动不动。。。(此处疑问保留)

3、@ConfigurationProperties能不能读取yml文件
试了试读取demo.yml,读取失败,字段全为null
如果是application.yml呢,在application.yml中添加:

demo:name: lisiage: 180address: shenzhen


执行项目

看到结果呵呵。。能读到,并且其他文件demo.properties、demoadd.properties里的demo前缀字段数据全给替换了

六、@PropertySources

实现多个配置文件的读取,可是@PropertySource本来就有这个功能啊,为什么还要搞个这?
源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PropertySources {PropertySource[] value();}

使用方法:

@Data
@Component
@PropertySources(value = {@PropertySource("classpath:demo.properties"),@PropertySource("classpath:demoadd.properties")})
public class ReadConfByValue {@Value("${demo.name}")private String name;@Value("${demo.age}")private String age;@Value("${demo.address}")private String address;}

@PropertySource与@ConfigurationProperties多种方式读取配置文件详解,附带@PropertySources使用说明相关推荐

  1. java读取配置文件详解

    目录 使用类加载器加载配置文件 ServletContext方式 读取web.xml配置 前言: 1 这是一个java读取配置文件的方法集合,只有常见的方式 2 围绕的内容大部分是读取properti ...

  2. 多种方式读取文件内容

    2019独角兽企业重金招聘Python工程师标准>>> import java.io.*; /**  *  * @author wxp  * Created by Administr ...

  3. Spring Boot 2.x基础教程:配置文件详解

    在快速入门一节中,我们轻松的实现了一个简单的RESTful API应用,体验了一下Spring Boot给我们带来的诸多优点,我们用非常少的代码量就成功的实现了一个Web应用,这是传统的Spring应 ...

  4. Spring Boot (4)---配置文件详解

    Spring2.0 Boot配置文件详解 配置文件说明 Spring Boot 配置文件允许为同一套应用,为不同的环境用不同的配置文件.比如开发环境.测试环境.生成环境.你可以用 properties ...

  5. MyBatis复习笔记2:配置文件详解

    配置文件详解 属性(properties) MyBatis可以使用 properties 来引入外部 properties 配置文件的内容 resource:引入类路径下的资源 url:引入网络路径或 ...

  6. (转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

  7. SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    springboot采纳了建立生产就绪Spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

  8. SpringBoot非官方教程 | 第二篇:SpringBoot配置文件详解

    springboot采纳了建立生产就绪Spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

  9. python实现单例模式的几种方式_基于Python中单例模式的几种实现方式及优化详解...

    单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...

最新文章

  1. 如何使用YUM列出包的内容?
  2. 有向无环图的拓扑排序
  3. bzoj4237稻草人
  4. 【学习笔记】信息学竞赛中的概率与期望小结
  5. applicationproperties不是小叶子_为何“砂糖桔”是带着叶子出售?原来其中有着“猫腻”,涨知识了...
  6. OpenCV编译安装
  7. 蔚来公布西安ES8自燃事故原因 果然是电池的锅...
  8. 斗鱼mac html5,斗鱼直播伴侣ios苹果版使用教程_苹果版斗鱼直播伴侣怎么用_3DM手游...
  9. 滴滴如何调度_滴滴智能调度浅析
  10. h5画三角形_HTML5怎么画三角形?
  11. 计算机用户界面的设计,计算机软件用户界面设计的基本原则
  12. mysql 按比例计算排名_计算MS SQL中的百分比排名
  13. Arduino学习(六) 继电器实验
  14. JSP的四大作用域及属性范围
  15. Android 常用正则表达式,阿里巴巴内部spring宝典意外流出
  16. ActiViz学习点滴(五)——坐标变换
  17. 超火 3D 照片墙,你学废了吗?
  18. Java 面向对象 1 《 new 一个女朋友》
  19. 下载安装和汉化Eclipse(详细)
  20. App新品推广相关知识

热门文章

  1. 数据预处理|关于标准化和归一化的一切
  2. 2020 年值得再读一遍的网易云信技术干货 | 上篇
  3. 【易创课堂】第4期深圳开讲,还有大包好礼!
  4. PHP开发电脑网站支付宝支付详细流程(沙箱测试篇)
  5. Spark Streaming实时流处理学习
  6. Oracle 11g Express Edition 安装及常见问题
  7. 企业级 SpringBoot 教程 (十七)上传文件
  8. Django Sqlite3 数据库向MySQL迁移
  9. 使用shell脚本实现自动SSH互信功能
  10. java神雕侠侣1古墓情缘游戏攻略_《神雕侠侣》古墓派平民玩法攻略