使用 spring 从外部路径加载 属性 文件
load property file from external path using spring
我必须使用 spring 从 WildFly 读取 war 外部的 属性 文件,我在 spring 中使用 PropertyPlaceholderConfigurer 尝试了它并且它正在工作但是有一个问题。
applicationContext.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:/C:\wildfly-9.0.2.Final\wildfly-9.0.2.Final\standalone\deployments\/propertyLoader.properties</value>
</property>
</bean>
<bean id="getPropertyBean"
class="com.csc.loadProperty.GetProperty">
<property name="prefixProp" value="${prefix}" />
<property name="suffixProp" value="${suffix}" />
<property name="newProp" value="${new}" />
</bean>
这里我给出了 propertyLoader.properties 的绝对路径,但我必须给出服务器的相对路径,因为不同机器的路径可能不同。谁能帮帮我?
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:# C:/wildfly-9.0.2.Final/wildfly-9.0.2.Final/standalone/deployments/propertyLoader.properties</value>
</property>
</bean>
如果您使用的是 spring 4 ,则使用 ${}
指定 属性 文件路径
@Configuration
@PropertySource("file:${app.home}/app.properties")
public class AppConfig
@Autowired
Environment env;
}
然后在启动时将app.home设置为系统变量。如果您 运行 spring 应用程序在某个容器内,则在 java 启动选项或 vm 参数中设置此 属性。
java -jar -Dapp.home="/home/dev/config" example.jar
我必须使用 spring 从 WildFly 读取 war 外部的 属性 文件,我在 spring 中使用 PropertyPlaceholderConfigurer 尝试了它并且它正在工作但是有一个问题。
applicationContext.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:/C:\wildfly-9.0.2.Final\wildfly-9.0.2.Final\standalone\deployments\/propertyLoader.properties</value>
</property>
</bean>
<bean id="getPropertyBean"
class="com.csc.loadProperty.GetProperty">
<property name="prefixProp" value="${prefix}" />
<property name="suffixProp" value="${suffix}" />
<property name="newProp" value="${new}" />
</bean>
这里我给出了 propertyLoader.properties 的绝对路径,但我必须给出服务器的相对路径,因为不同机器的路径可能不同。谁能帮帮我?
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:# C:/wildfly-9.0.2.Final/wildfly-9.0.2.Final/standalone/deployments/propertyLoader.properties</value>
</property>
</bean>
如果您使用的是 spring 4 ,则使用 ${}
指定 属性 文件路径@Configuration
@PropertySource("file:${app.home}/app.properties")
public class AppConfig
@Autowired
Environment env;
}
然后在启动时将app.home设置为系统变量。如果您 运行 spring 应用程序在某个容器内,则在 java 启动选项或 vm 参数中设置此 属性。
java -jar -Dapp.home="/home/dev/config" example.jar