应用中有多个Spring Property PlaceHolder导致@Value只能获取到默认值
背景
工作中负责的一套计费系统需要开发一个新通知功能,在扣费等事件触发后发送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只能获取到默认值的更多相关文章
- Spring boot Jpa添加对象字段使用数据库默认值
Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
- spring中使用@Value设置全局变量默认值
前几天在开发过程中遇到一个使用 spring 的 @Value 给类的全局变量设置默认值不成功的问题,最后通过查资料也是轻松解决,但是发现使用@Value也是有多种多样的方式,今天总算是将开发任务结束 ...
- spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化
这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...
- 基于spring的placeholder思路处理配置信息敏感信息加密解密的实践
基于Spring的placeholder处理思路,实现系统配置信息敏感信息的加密解密处理. 我们的处理方案,是基于类org.springframework.beans.factory.config.P ...
- Spring property文件配置方法以及如何与工程分离
1,Spring使用property文件作为配置源 工程中难免出现一些需要每次部署都需要配置的参数,如数据源连接参数等,测试环境跟实际运行环境是不一样的. 使用spring框架的话,这些参 ...
- 给Spring的placeholder设置默认值
问题:使用Spring时,可以方便地通过placeholder的形式${key}将key对应的properities定义value,注入到Bean中.但是如果在properities文件中,没有对ke ...
- spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />
spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...
- [转]spring property标签中的 ref属性和ref 标签有什么不同
spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...
- Spring的PropertyPlaceholderConfigurer强制使用默认值的坑
1.问题 dubbo client配置: <dubbo:reference id="channelCustomerClient" interface="com.gt ...
随机推荐
- vue 百度地图多标注展示和点击标注进行的提示
index.html中加入script <script type="text/javascript" src="http://api.map.baidu.com/a ...
- Django 的 slug url 正则匹配
如果按照 .* 去匹配,最后会把 /edit/ 和 /delete/ 也匹配进去. urlpatterns = [ #... url(r'^(?P<slug>.*)/$', post_de ...
- win10中通过Anaconda安装tensorflow时报错Traceback (most recent call last): File “E:\Anaconda3\lib\site-packages\pip_vendor\urllib3\response.py”, line 360, in _error_catcher yield
问题:通过默认镜像安装,下载过程中可能会报错,下载安装失败 Traceback (most recent call last): File “E:\Anaconda3\lib\site-package ...
- 第01组 Alpha冲刺(2/6)
队名:007 组长博客: https://www.cnblogs.com/Linrrui/p/11861798.html 作业博客: https://edu.cnblogs.com/campus/fz ...
- cloudstack 安装 install for ubuntu
准备工作环境信息 修改dns配置 设置阿里源root@sh-saas-cs-manager-online-01:~# mv /etc/apt/sources.list /etc/apt/sources ...
- 分布式系统之CAP原理
参考链接:http://blog.csdn.net/wireless_com/article/details/79153643 CAP是什么? CAP理论,被戏称为[帽子理论].CAP理论由Eric ...
- 记一次netty http server给客户端返回reset包排除
类似文章:解决用netty去做web服务时,post长度过大的问题 现象:当客户端给server发送的请求体较大时,服务直接给客户端返回reset包. tcpdump: 应用还没有完全收上去,就clo ...
- Spring中为什么继承了ApplicationContextAware接口就可以使用ApplicationContext对象?
1.Spring中使用applicationContext对象 public class SpringContextUtil implements ApplicationContextAware { ...
- JavaScript的filter方法
var ages = [32, 33, 16, 40]; function checkAdult(age) { return age >= 18; } function myFunction() ...
- Ubuntu 17.10安装VirtualBox 5.2.2 及相关问题解决
link:https://www.linuxidc.com/Linux/2017-11/148870.htm sudo apt update && sudo apt upgrade s ...