Java 基于系统属性模式(Dev/Prod/UAT)加载适当的属性文件
我的rest客户端将系统属性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
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>