使用spring

时间:2016-08-11 10:59:45

标签: spring wildfly-9

我必须从WildFly中读取属性文件,这是使用spring在wildfly之外的战争,我在春天尝试使用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的绝对路径,但我必须从服务器给出相对路径,因为不同机器的路径可能不同。有谁可以帮助我?

2 个答案:

答案 0 :(得分:1)

如果您使用的是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

答案 1 :(得分: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>
相关问题