无法使用PropertyPlaceholderConfigurer更新配置文件

时间:2015-08-26 08:25:01

标签: java spring configuration

是否可以使用PropertyPlaceholderConfigurer

更新属性

applicationContext.html

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="false"></property>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="locations" value="classpath:config.properties" />
</bean>
<bean id="appConfig" class="com.abc.Configuration">
    <property name="myProperty" value="${config.request.myProperty}" />
</bean>

Configuration.java

@Configuration
@Component
public class ServerConfig {
    private int myProperty;
    public int getMyProperty(){return myProperty;}
    public int setMyProperty(int value){this.myProperty = value }
}

config.properties

myProperty=123456

我可以使用getMyproperty()获取“myProperty”值。但我无法使用setMyProperty()更新属性 - 配置文件未更新,因此如果重新启动应用程序,新值将丢失。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

更新此类属性将无效。您需要直接访问配置文件并将更改写入其中。问题是PropertyPlaceholderConfigurer可能会读取不同类型的源(在您的情况下是类路径资源)。其中一些可能是只读的。

另请注意,如果您进行更改,该事件将不会自动获取。你必须刷新春天的背景。或者您可以使用一些专用配置库(例如cfg4j

相关问题