Java 如何为整个项目配置单个属性占位符
在我的项目中,我有多个上下文文件。我使用属性占位符加载属性文件,如下所示 下面是我的context.xml文件Java 如何为整个项目配置单个属性占位符,java,spring,properties,configuration,Java,Spring,Properties,Configuration,在我的项目中,我有多个上下文文件。我使用属性占位符加载属性文件,如下所示 下面是我的context.xml文件 a.xml <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="file:${conf.path}/devconfigur
a.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
b.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
c.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
a.xml
b、 xml
c、 xml
每次使用war文件时,我们都要更改所有上下文文件。是他们的任何方式,有一个财产的地方持有人为整个项目。
我尝试过,但如果不使用属性占位符bean,我无法加载属性文件。非常感谢您的帮助。您可以将
b.xml
和c.xml
的定义导入a.xml
<beans> // a.xml
...
<import resource="classpath:b.xml"/>
<import resource="classpath:c.xml"/>
或者如果您没有使用上下文
名称空间
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:a.properties</value>
<value>classpath:b.properties</value>
<value>classpath:c.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
类路径:a.properties
类路径:b.properties
类路径:c.properties
您可以将b.xml
和c.xml
的定义导入a.xml
<beans> // a.xml
...
<import resource="classpath:b.xml"/>
<import resource="classpath:c.xml"/>
或者如果您没有使用上下文
名称空间
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:a.properties</value>
<value>classpath:b.properties</value>
<value>classpath:c.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
类路径:a.properties
类路径:b.properties
类路径:c.properties
如果我正确理解该场景,您是否为每个环境配置了3个配置?可能是区域性的,您可以在不同的xml文件之间切换
a.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
b.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
c.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
您应该做的是只有一个xml文件,它解析为属性文件中的属性
在资源文件夹中,每个环境有一个属性文件
在启动时,您可以传递一个jvm参数来设置“区域”或标识环境的方式,然后在属性文件中有占位符的地方“填充”xml
这应该有助于您开始。如果我正确理解了该场景,您是否为每个环境配置了3个配置?可能是区域性的,您可以在不同的xml文件之间切换
a.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
b.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
c.xml
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${conf.path}/devconfiguration.xml" />
<!--<property name="location" value="file:${conf.path}/sitconfiguration.xml" />
<property name="location" value="file:${conf.path}/uatconfiguration.xml" />
<property name="location" value="file:${conf.path}/prodconfiguration.xml" />-->
</bean>
您应该做的是只有一个xml文件,它解析为属性文件中的属性
在资源文件夹中,每个环境有一个属性文件
在启动时,您可以传递一个jvm参数来设置“区域”或标识环境的方式,然后在属性文件中有占位符的地方“填充”xml
这应该可以帮助您开始。可能取决于构建工具(例如)。可能取决于构建工具(例如)。