如何使用spring在java中动态加载配置
我正在用如何使用spring在java中动态加载配置,java,spring,Java,Spring,我正在用java中的spring编写一个WebApp后端。代码中有很多神奇的数字。有没有一种方法可以将其放入配置中,使此配置中的任何更改在不重新启动整个应用程序的情况下生效?当java process start加载spring上下文时,加载spring上下文后,它只读取属性文件一次,所以如果您要更改任何属性,您必须重新启动应用程序,这样做很好 或者,您可以使用Apache Commons配置项目中的PropertiesConfiguration替换java.util.Properties。它支
java
中的spring编写一个WebApp后端。代码中有很多神奇的数字。有没有一种方法可以将其放入配置
中,使此配置中的任何更改在不重新启动整个应用程序的情况下生效?当java process start加载spring上下文时,加载spring上下文后,它只读取属性文件一次,所以如果您要更改任何属性,您必须重新启动应用程序,这样做很好
或者,您可以使用Apache Commons配置项目中的PropertiesConfiguration替换java.util.Properties。它支持自动重新加载,或者通过检测文件何时更改,或者通过JMX触发
另一种方法是将所有prop变量保留在数据库中,并定期刷新引用缓存,这样您就不必重新启动应用程序,也可以通过数据库动态更改属性。您可以通过以下步骤调用配置文件:
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
下面是您可以配置它的方法
<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>
检查这个问题
public class Customer {
@Value("${example.propertyNameUnderPropertyFile}")
private String attr;
}