无法使用 PropertyPlaceholderConfigurer 更新配置文件

Can not update config file using PropertyPlaceholderConfigurer

是否可以使用 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

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

如有任何帮助,我们将不胜感激。

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

另请记住,如果您进行更改,事件将不会自动生效。您必须刷新 spring 上下文。或者你可以使用一些专用的配置库(例如 cfg4j