spring的value标签获取不值原样输出${}

前提已经确保spring的配置文件以及读取到properties文件了我的配置是:
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:filer.properties" order="4" ignore-unresolvable="true"/>


spring的value标签注入properties配置文件的值在Controller中输入${"name"},但是在service层就可以
获取到配置文件的值。


经过上面的分析,足以说明是Controller层和service层的问题引起的,因为service层可以读取到值,
那么是什么原因导致Controller原样输出了${}这些东西呢?


@Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml


我的另一篇博客有一种解决办法,但是感觉不够好(需要的可以进入博客去找下),那种办法就是在每个xxx-servlet.xml中都扫描配置文件,
那我这里介绍的就是在applicationContext.xml中扫描一次,但是暴露自己的Id,在其他的xxx-servlet.xml中引入这个对象就可以了,下面是配置
applicationContext.xml扫描方式:
<!-- 引入属性文件 -->
<bean id="global" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<array>
<value>classpath:config/file.properties</value>
</array>
</property>
</bean>
这里暴露的Id是global,
下面是xxx-servlet.xml的配置文件:
<!-- 引入其他容器的配置 -->
<context:property-placeholder properties-ref="global" />
这样就可以注入对象了,下面有个注意点:(不要el表达式写多了这里后面写空格,除非你的配置文件名称的后面真的有空格,不如会找不到)
正确写法:@Value(value = "${file.serverPath}")
错误写法:@Value(value = "${file.serverPath }")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值