2016-12-16 52 views
2

我想在Spring應用程序中維護應用程序屬性列表,如服務端點,應用程序變量等。這些屬性應該能夠動態更新(可能通過系統管理員通過網頁)。如何在Spring中動態地維護,更新應用程序屬性?

春天有內置功能來完成這項要求嗎?

+4

請參閱此處:http://stackoverflow.com/questions/26150527/how-can-i-reload-properties-file-in-spring-4-using-annotations除發佈的解決方案之外,還有一條評論引用了以下內容內置功能:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#dynamic-language-refreshable-beans –

+0

你可能想看看spring-boot admin 。 http://codecentric.github.io/spring-boot-admin/1.5.3/ –

回答

0

我不確定,但檢查是否可以使用Spring引導框架的@ConfigurationProperties。

@ConfigurationProperties(locations = "classpath:application.properties", ignoreUnknownFields = false, prefix = "spring.datasource") 
  1. 你可以把這個文件的application.properties在你的classpath
  2. 更改該文件中的屬性,而無需重新部署應用程序

的Java專家 - 我只是想探索我的看法。更正總是受歡迎的。

編輯 - 我讀@PropertySource here

0

外化性能很好的例子,看看here

彈簧加載這些屬性可以在運行時進行配置,並以不同的方式應用程序訪問的。

0

我不確定spring是否有動態更新屬性文件的實現。

您可以執行諸如使用FileInputStream讀取屬性文件到Properties對象。然後你將能夠更新屬性。稍後,您可以使用FileOutputStream將屬性寫回到同一個文件。

// reading the existing properties 
FileInputStream in = new FileInputStream("propertiesFile"); 
Properties props = new Properties(); 
props.load(in); 
in.close(); 
// writing back the properties after updation 
FileOutputStream out = new FileOutputStream("propertiesFile"); 
props.setProperty("property", "value"); 
props.store(out, null); 
out.close(); 
0

添加您自己的實施PropertySource到您的Environment

警告:@ConfigurationProperties@Value註釋使用屬性只在應用程序啓動時讀取一次,在運行時這樣改變實際屬性值將沒有任何影響(直到重新啓動)。

相關問題