<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
其中classpath是引用src目录下的文件写法。
当存在多个Properties文件时,配置就需使用locations了:(2)
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>
接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties 文件,其配置如下:(3)
<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="location">
<value>classpath:/spring/include/dbQuery.properties</value>
</property>
</bean>
<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:/spring/include/jdbc-parms.properties</value>
<value>classpath:/spring/include/base-config.properties</value>
</list>
</property>
</bean>
其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true
(这一个true害了我两天的时间啊)
至此你已经了解到了如何使用PropertyPlaceholderConfigurer,如何使用多个Properties文件,以及如何配置多 个PropertyPlaceholderConfigurer来分解工程中分散的Properties文件。至于 PropertyPlaceholderConfigurer还有更多的扩展应用,如属性文件加密解密等方法将在之后的博文中续写。
分享到:
相关推荐
在Spring框架中,我们经常会遇到单个工程需要配置多个子系统的场景。为了更好地管理这些子系统及其配置,Spring提供了多种方式来处理这一问题。本文将详细介绍如何在一个项目中配置多个系统,尤其是在涉及到多个属性...
为了适应更复杂的应用场景,我们还可以配置多个`PropertyPlaceholderConfigurer`实例,以达到整合多工程下多个分散的properties文件的目的。例如: ```xml class="org.springframework.beans.factory.config....
Spring 中 ...在大型项目中,可能需要加载多个 Properties 文件,并且这些文件可能来自不同的模块或工程。在这种情况下,可以使用多个 `PropertyPlaceholderConfigurer` 来分散配置。例如: ```xml ...
2. **属性文件的顺序**:如果有多个属性文件,Spring会按照文件位置的顺序依次解析,后一个文件中的同名属性会覆盖前一个文件中的值。 3. **系统属性优先级**:通过设置`systemPropertiesMode`,可以选择是否让系统...
Spring 无法读取 properties ...Spring 无法读取 properties 文件数据问题可能是由于配置文件路径不正确、配置文件不存在或多个配置文件导致的。通过正确配置配置文件的路径和加载所有的配置文件,可以解决这些问题。
1. **加载属性源**:根据配置的`location`属性,加载一个或多个属性文件。 2. **解析占位符**:遍历所有bean定义,找到`${...}`形式的占位符,用对应的属性值进行替换。 3. **处理嵌套占位符**:如果属性值中还包含...
在实际项目中,可能会有多个properties文件,比如`database.properties`、`email.properties`等,这时可以通过`locations`属性一次性加载多个文件: ```xml <bean id="propertyConfigurer" class="org.spring...
本文将详细介绍五种在Java环境中,尤其是Spring框架中读取properties文件内容的方法,帮助开发者灵活地处理配置文件。 1. **通过`context:property-placeholder`加载配置** 在Spring配置文件中,可以通过`...