一、PropertySource 简介二、@PropertySource与Environment读取配置文件三、@PropertySource与@Value读取配置文件@Value 基本使用@Value 高级用法四、@PropertySource 与 @Import 一、PropertySource 简介 org.springframework.context.annotation.PropertySource 二、@PropertySource与Environment读取配置文件 此注解@PropertySource 为Spring 中的 Environment提供方便和声明机制,通常与Configuration 引入PropertySource,注入Environment,然后就能用environment 获取配置文件中的value值。 四、@PropertySource 与 @Import @Import 可以用来导入 @PropertySource 标注的类,具体代码如下: 新建一个PropertySourceReadApplication
本文链接:https://blog.csdn.net/u014427391/article/details/102931513 SpringBoot系列之@PropertySource用法简介 继上篇博客 :SpringBoot系列之@Value和@ConfigurationProperties用法对比之后,本博客继续介绍一下@PropertySource注解的用法,通过上一篇博客的知识,可以知道@Value application.yml或者application.properties的属性值 不过我们想要配属性的话,肯定都不能全都往默认配置文件里堆的,如果想引用其它配置文件的属性值,就可以使用本博客介绍的@PropertySource user.maps.k1=v1 user.maps.k2=v2 user.lists=list1,list2 user.address.tel= 15899988899 user.address.name=上海 使用@PropertySource 1, maps={k1=v1, k2=v2}, lists=[list1, list2], address=Address{tel='15899988899', name='上海市'}} 注意:对于@PropertySource
因为yaml语法很简洁,比较喜欢写yaml配置文件,但是经过试验,@PropertySource默认不支持yaml读取,我们改成@Value注解也是可以读取的,不过属性一堆的话,一个一个读取也是很繁琐的 ,通过网上找资料和自己实验验证,发现是可以实现对yaml支持 然后,为什么@PropertySource注解默认不支持yaml文件读取? 可以简单跟一下源码 @PropertySource源码: ? 根据注释,默认使用DefaultPropertySourceFactory类作为资源文件加载类 ? 源码实现的可以参考博客:https://www.cnblogs.com/liuming1992/p/4360310.html,这篇博客写的比较详细 然后要支持的话,具体怎么实现,参考我博客:SpringBoot系列之@PropertySource
使用@Value 和@PropertySource 实现为属性赋值 @Value 注解可以为属性值赋值,赋值的方式有多种。 基于数值。 基于 SpEL 表达式#{}。 基于配置文件的。 Spring Expression Language(SpEL)速查指南 如: @Value("#{30-2}") private Integer age; 基于配置文件 第一步先指定配置文件,通过 PropertySource @Component @PropertySource(value = { "classpath:/mine.properties"} ) public class User { @Value user.name}") private String userName; 这个${user.name} 注入的是环境变量中的值 Administrator 静态变量注入值 @Component @PropertySource applicationContext.close(); } } 输出: null 原因在于,Spring 不允许将值直接注入给静态变量 ,只能使用变通的方法: @Component @PropertySource
今天就针对我们平时最长使用的@Value,以及可能很少人使用的@PropertySource、@ConfigurationProperties等相关注解进行一个详细的扫盲,希望能够帮助到到家,使用起来更加顺畅 :加载配置属性源 此注解也是非常非常的强大,用好了,可以很好的实现配置文件的分离关注,大大提高开发的效率,实现集中化管理 最简单的应用,结合@Value注入属性值(也是最常见的应用) 通过@PropertySource @Override public PropertySource<? @PropertySource多环境配置以及表达式使用(spring.profiles.active) 方法一:可以这么配置 @PropertySource(“classpath:jdbc-${spring.profiles.active 细节:Bean的字段必须有get/set方法,请注意~~~ 另外还有一种结合@PropertySource使用的方式,可谓完美搭配 @Component @PropertySource("classpath
@PropertySource注解 @PropertySource注解只能标注到类上,能够通过指定配置文件的位置来加载配置文件,@PropertySource注解除了可以加载properties配置文件外 (PropertySource<? 本章,首先介绍了@PropertySource注解的源码和使用场景,随后,简单给出了一个@PropertySource注解的使用案例。 关于@PropertySource注解,通常会有如下几个经典面试题: @PropertySource注解的执行流程? @PropertySource注解是如何将配置文件加载到环境变量的? @PropertySource注解有哪些使用场景? Spring中为何会设计一个@PropertySource注解来加载配置文件? 从@PropertySource注解的设计中,你得到了哪些启发?
@PropertySource:加载指定的配置文件; 因为当我们把所有配置文件都放在全局的配置文件 中时会导致配置文件过多,所以我们可以根据业务逻辑把配置文件分开来放 本次做了一个测试,当自定义配置文件后缀为 /09/03 maps: { k1: v1 ,k2: v2} lists: [1,2,3] dog: name: dog age: 10 bean @Component @PropertySource v2 persion.lists=1,2,3 persion.dog.name=dog persion.dog.age=15 persion.last-name=王五 bean @Component @PropertySource
PropertySource:属性源,key-value 属性对抽象,用于配置数据。 该成员变量内部存储着一组 PropertySource,表示 key-value 键值对的源的抽象基类,即一个 PropertySource 对象则是一个 key-value 键值对。 = null) { for (PropertySource<? logger.trace("Searching for key '" + key + "' in PropertySource '" + propertySource.getName 子类的主要区别在于它们默认添加的 PropertySource 对象。而 AbstractEnvironment 则没有添加任何内容。
读取配置文件的几种方式测试方式1:通过Environment读取配置信息测试方式2:通过@Value注解读取配置信息(推荐使用)测试方式3:通过@ConfigurationProperties注解读取配置信息测试方式4:通过@PropertySource +@Value注解读取配置信息测试方式5:通过@PropertySource+@ConfigurationProperties注解读取配置信息测试方式6:通过Properties读取配置信息总结结论:无论什么场景都推荐使用 +@Value注解读取配置信息注意点说明:注意点1:@PropertySource注解加载指定的属性文件(*.properties)到 Spring 的 注意点2:@PropertySource注解可以配合 @Value 和@ConfigurationProperties 和@Bean一起使用,只不过我没写案例 第3种方案:如果非要使用@PropertySource注解注入一个配置文件,那么一定指向自定义名称配置文件,千万不要指向以application-为前缀的配置文件
Spring框架提供了PropertySource注解,目的是加载指定的属性文件。 > propertySource) { String name = propertySource.getName(); // 这个特别的重要,这个其实就是Spring处理配置文件优先级的原理 = null) { PropertySource<? ((ResourcePropertySource) propertySource).withResourceName() : propertySource); if (existing instanceof ) @Documented @Repeatable(PropertySources.class) public @interface PropertySource { // 该配置项PropertySource
提供了一个named(String name)方法用于构造基于name的PropertySource的空实现,从而便于PropertySource 集合中查找指定属性命的PropertySource(毕竟上面说了它只和 <PropertySource<? <PropertySource<? > propertySource) { removeIfPresent(propertySource); this.propertySourceList.add(0, propertySource > propertySource) { removeIfPresent(propertySource); this.propertySourceList.add(propertySource);
项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/spring-annotation @PropertySource注解概述 @PropertySource @PropertySource注解的源代码如下所示。 @PropertySource(value={"classpath:xxx.properties", "classpath:yyy.properties"}) 细心的读者可以看到,在@PropertySource @PropertySources(value={ @PropertySource(value={"classpath:xxx.properties"}), @PropertySource 接下来,我们就以一个小案例来说明@PropertySource注解的用法。
记一次SpringBoot2.x的@ImportResource和@PropertySource的坑 众所周知,SpringBoot提倡的是无配置文件,也就摒弃了以前用Spring时繁重的 这时候就想到了几种方式:@ImportResource, @PropertySource @ImportResource 是用于导入外部的.xml文件 @PropertySource 是用于导入变量类型的配置文件如 在尝试了很多次之后,我都无法通过@ImportResource的方式来导入外部.yml .properties配置文件,我也没有细心去查阅相关的源码,因此我转而关注@PropertySource来帮助我解决问题 在使用@PropertySource的时候又发现了一个问题,它能正常帮助我导入.properties文件,我也能通过@ConfigurationProperties和@Value来得到配置文件中的相应变量值 * * @PropertySource的facotroy属性,默认是使用 DefaultPropertySourceFactory 实现。
;public interface PropertySourceLocator { PropertySource<? > propertySource = locator.locate(environment); if (propertySource == null) { return )) { Collection<PropertySource<? >> sources = ((CompositePropertySource) propertySource).getPropertySources(); List<PropertySource ); } }}一个方法locate(),返回了PropertySource,另一个是默认的方法可以返回PropertySource的集合源码非常简单,下面我们去实现一下这个接口三、
<T> makeEncryptable(PropertySource<T> propertySource) { if (propertySource instanceof EncryptablePropertySource {} [{}", propertySource.getName(), propertySource.getClass()); } return propertySource ; } PropertySource<T> encryptablePropertySource = convertPropertySource(propertySource) proxyPropertySource(propertySource) : instantiatePropertySource(propertySource); } private <T> PropertySource <T> instantiatePropertySource(PropertySource<T> propertySource) { PropertySource<T> encryptablePropertySource
PropertySource<? PropertySource & PropertySources 从名字就能直观看出,PropertySources 是持有一个或者多个 PropertySource 的类。 PropertySource 下面看下 PropertySource 的源码: public abstract class PropertySource<T> { protected final public static PropertySource<? = null) { // 遍历所有的 PropertySource for (PropertySource<?
> propertySource) { if (logger.isDebugEnabled()) { logger.debug("Adding PropertySource '" + propertySource.getName , propertySource); //尝试移除同名的PropertySource removeIfPresent(propertySource); //获取前一个PropertySource > propertySource) { //注意,这里会再次尝试移除同名的PropertySource removeIfPresent(propertySource); this.propertySourceList.add = null) { //遍历所有的PropertySource for (PropertySource<? 中存在同名的key,返回的是第一个PropertySource对应key的属性值的处理结果,因此我们如果需要自定义一些环境属性,需要十分清楚各个PropertySource的顺序。
org.springframework.stereotype.Component; /** * Component 定义为组件 * ConfigurationProperties 通过前缀+属性自动注入 * PropertySource 指定配置文件 */ @Component @ConfigurationProperties(prefix = "flower",ignoreUnknownFields = true) @PropertySource org.springframework.stereotype.Component; /** * Component 定义为组件 * ConfigurationProperties 通过前缀+属性自动注入 * PropertySource 指定配置文件 */ @Data @Component @ConfigurationProperties(prefix = "flower",ignoreUnknownFields = true) @PropertySource Component @NoArgsConstructor @ConfigurationProperties(prefix = "flower",ignoreUnknownFields = true) @PropertySource
<T> instantiatePropertySource(PropertySource<T> propertySource) { PropertySource<T> encryptablePropertySource (propertySource); } else if (propertySource instanceof SystemEnvironmentPropertySource) { encryptablePropertySource = (PropertySource<T>) new EncryptableSystemEnvironmentPropertySourceWrapper ((SystemEnvironmentPropertySource) propertySource, propertyResolver, propertyFilter); } else if (propertySource instanceof MapPropertySource) { encryptablePropertySource = (PropertySource
name: 张三 email: zhangsan@qq.com 其映射实体内容为如下 @Data @AllArgsConstructor @NoArgsConstructor @Builder @PropertySource propertySource = getPropertySource(propertySourceName); // if(! ObjectUtils.isEmpty(propertySource)){ // user.setName(String.valueOf(propertySource.getProperty propertySource = getPropertySource(propertySourceName); if(! ObjectUtils.isEmpty(propertySource)){ //方法一 bean初始化之前,修改属性文件加载顺序 getEnvironment