[英]Location of ApplicationContext.xml and properties file in spring
[英]how can i access properties file filed using applicationcontext.xml file in spring?
我在我的applicationContext文件中添加了以下内容
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:stage.properties</value>
<value>classpath:environment.properties</value>
</list>
</property>
</bean>
我需要访问stage.properties文件值的位置.stage.properties文件在src / main / resources中
我已经在我的java类中编写了以下代码来访问此文件
@Value("${spring.username}")
private String usr;
但是我得到usr的值就像= $ {spring.username},我在这里缺少什么?
@Value(“ $ {spring.username}”)表示法需要使用PropertyPalceholderConfigurer / PropertySourcesPlaceholderConfigurer(取决于Spring版本)来解析属性占位符。 要么
解决方案1
更换
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:stage.properties</value>
<value>classpath:environment.properties</value>
</list>
</property>
</bean>
与
<context:property-placeholder location="classpath:stage.propertes,classpath:environment.properties"/>
确保导入Spring上下文名称空间
解决方案2。
使用SpEL通过@Value访问属性bean,如下所示
@Value("#{properties['spring.username']}
private String usr;
这将在您的上下文中访问属性bean的spring.username属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.