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

来源:互联网 发布:ai人工智能设计软件 编辑:程序博客网 时间:2024/05/29 13:46
前提已经确保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 }")
原创粉丝点击