2016-08-11 104 views
2

我必须从WildFly中读取属性文件,这是在使用spring的野蛮外战中,我在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绝对路径,但我必须从服务器给相对路径的路径可以是不同的机器不同。谁能帮我?

回答

1

如果使用的是弹簧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 
0
<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> 
相关问题