Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于系统属性模式(Dev/Prod/UAT)加载适当的属性文件_Java_Spring - Fatal编程技术网

Java 基于系统属性模式(Dev/Prod/UAT)加载适当的属性文件

Java 基于系统属性模式(Dev/Prod/UAT)加载适当的属性文件,java,spring,Java,Spring,我的rest客户端将系统属性service.mode设置为prod或uat或dev。基于此属性,应加载文件dev.properties、prod.properties、uat.properties的相应属性。此属性将由spring的applicationContext.xml定义的不同服务bean使用 基于系统属性服务.mode加载相应属性的最佳方法是什么?最佳方法是在spring配置文件中使用PropertyPlaceHolderConfigure。像这样 <bean class="or

我的rest客户端将系统属性
service.mode
设置为prod或uat或dev。基于此属性,应加载文件
dev.properties、prod.properties、uat.properties
的相应属性。此属性将由spring的applicationContext.xml定义的不同服务bean使用


基于系统属性
服务.mode
加载相应属性的最佳方法是什么?

最佳方法是在spring配置文件中使用PropertyPlaceHolderConfigure。像这样

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>file:${service.mode}</value>
        </property>
</bean>

文件:${service.mode}
然后你可以在你的bean中访问这些属性

<bean id="initSystemStatus" class="xxxx.xxxx.xxxx.InitSystemStatus">
        <constructor-arg index="0" value="${application.context.instanceID:0001}" />
        <constructor-arg index="1" value="${application.context.timeout:2000}"/>
</bean>

N.B.


${application.context.instanceID:0001}
中,如果属性文件中未提供属性
application.context.instanceID
,则默认值为
0001

使用
PropertyPlaceHolderConfigure
,如下所示

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>${service.mode}.properties</value>
        </property>
</bean> 
请注意提到属性的方式
${service.mode:prod}
。这意味着,如果无法以任何方式解析
service.mode
,则将拾取默认值,即
prod


注意:这只适用于SPRING 3.X

属性PlaceHolderConfigure的配置是相同的,但是如果你仔细举例说明OP的问题,他需要一个标志(dev/uat等),让应用程序选择属性文件。用户只需传递dev/uat,而不传递dev.properties/uat.properties(您假定的)。如果未提供服务模式,则可以
PropertyPlaceholderConfigure
选择默认的say prod.poroperties文件。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>${service.mode:prod}.properties</value>
        </property>
</bean>