`
struggle2036
  • 浏览: 6521 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring中context:property-placeholder/元素

阅读更多

1.有些参数在某些阶段中是常量

    比如:a、在开发阶段我们连接数据库时的连接url,username,password,driverClass等 

               b、分布式应用中client端访问server端所用的server地址,port,service等  

               c、配置文件的位置

2.而这些参数在不同阶段之间又往往需要改变

    比如:在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况。

期望:能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息

解决:spring3中提供了一种简便的方式就是context:property-placeholder/元素

只需要在spring的配置文件里添加一句:<context:property-placeholder location="classpath:jdbc.properties"/> 即可,这里location值为参数配置文件的位置,参数配置文件通常放在src目录下,而参数配置文件的格式跟java通用的参数配置文件相同,即键值对的形式,例如:

#jdbc配置

test.jdbc.driverClassName=com.mysql.jdbc.Driver
test.jdbc.url=jdbc:mysql://localhost:3306/test
test.jdbc.username=root
test.jdbc.password=root

行内#号后面部分为注释

应用:

1.这样一来就可以为spring配置的bean的属性设置值了,比如spring有一个jdbc数据源的类DriverManagerDataSource

在配置文件里这么定义bean:

<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${test.jdbc.driverClassName}"/>
    <property name="url" value="${test.jdbc.url}"/>
    <property name="username" value="${test.jdbc.username}"/>
    <property name="password" value="${test.jdbc.password}"/>
</bean>

2.甚至可以将${ }这种形式的变量用在spring提供的注解当中,为注解的属性提供值

---------------------------------------------------------  

外在化应用参数的配置

在开发企业应用期间,或者在将企业应用部署到生产环境时,应用依赖的很多参数信息往往需要调整,比如LDAP连接、RDBMS JDBC连接信息。对这类信息进行外在化管理显得格外重要。PropertyPlaceholderConfigurer和PropertyOverrideConfigurer对象,它们正是担负着外在化配置应用参数的重任。

  <context:property-placeholder/>元素

PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,它能够对<bean/>中的属性值进行外在化管理。开发者可以提供单独的属性文件来管理相关属性。比如,存在如下属性文件,摘自userinfo.properties。
db.username=scott
db.password=tiger

如下内容摘自propertyplaceholderconfigurer.xml。正常情况下,在userInfo的定义中不会出现${db.username}、${db.password}等类似信息,这里采用PropertyPlaceholderConfigurer管理username和password属性的取值。DI容器实例化userInfo前,PropertyPlaceholderConfigurer 会修改userInfo的元数据信息(<bean/>定义),它会用userinfo.properties中db.username对应的 scott值替换${db.username}、db.password对应的tiger值替换${db.password}。最终,DI容器在实例化 userInfo时,UserInfo便会得到新的属性值,而不是${db.username}、${db.password}等类似信息。

 
  1. <bean id="propertyPlaceholderConfigurer"   
  2.         class="org.springframework.beans.factory.config.  
  3. PropertyPlaceholderConfigurer">  
  4.     <property name="locations">  
  5.         <list>  
  6.             <value>userinfo.properties</value>  
  7.         </list>  
  8.     </property>  
  9. </bean>  
  10.  
  11. <bean name="userInfo" class="test.UserInfo">  
  12.   <property name="username" value="${db.username}"/>  
  13.   <property name="password" value="${db.password}"/>  
  14. </bean> 

通过运行并分析PropertyPlaceholderConfigurerDemo示例应用,开发者能够深入理解PropertyPlaceholderConfigurer。为简化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。

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

PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM 系统属性(System.getProperty())和环境变量(System.getenv())中寻找。通过启用 systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。

分享到:
评论

相关推荐

    context:property-placeholder 和util:properties

    context:property-placeholder 和util:properties 博客:https://blog.csdn.net/u010476739/article/details/76735527

    Spring整合Mybatis使用&lt;context:property-placeholder&gt;时的坑

    通常,如果你在配置文件中使用了相同的`&lt;context:property-placeholder&gt;`标签,Spring只会加载一次属性文件,而不会针对每个数据源重新加载。因此,${jdbc.url}等属性可能只被解析了一次,而不是按照每个数据源分别...

    SSH笔记-通过property-placeholder使用外部属性文件

    在Spring框架中,`&lt;context:property-placeholder&gt;`是用于加载和解析属性文件的一个标签,它允许我们在XML配置或Java配置中使用占位符 `${...}` 来引用属性文件中的值。这样做的好处是,我们可以将敏感信息如数据库...

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

    我们可以使用 `&lt;context:property-placeholder&gt;` 元素来加载 properties 文件,并将其配置信息注入到 bean 中。 PropertySource 注解配置 从 Spring 3.1 开始,我们可以使用 `@PropertySource` 注解来配置 ...

    spring_MVC源码

    15. &lt;context:property-placeholder location="classpath:/hibernate.properties" /&gt; 16. 17. 18. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; 19. &lt;property name=...

    17 Spring IoC容器如何读取多个属性文件或者配置文件?慕课专栏(1)1

    在Spring的老版本中,通常使用`&lt;context:property-placeholder&gt;`或`&lt;util:properties&gt;`元素来加载属性文件。例如: ```xml &lt;!-- 使用 context:property-placeholder --&gt; &lt;beans xmlns="http://...

    集成springmvc spring hibernate的配置

    使用`context:property-placeholder`标签将属性文件加载到Spring上下文中。然后,配置数据源`dataSource`,例如使用Apache Commons DBCP库: ```xml &lt;context:property-placeholder location="classpath:jdbc....

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    &lt;context:property-placeholder location="classpath:jdbc.properties" /&gt; class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource" ref=...

    引入多个properties时.txt

    在`applicationContext.xml`中通过`&lt;context:property-placeholder&gt;`标签来指定`properties`文件的位置。例如,我们需要引入两个文件:`jdbc.properties`和`res.properties`,可以这样配置: ```xml &lt;!-- 引入 jdbc...

    java 获取properties的几种方式(csdn)————程序.pdf

    例如,Spring的`PropertyPlaceholderConfigurer`和`&lt;context:property-placeholder&gt;`适用于Spring应用,而`ResourceBundle`适合处理本地化,`Properties`类则是一个通用解决方案。理解并熟练掌握这些方法,将有助于...

    spring4-hibernate4-struts2整合

    &lt;context:property-placeholder location="classpath:config.properties"/&gt; &lt;!-- 自动扫描dao和service包 --&gt; &lt;context:component-scan base-package="me.gacl.dao,me.gacl.service"/&gt; &lt;/beans&gt; ``` 4. **...

    spring,配置文件从属性文件读取JDBC连接的相关参数

    接下来,在Spring的`applicationContext.xml`配置文件中,我们使用`&lt;context:property-placeholder&gt;`标签来加载属性文件,并声明数据源bean。这个标签会自动替换XML配置文件中的占位符(以`${}`包裹)为属性文件中的...

    第十章 Spring 配置元信息(Configuration Metadata)1

    7. **基于Properties和YAML文件装载外部化配置**:`&lt;context:property-placeholder&gt;`和`&lt;context:property-override&gt;`用于加载和覆盖属性值,而YAML文件提供了更友好的格式来组织配置数据。 8. **基于Extensible ...

    浅谈xml配置spring profiles的几个注意点

    * 我们可以使用`&lt;context:property-placeholder&gt;`元素来加载不同的properties文件。 使用xml配置spring profiles可以帮助我们根据不同的环境配置不同的Bean,从而提高系统的灵活性和可维护性。

    spring-framework-3.2.0.RC2-schema.zip

    `context`命名空间下的 `&lt;context:component-scan&gt;`、`&lt;context:property-placeholder&gt;`等元素,用于扫描组件、加载外部属性文件,增强了Spring的应用范围和灵活性。 "cache"模块则提供了缓存抽象,支持如 EhCache...

    ssm 框架配置

    &lt;context:property-placeholder location="classpath:db.properties"/&gt; &lt;!-- 配置数据源 --&gt; &lt;property name="driverClassName" value="${jdbc.driver}" /&gt; &lt;property name="url" value="${jdbc.url}" /&gt; ...

    spring与mybatis整合配置文档

    这里通过`&lt;context:property-placeholder&gt;`加载了`dbconfig.properties`中的配置项,确保了数据库连接参数的外部化管理。 ```xml &lt;context:property-placeholder location="classpath:dbconfig.properties"/&gt; ...

    Maven项目中读取src/main/resources目录下的配置文件的方法

    在Spring框架中,我们也可以使用`context:property-placeholder`来读取配置文件。例如: ```xml &lt;context:property-placeholder location="classpath:kafka.properties"/&gt; ``` 在上面的配置中,我们使用`context:...

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

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

Global site tag (gtag.js) - Google Analytics