`

spring中context:property-placeholder元素详解

 
阅读更多

在spring-context.xml配置中,读取配置文件我们会这样配置,

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="locations">

            <list>

                <value>classpath:conf-${spring.profiles.active}.properties</value>

            </list>

        </property>

        <property name="fileEncoding" value="utf-8"/>

    </bean>

殊不知,还有个容易被忽略的标签,<context:property-placeholder/>

为了简化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。

下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。

 

<context:property-placeholder location="userinfo.properties"/> 

它的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。

 

通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。

分享到:
评论

相关推荐

    Spring中property-placeholder的使用与解析详解

    Spring 中 property-placeholder 的使用与解析详解 -property-placeholder 是 Spring 框架中的一种机制,用于加载和解析 properties 文件中的配置信息。在本文中,我们将详细介绍 property-placeholder 的使用和...

    spring约束dtd.zip

    首先,DTD是一种定义XML文档结构的规范,它描述了文档中元素和属性的合法组合。在Spring框架中,DTD文件用于定义Spring容器如何解析和理解XML配置文件,确保配置的正确性和一致性。例如,`spring-beans-4.0.xsd`是...

    详解Spring加载Properties配置文件的四种方式

    在Spring XML配置文件中,可以使用context:property-placeholder标签来加载Properties配置文件。例如: ``` &lt;context:property-placeholder ignore-unresolvable="true" location="classpath:redis-key.properties"/...

    详解SpringMVC加载配置Properties文件的几种方式

    这是最常用的方式,通过在Spring的配置文件(如`spring.xml`)中引入`context`命名空间,并使用`context:property-placeholder`标签来指定Properties文件的位置。 ```xml &lt;?xml version="1.0" encoding="UTF-8"?&gt; ...

    Spring加载properties文件的两种方式实例详解

    在Spring中,我们可以使用 `&lt;context:property-placeholder location=""/&gt;` 标签来加载properties配置文件。location是配置文件的路径,我们可以在工程目录的src下新建一个conn.properties文件,里面写上dataSource...

    详解五种方式让你在java中读取properties文件内容不再是难题

    在Spring配置文件中,可以通过`&lt;context:property-placeholder&gt;`标签来加载properties文件。例如,配置文件`jdbc.properties`位于类路径下,可以这样设置: ```xml &lt;context:property-placeholder location=...

    spring profile 多环境配置管理详解

    Spring Profile 是 Spring 框架中的一个重要特性,它允许开发者为不同的环境(如开发、测试、生产等)创建和管理独立的配置。在多环境配置管理中,Spring Profile 提供了方便的方式来切换不同环境下的配置,确保每个...

    spring无法读取properties文件数据问题详解

    &lt;context:property-placeholder ignore-unresolvable="true" location="classpath:/jdbc.properties, classpath*:/config.properties"/&gt; ``` 问题三:诊断问题 在诊断 Spring 无法读取 properties 文件数据...

    详解spring applicationContext.xml 配置文件

    4. `&lt;context:property-placeholder&gt;`:这是一个特殊的bean,用于加载外部属性文件(如`jdbc.properties`),使得在配置中可以使用`${}`引用这些属性,避免硬编码。 5. `&lt;dataSource&gt;`:这里配置了数据源,Druid是...

    Spring中利用配置文件和@value注入属性值代码详解

    Spring中利用配置文件和@Value注解注入属性值代码详解 在Spring框架中,配置文件和@Value注解是两个常用的注入属性值的方法。本文将详细介绍如何使用配置文件和@Value注解注入属性值,并提供了代码示例。 一、简单...

    Java利用Sping框架编写RPC远程过程调用服务的教程

    在服务端的Spring配置文件(如`spring.xml`)中,使用`&lt;context:component-scan&gt;`扫描包含服务实现的包,并使用`&lt;context:property-placeholder&gt;`加载配置属性。 ```xml &lt;context:component-scan base-package=...

    SpringDataMongoDB多文档事务的实现

    &lt;context:property-placeholder location="classpath:mongo.properties" ignore-unresolvable="true" file-encoding="utf-8" /&gt; &lt;mongo:mongo-client id="mongoClient" replica-set="${mongo.replSet}"&gt; &lt;mongo:...

    springmvc+mybatis框架整合详解

    &lt;context:property-placeholder location="classpath:db.properties"/&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt; &lt;property name="driverClassName" value...

    ssm框架的搭建

    &lt;context:property-placeholder file-encoding="utf-8" location="classpath:db.properties"/&gt; &lt;!-- 数据源配置 --&gt; &lt;property name="username" value="${db.uname}"/&gt; &lt;property name="url" value="${db....

    详解MongoDB和Spring整合的实例代码

    &lt;context:property-placeholder ignore-unresolvable="true" location="classpath:/mongodb.properties"/&gt; &lt;mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}"&gt; &lt;!-- ...

    spring基于通用Dao的多数据源配置详解

    【Spring 多数据源配置详解】在开发过程中,有时我们需要在一个项目中同时连接多个数据库,这时就需要在Spring框架中配置多个数据源。本篇将详细讲解如何基于通用Dao实现Spring的多数据源配置。 首先,我们需要创建...

Global site tag (gtag.js) - Google Analytics