Spring注解方式读取配置文件的值

1.在spring.xml中加入

<!-- 注解使用properties -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
    <list>
        <value>classpath:/*.properties</value>
    </list>
</property>
</bean>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
      <property name="properties" ref="configProperties" />
</bean>

 

2.在类的成员变量上直接加上注解@Value("#{configProperties['xxxxx']}"),这里configProperties即你在xml配置的ref,xxxx即配置文件中的key名称。

 

posted @ 2015-12-17 08:56  我是魏星星  阅读(1375)  评论(0编辑  收藏  举报