env.getProperty不起作用Spring PropertyPlaceholderConfigurer [英] env.getProperty not working Spring PropertyPlaceholderConfigurer

查看:952
本文介绍了env.getProperty不起作用Spring PropertyPlaceholderConfigurer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring加载属性文件

I am loading properties file using spring

  <bean id="appProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="locations" value="classpath:/sample.properties" />
          <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>

当我使用

@Value("${testkey}")正常工作.

但是当我尝试使用env时

but when i am trying to get using env

@Resource
 private Environment environment;

environment.getProperty("testkey") // returning null

推荐答案

PropertyPlaceholderConfigurer不会将其locations中的属性添加到Environment中.使用Java config,您可以使用@PropertySource来做到这一点.

A PropertyPlaceholderConfigurer does not add the properties from its locations to the Environment. With Java config, you can use @PropertySource to do that.

这篇关于env.getProperty不起作用Spring PropertyPlaceholderConfigurer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆