2013-08-26 142 views
1

如何在運行時重新加載/刷新屬性而無需重啓jvm?如何在運行時重新加載/刷新屬性而無需重啓jvm?

我在找優雅的東西,它應該適用於產品。因此,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; 
+0

據我所知Spring屬性解析爲java最終靜態變量,因此一旦沒有jvm重啓就實例化它們就無法更改。 –

+0

它並不完全是spring屬性,但更多的是容器在啓動時初始化的Bean。 –

+0

取決於豆的性質。如果它在你的例子中,並且它初始化到DB的連接,即使bean被重新配置,那麼舊連接會發生什麼?在內部,Spring bean有一個生命週期,可以重新啓動,但很難說沒有切入到框架代碼中是可能的。也許尋找一些JMX功能呢? –

回答

0

使用ReloadableResourceBundleMessageSource

但這不會被刷新。

private String checkinEnabled; 

您必須重新加載checkinEnabled變量。

0

考慮實施的MessageSource的Spring bean是這樣的:
1.Update的applicationContext.xml

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

2.Write自己的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.Implement您的定製服務,以讀取屬性從某個地方(DB,屬性文件等)。
4.更新屬性源時可以動態更新屬性。

相關問題