Java 如何使用Spring重新加载属性?
我正在使用Spring3的属性文件。 当Spring初始化其contex时,它加载属性文件并将其放入带有@Value注释的所有bean中 我希望能够更新文件中的一些属性,并在服务器上公开一个JMX,将新属性重新加载到Spring,而无需重新启动服务器并重新加载其上下文Java 如何使用Spring重新加载属性?,java,spring,properties,Java,Spring,Properties,我正在使用Spring3的属性文件。 当Spring初始化其contex时,它加载属性文件并将其放入带有@Value注释的所有bean中 我希望能够更新文件中的一些属性,并在服务器上公开一个JMX,将新属性重新加载到Spring,而无需重新启动服务器并重新加载其上下文 我可以通过使用一些Spring方法来实现这一点吗来重新加载属性并将它们填充到所有bean中,或者我应该自己编写类似的东西吗?我建议用项目中的属性配置来替换java.util.properties。它支持自动重新加载,无论是通过检测
我可以通过使用一些Spring方法来实现这一点吗来重新加载属性并将它们填充到所有bean中,或者我应该自己编写类似的东西吗?我建议用项目中的
属性配置来替换java.util.properties
。它支持自动重新加载,无论是通过检测文件何时更改,还是通过JMX触发。我认为没有通用的方法可以做到这一点。最“干净”的方法是关闭Spring上下文并从头开始构建它。例如,考虑使用DCPP连接池并更新它的数据库连接URL。这意味着必须正确关闭池,然后创建新对象,然后更新对池的所有引用。现在,一些bean可能从该池获取连接,而更新池配置意味着您需要以某种方式重新请求连接。因此,bean可能需要知道如何做到这一点,这并不常见
我建议使用配置和更新事件创建单独的bean,并将该bean作为您需要了解配置更改的所有bean的依赖项。然后,您可以使用ApacheCommons配置来更改文件并传播配置更新
也许使用JMS是好的(如果你以后要发布你的应用的话)。是的,你可以用SpringJMX的方式来做。将这些bean添加到spring配置文件中。创建一个单独的方法来读取属性文件。在这个示例中,我使用callThisReach()方法。
再说一遍
之后,您可以使用jconsole重新加载您的方法,而无需重新启动服务器。使用apache common with spring,如下所示:
@Component
public class ApplicationProperties {
private PropertiesConfiguration configuration;
@PostConstruct
private void init() {
try {
String filePath = "/opt/files/myproperties.properties";
System.out.println("Loading the properties file: " + filePath);
configuration = new PropertiesConfiguration(filePath);
//Create new FileChangedReloadingStrategy to reload the properties file based on the given time interval
FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
fileChangedReloadingStrategy.setRefreshDelay(60*1000);
configuration.setReloadingStrategy(fileChangedReloadingStrategy);
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public String getProperty(String key) {
return (String) configuration.getProperty(key);
}
public void setProperty(String key, Object value) {
configuration.setProperty(key, value);
}
public void save() {
try {
configuration.save();
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}
Apache为我们提供了使用可重新加载属性文件的实用工具。
这里有些人建议使用外部方式来使用属性(Spring自己使用属性文件的方式之外)。这个答案正是您在SpringBoot和JavaEE中寻找的热重新加载属性 这是可以的,但是如何将所有新属性填充到bean中呢?我想使用一些spring方法重新加载这些属性,检查所有bean并设置新值使用PropertiesConfiguration
是bean的一个属性,无需重新填充,实例本身会更新。您应该查看而不是属性配置。您的回答让我有足够的洞察力来回答类似的问题:如何在Java EE和Spring Boot中热加载属性。谢谢
@Component
public class ApplicationProperties {
private PropertiesConfiguration configuration;
@PostConstruct
private void init() {
try {
String filePath = "/opt/files/myproperties.properties";
System.out.println("Loading the properties file: " + filePath);
configuration = new PropertiesConfiguration(filePath);
//Create new FileChangedReloadingStrategy to reload the properties file based on the given time interval
FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
fileChangedReloadingStrategy.setRefreshDelay(60*1000);
configuration.setReloadingStrategy(fileChangedReloadingStrategy);
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public String getProperty(String key) {
return (String) configuration.getProperty(key);
}
public void setProperty(String key, Object value) {
configuration.setProperty(key, value);
}
public void save() {
try {
configuration.save();
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}
<bean id="propertiesReloadingStrategy" class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy">
<property name="refreshDelay" value="30000" /> <!-- 30 seconds -->
</bean>
<bean id="reloadableProperties" class="org.apache.commons.configuration.PropertiesConfiguration">
<constructor-arg value="file:/web/${weblogic.Domain}/${weblogic.Name}/${app.Name}/reloadable_cfg/Reloadable.properties"/>
<property name="reloadingStrategy" ref="propertiesReloadingStrategy"/>
</bean>