外在化应用参数的配置

在开发企业应用期间,或者在将企业应用部署到生产环境时,应用依赖的很多参数信息往往需要调整,比如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}等类似信息。

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>userinfo.properties</value></list></property>
</bean>
<bean name="userInfo" class="test.UserInfo"><property name="username" value="${db.username}"/><property name="password" value="${db.password}"/>
</bean>

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

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

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

转载于:https://www.cnblogs.com/yifanSJ/p/9226900.html

Spring —— context:property-placeholder/元素相关推荐

  1. spring context 例子

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. 基于Xml 的IOC 容器-载入<property>元素

    BeanDefinitionParserDelegate 在解析<Bean>调用parsePropertyElements()方法解析<Bean>元素中的<propert ...

  3. Spring基本使用(元素lookup-method使用)

    0.0 可以使用 元素 lookup-method 来取代 ApplicationContextAware 接口,避免应用代码直接和和 Spring 代码耦合在一起 注意:元素 lookup-meth ...

  4. spring context 学习总结(注解功能,配置文件,jar包)

    目录 一jar包 1.POM.XML文件 总项目的POM.XML文件 公用工具的模块POM.XML文件 子类模块POM.XML文件 二配置文件 2.web.xml文件 总项目的web.xml文件 工具 ...

  5. Spring Context 你真的懂了吗?

    今天介绍一下大家常见的一个单词 context 应该怎么去理解,正确的理解它有助于我们学习 spring 以及计算机系统中的其他知识. 1. context 是什么 我们经常在编程中见到 contex ...

  6. java spring context_java将对象注册到spring context中 | 学步园

    很多时候需要从spring中取出相应的对象,很多时候又需要将对象注入到spring context中. 小卒碰到了这样的问题.在构件中有DAO. 其他构件需要复用这个DAO,但是连接配置各不相同,并且 ...

  7. Spring context:component-scan代替context:annotation-config

    Spring context:component-scan代替context:annotation-config XML: <?xml version="1.0" encod ...

  8. Arthas实践--获取到Spring Context,然后为所欲为

    背景 Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. https://github.com/alibaba/arthas Arthas提供了非常丰富的关于调用拦截的命令,比如 ...

  9. spring context标签

    原文链接:http://kingliu.iteye.com/blog/1972678 spring context有以下标签: 1.annotation-config.作用是隐式地向Spring容器注 ...

最新文章

  1. 怎么把MySQL的数据传入JTable,如何直接从数据库中将数据填充到Jtable
  2. 这才是智能手表该有的样子 HUAWEI WATCH 2评测
  3. linux下搭建nagios监控
  4. Pythone3 sys模块
  5. 祝各位朋友元旦快乐,新年大发!!!
  6. python思维方法_小白3个月学会Python的路径和思维方式“大揭秘”!
  7. 当 p<1时,p 范数不满足三角不等式的证明 | p norm | triangle inequality
  8. Quartus II 12.0 下载、安装和破解
  9. 最近公共祖(LCA)模板_祖先深度_区域祖先_(欧拉序列+标准RMQ+四毛子)O(n)-O(1)
  10. 集成安装光盘制作教程(http://bbs.deepin.org/viewthread.php?tid=1170144)
  11. 带你认识世界最值钱的十种货币
  12. SAP SD 销售中的借贷项凭证
  13. USASCII7字符集导入ZHS16GBK字符集
  14. 个人对粗糙集的一些理解和简单举例
  15. powerdesigner设置自动递增
  16. moon服务器 zerotier_初探:建立 zerotier 的 moon 服务器
  17. 中南大学计算机专业男女比例,中南大学相亲网上线 目前男女比例过于悬殊
  18. table表格头出现表头错乱
  19. 学习资料 | TI毫米波雷达学习网站汇总(雷达初学者必须要知道)
  20. 《解忧杂货店》- 解答心中已经有结论的疑问

热门文章

  1. 民办大学计算机专业教师,干货!民办大学和独立院校教师创新排行榜,这五个高校最亮眼...
  2. Github | 微软-计算机视觉最佳实践、代码示例和相关文档
  3. Matlab线性/非线性规划优化算法(1)
  4. java中doloop语句_Java中的do-while循环——通过示例学习Java编程(11)
  5. java 模板方法_Java设计模式21:模板方法(Template Method)
  6. parted命令 Linux 分区超过2TB硬盘
  7. 渗透测试入门6之权限提升
  8. mysql修改数据库历史_MySQL之操作数据库
  9. delphi中保留字的属性和含义
  10. 代码示例_标准IO_fseek