1

如何在运行时在 spring 中重新加载/刷新属性而不重新启动 jvm?

我正在寻找一些优雅的东西,它应该适用于 Prod。因此,我猜 JRebel 不在讨论范围内。

目前,我在 MYProject.properties 中有我的属性

oms.url=abbc.com
checkin.enabled=true

并且我的 java 文件被自动连接以从 applicationContext 中搜索和使用这些属性,提供了不同的属性文件:

<bean id="applicationProperties" class="myproject.config.CustomPropertyPlaceHolder">
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:MyProject.properties</value>
                <value>file:${CATALINA_BASE}/lib/MyProject.properties</value>
                <!--<value>file:///appl/conf/outlet.properties</value>-->
                <value>classpath:db.password</value>
                <value>file:${CATALINA_BASE}/lib/db.password</value>
                <!-- <ref bean="applPropertiesFromDb" /> -->
            </list>
        </property>
    </bean>

和Java文件为:

@Value("${checkin.enabled}")
    private String checkinEnabled;
4

2 回答 2

0

考虑像这样实现 MessageSource Spring bean:
1.更新 applicationContext.xml

<bean id="messageSource" class="com.mycompany.service.DynamicMessageSource" />

2.编写自己的MessageSource实现

public class DynamicMessageSource extends AbstractMessageSource {

    // Autowire propertiesService

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        String result = propertiesService.getProperty(code);
        return new MessageFormat(result, locale);
    }
}

3.实现您的自定义服务以从某个地方(数据库、属性文件等)读取属性。
4.在更新属性源时享受属性的动态更新。

于 2014-02-03T08:50:22.190 回答
0

使用ReloadableResourceBundleMessageSource.

但这不会被刷新。

private String checkinEnabled;

您必须重新加载checkinEnabled变量。

于 2014-02-03T08:23:28.507 回答