在Spring托管bean的方法中使用PropertySourcesPlaceholderConfigurer从文件中读取属性

时间:2013-09-19 07:05:47

标签: spring spring-mvc

我使用下面的代码来读取文件,我可以使用env.properties注释(注入)读取@Value中定义的值。

有没有办法在不使用@value(注入)的情况下在方法中获取这些值?

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
          <property name="locations">
            <list>
                <value>classpath:env.properties</value>

            </list>
          </property>
          <property name="ignoreUnresolvablePlaceholders" value="true" />
          <property name="ignoreResourceNotFound" value="true" />
  </bean>

1 个答案:

答案 0 :(得分:1)

您也可以在属性bean中自动装配:

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="properties" location="classpath:/environment.properties"/>

或者在你的方法中做一个getBean()

但坚持@value,它光滑,简单,易于测试。