1. 从环境变量中读取路径:
<bean id="envPath" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <!-- System.getProperties() --> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="java.lang.System" /> <property name="targetMethod" value="getProperties" /> </bean> </property> <property name="targetMethod" value="getProperty" /> <property name="arguments" value="env.home"> </property> </bean>
2. 配置动态读取:
<bean id="dynamic.upload.config" class="org.apache.commons.configuration.PropertiesConfiguration" init-method="load">
<property name="fileName" value="#{envPath}\properties\upload\upload-config.properties"/>
<property name="reloadingStrategy">
<bean class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy"/>
</property>
</bean>
3. 需要引入jar包 commons-configuration
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>${commons.configuration-version}</version>
</dependency>

本文介绍如何使用Spring框架从环境变量中读取路径,并基于该路径动态加载配置文件。通过配置`MethodInvokingFactoryBean`来获取系统属性,再利用`PropertiesConfiguration`类动态加载配置,实现配置文件的热更新。
168万+

被折叠的 条评论
为什么被折叠?



