`
sumskyjia
  • 浏览: 11371 次
  • 性别: Icon_minigender_1
  • 来自: 常州
社区版块
存档分类
最新评论

Spring中属性占位符配置器PropertyPlaceholderConfigurer

 
阅读更多

<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了。

使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties 文件,其配置如下:

 

figurer">
<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。

PropertyPlaceholderConfigurer可以将${...}替换为指定的properties文件或system properties中的值。这样一来,我们就可以不再配置文件中为bean注入数值,而改用properties文件,可以降低耦合性,以后如果需要修改诸如数值,只修改properties文件就可以了。

 

分享到:
评论

相关推荐

    属性占位符配置器

    ### 属性占位符配置器:Spring框架中的高级配置机制 #### 一、概念解析 在Spring框架中,**属性占位符配置器**(Property Placeholder Configurator)是一种强大的配置工具,它允许开发者在配置文件中使用占位符来...

    Spring实战之属性占位符配置器用法示例

    Spring框架中提供了属性占位符配置器(PropertyPlaceholderConfigurer),用于读取外部属性文件,并将其设置为Spring配置文件的数据。本文将详细介绍Spring实战之属性占位符配置器用法示例,结合实例形式分析了...

    Spring PropertyPlaceholderConfigurer配置文件加载器集成ZooKeeper来实现远程配置读取

    `PropertyPlaceholderConfigurer`是Spring提供的一个bean定义解析器,它能够解析bean定义中的${}占位符,并将其替换为属性文件中的相应值。通常,这些属性文件位于项目的类路径下,或者通过classpath:前缀指定其他...

    关于spring系统中多系统的配置

    在Spring框架中,`PropertyPlaceholderConfigurer`是一种特殊的Bean,它被用来处理Spring配置文件中的占位符(placeholder),并将它们替换为具体的值。这些值通常来自外部的属性文件,如`.properties`或`.xml`等。...

    Spring动态加载配置文件

    1. `PropertyPlaceholderConfigurer`: 这个类可以解析包含占位符(如`${property}`)的bean定义,并替换为属性文件中的相应值。我们可以通过`locations`属性指定一个或多个属性文件的位置,Spring会在启动时加载这些...

    Spring 配置学习文件

    1. **PropertyPlaceholderConfigurer**:这个bean是用来加载外部属性文件`dataResources.properties`,它允许我们在配置中使用占位符 `${...}`,这些占位符会被对应的属性值替换。这样可以使得数据库连接信息等敏感...

    Spring 3.0 整合 iBatis 3 Beta10 配置

    使用C3P0连接池配置,如示例所示,通过属性占位符 `${jdbc.*}` 来引用`.properties`文件中的值: ```xml &lt;!-- C3P0 数据源配置 --&gt; ${jdbc.driverClass}" /&gt; ${jdbc.url}" /&gt; ${jdbc.user}" /&gt; ${jdbc....

    Spring中属性文件properties的读取与使用详解

    在这里,`${}`语法用于引用属性文件中的键值对,Spring会在运行时替换这些占位符。 第二种使用方式是通过注解在Java代码中获取属性值。例如,我们创建一个`ConfigInfo`类来封装配置信息: ```java @Component(...

    spring配置两个数据

    这里值得注意的是,每个数据源都通过`${...}`占位符引用了`db.properties`文件中的属性值,实现了动态配置。 ```xml ${jdbc.driverClassName1}"/&gt; ${jdbc.url1}"/&gt; ${jdbc.username1}"/&gt; ${jdbc.password1}...

    spring 配置

    在上述配置中,Spring使用了一个名为`propertyConfigurer`的Bean,它是`PropertyPlaceholderConfigurer`类的实例,用于加载外部属性文件(如`jdbc.properties`),以便在配置文件中使用`${property}`占位符引用这些...

    spring-reference

    `PropertyPlaceholderConfigurer`是一个BeanFactoryPostprocessor,用于在运行时替换配置文件中的占位符。 ##### 3.8.2 PropertyOverrideConfigurer `PropertyOverrideConfigurer`也是BeanFactoryPostprocessor的一...

    Spring 2企业应用开发[配套源代码]

    Spring框架允许使用`PropertyPlaceholderConfigurer`来替换配置文件中的占位符,例如`${database.url}`,这样可以实现环境间的配置隔离,方便在开发、测试和生产环境之间切换。 3. `applicationContext.xml`:这是...

    Spring源码学习四:BeanDefinition装载前奏曲1

    例如,`PropertyPlaceholderConfigurer`就是在这里处理属性占位符的替换。 `registerBeanPostProcessors(beanFactory)`注册了BeanPostProcessors,这些处理器会在Bean初始化时介入,执行额外的操作,如AOP代理、...

    spring工程需要的四个核心jar包之beans包

    - 在"beans"包中,`org.springframework.beans.factory.config`包下的`PropertyPlaceholderConfigurer`类用于处理占位符替换,实现环境变量或属性文件的值注入到Bean的属性中。 3. **Bean的生命周期管理** - ...

    spring-reference.pdf

    1. **PropertyPlaceholderConfigurer**:用于在配置文件中解析占位符,将占位符替换为实际值。 2. **PropertyOverrideConfigurer**:用于覆盖已存在的Bean定义属性,通常用于环境特定的配置。 3. **自定义...

    掌握Spring设计模式:Java工程师必备指南

    此外,`BeanFactoryPostProcessor`接口允许自定义扩展,如`PropertyPlaceholderConfigurer`用于处理配置文件中的占位符。 2. **工厂方法**:Spring提供的`FactoryBean`接口是工厂方法模式的体现。它使得我们能够...

    Spring 总结(1) 自用

    BeanPostProcessor允许在bean初始化前后进行自定义操作,而BeanFactoryPostProcessor则在bean加载但未实例化之前执行特殊处理,例如PropertyPlaceholderConfigurer可以用于处理属性文件中的占位符替换。 总的来说,...

    spring学习笔记2

    - `PropertyPlaceholderConfigurer`:用于解析属性占位符,如`${property}`,将其替换为实际的属性值。 - `CustomEditorConfigurer`:允许注册自定义编辑器,用于将字符串转换为特定类型的数据。 3. **事件处理**...

Global site tag (gtag.js) - Google Analytics