2017-10-04 36 views
1

的Spring MVC + Java的8 + Tomcat的8堆棧更改配置,無需重新啓動容器

我保持我的YAML配置和使用Spring的PropertyPlaceholderConfigurer壓扁性能和維護一個bean的配置。

今天,它有一個固有的問題,因爲我需要在YML文件發生變化時重新啓動服務器。

我相信有方法可以在不重啓的情況下刷新bean,但我主要關心的是如何以失敗安全的方式進行操作。假設有一個請求,那個時候配置是A,然後我們刷新配置,所以現在它的B,但是如果任何後續的用戶請求依賴於配置,那麼它會炸燬。

回答

1

此配置添加到你的servlet-context.xml中捉對飛行特性的變化:

<context:property-placeholder 
    location="file:${A_CONFIG_LOCATION}/configuration.properties" /> 

<beans:bean id="propertiesLoader" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 

    <beans:property name="cacheSeconds" value="1" /> 
    <beans:property name="basenames"> 
     <beans:list> 
      <beans:value>file:${A_CONFIG_LOCATION}/configuration 
      </beans:value> 
     </beans:list> 
    </beans:property> 
</beans:bean> 

然後你就可以讀出的屬性值是這樣的:

@Component 
public class PropertiesReader { 

    private String value   = "some_default_value"; 

    @Autowired 
    MessageSource propertiesLoader; 

    public String getValue() { 
     value = propertiesLoader.getMessage("configuration.value", null, null); 
     return value; 
    } 


} 
相關問題