背景

工作中负责的一套计费系统需要开发一个新通知功能,在扣费等事件触发后发送MQ,然后消费MQ发送邮件或短信通知给客户。因为有多套环境,测试时需要知道是从哪套环境发出的邮件,又不想维护多套通知模板,因此就打算在各环境的properties中声明不同的title前缀,实现类似[DEV]您的xx月账单[TEST]您的xx月账单的效果,但是这个前缀需要在生产环境中去掉,因此我想到用Spring @Value的默认值来实现,伪代码如下:


@Value("${notice.mail.titlePrefix:}")
private String mailTitlePrefix; public void doSendEmail() {
...
String title = "xxx";
if (StringUtils.isNotBlank(mailTitlePrefix)) {
title = mailTitlePrefix + title;
}
mailSender.send(title, content, recevier);
}

采用上述代码后,运行发现,即使在properties中配置了值,但是mailTitlePrefix一直是空字符串"",一旦把冒号去掉又能正常读取到配置的值,修改:后面的数据为其他值,如@Value("${notice.mail.titlePrefix:113}") mailTitlePrefix的值也为113,即@Value一直只能获取到默认值。

工程采用spring标签声明了两个property-placeholder,分别读取不同的配置文件:

<context:property-placeholder order="0" location="classpath*:db.properties" ignore-unresolvable="true"/>
<context:property-placeholder order="0" location="classpath*:config.properties" ignore-unresolvable="true"/>

notice.mail.titlePrefix的配置在config.properties文件中。

问题定位

可以确定是spring属性值注入出现的问题,google一番后,找到一篇相同问题的文章spring-boot-spring-always-assigns-default-value-to-property-despite-of-it-bein

按照 SPR-9989 上的说明,Spring在有多个property-placeholder时,如果第一个property-placeholder在处理@Value时没有找到属性值,则会采用默认值对属性进行赋值,然后第二个property-placeholder就会忽略该@Value,于是@Value获取到的永远都是默认值。

问题解决

合并property-placeholder声明:

<context:property-placeholder order="0" location="classpath*:db.properties,classpath*:config.properties" ignore-unresolvable="true"/>

追加

这个bug一直是未修复状态,好像Spring官方不认为这是一个bug?截至spring 5.2.x版本也有这个问题。完整的TestCase详见larva-zhang/some-problems-record/spring-always-assigns-default-value-to-Value-annotation

应用中有多个Spring Property PlaceHolder导致@Value只能获取到默认值的更多相关文章

  1. Spring boot Jpa添加对象字段使用数据库默认值

    Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...

  2. spring中使用@Value设置全局变量默认值

    前几天在开发过程中遇到一个使用 spring 的 @Value 给类的全局变量设置默认值不成功的问题,最后通过查资料也是轻松解决,但是发现使用@Value也是有多种多样的方式,今天总算是将开发任务结束 ...

  3. spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化

    这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...

  4. 基于spring的placeholder思路处理配置信息敏感信息加密解密的实践

    基于Spring的placeholder处理思路,实现系统配置信息敏感信息的加密解密处理. 我们的处理方案,是基于类org.springframework.beans.factory.config.P ...

  5. Spring property文件配置方法以及如何与工程分离

    1,Spring使用property文件作为配置源    工程中难免出现一些需要每次部署都需要配置的参数,如数据源连接参数等,测试环境跟实际运行环境是不一样的.    使用spring框架的话,这些参 ...

  6. 给Spring的placeholder设置默认值

    问题:使用Spring时,可以方便地通过placeholder的形式${key}将key对应的properities定义value,注入到Bean中.但是如果在properities文件中,没有对ke ...

  7. spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />

    spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...

  8. [转]spring property标签中的 ref属性和ref 标签有什么不同

    spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...

  9. Spring的PropertyPlaceholderConfigurer强制使用默认值的坑

    1.问题 dubbo client配置: <dubbo:reference id="channelCustomerClient" interface="com.gt ...

随机推荐

  1. 04-树5 Root of AVL Tree (25 分)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  2. lintcode-1038. 珠宝和石头

    题目描述 给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝. J中的字母一定不同,J和S中的字符都是字母. 字母区分大小写,因 ...

  3. nginx php-fpm 配置问题(1)

    nginx php-fpm 配置问题(1) 1.问题    Nginx/FPM/PHP all php files say 'File not found.' nginx error日志: [erro ...

  4. C复习---动态内存分配

    原型extern void *malloc(unsigned int num_bytes);头文件#include <stdlib.h>#include <malloc.h>函 ...

  5. [Beta阶段]第三次Scrum Meeting

    Scrum Meeting博客目录 [Beta阶段]第三次Scrum Meeting 基本信息 名称 时间 地点 时长 第三次Scrum Meeting 19/05/05 大运村寝室6楼 30min ...

  6. tensorflow 2.0 的资料

    https://github.com/jtoy/awesome-tensorflow https://github.com/Amin-Tgz/awesome-tensorflow-2 https:// ...

  7. How to receive a million packets per second

    Last week during a casual conversation I overheard a colleague saying: "The Linux network stack ...

  8. 解决vue视图不渲染

    动态添加对象属性 //声明var travelMainVueObj;travelMainVueObj = new Vue({ "el" : "#portletConten ...

  9. [SQL]用于提取组内最新数据,左连接,内连接,not exist三种方案中,到底谁最快?

    本作代码下载:https://files.cnblogs.com/files/xiandedanteng/LeftInnerNotExist20191222.rar 人们总是喜欢给出或是得到一个简单明 ...

  10. 阿里云mysql数据库恢复到本地

    本地环境为win10,mysql引擎为InnoDB 第一步:服务里面停掉mysql 第二步:把my.ini 的 innodb_force_recovery  设置为0 第三步:把.frm和.idb文件 ...