我正在用java
在Spring中編寫WebApp後端。在代碼中有很多神奇的數字。有沒有一種方法可以將此配置放入config
中,以便在不重新啓動整個應用程序的情況下對此配置中的任何更改生效?如何在spring中動態加載java中的配置
0
A
回答
1
當java進程啓動時,加載spring上下文,一旦加載spring上下文,它只會讀取屬性文件一次,所以如果你改變任何屬性,你必須重新啓動你的應用程序,這很好。
或者您可以用Apache Commons Configuration項目中的PropertiesConfiguration替換java.util.Properties。它支持自動重新加載,通過檢測文件何時更改或通過JMX觸發來支持。
另一種替代方法是將所有的prop變量保存在數據庫中並定期刷新您的引用緩存,這樣您就不必重新啓動應用程序,並且可以從數據庫實時更改屬性。
0
您可以通過下面的步驟調用配置文件:
- 使用@Configuration標註爲它調用 配置文件中的類。
- 另一個註釋到類以上聲明用於定義路徑配置文件@PropertySource({ 「URL/PATH_OF_THE_CONFIG_FILE」})
- @Value( 「$ {PROPERTY_KEY}」)註釋上方的變量,其中對應於property_key的值需要被分配。
下列bean在相同的配置調用類。
@Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); }
確保@ComponentScan覆蓋了配置文件放在
0
這裏就是這樣的文件夾,你可以配置它
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--To load properties file -->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:META-INF/*-config.properties">
</property></bean>
<bean id="createCustomer" class="com.example.Customer">
<property name="propertyToInject" value="${example.propertyNameUnderPropertyFile}">
</beans>
您也可以參考它立即在java文件中
public class Customer {
@Value("${example.propertyNameUnderPropertyFile}")
private String attr;
}
相關問題
- 1. 動態加載spring xml配置
- 2. 如何在Magento中動態加載配置參數?
- 3. 動態加載配置Bean
- 4. 如何在Flash中反動態加載動態加載的PNG
- 5. 如何在apache + spring中動態加載屬性文件的值
- 6. 如何在Spring中動態加載bean的屬性
- 7. 如何在沒有spring-boot的情況下在spring-webflux中加載配置?
- 8. 如何在spring mvc 4中添加resourcehandler 4 java配置
- 9. 基於Spring中的屬性文件動態配置java beans
- 10. 使用Spring Boot中的動態端口配置Geb配置
- 11. 從Java配置中使用Spring動態語言支持
- 12. 在配置context.xml時在java spring中使用重載的方法
- 13. 在Java中動態加載模塊
- 14. 如何在AspNetCore中配置動態WebAPI
- 15. 如何在java中動態設置PreparedStatement?
- 16. 如何動態加載配置文件在春天
- 17. 如何在java中編程設置動態庫的加載路徑?
- 18. 如何在VueJs中動態添加一組配置的模板?
- 19. 如何加載靜態配置信息
- 20. 如何在nginx中配置動態SSL配置的動態虛擬主機?
- 21. 如何在Java配置類中設置Spring Weaver選項?
- 22. 如何在spring java配置中設置日誌記錄屬性?
- 23. 動態加載終端中的java類
- 24. 在CakePHP中動態加載插件配置文件3
- 25. 如何在基於Java的安全配置的Spring Security中配置兩個formLogin?
- 26. 的Spring Java配置
- 27. 的Spring Java配置
- 28. 在Java中配置OAuth 2 Spring Boot
- 29. Java Spring自動裝配配置文件
- 30. 如何在rails中啓動時加載配置?
檢查此問題http://stackoverflow.com/questions/26150527/how-can-i-reload-properties-file-in-spring-4-using-annotations – user1516873