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}等类似信息。
- <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属性,开发者能够控制这一行为。
转自:http://blog.sina.com.cn/s/blog_4550f3ca0100ubmt.html
相关推荐
context:property-placeholder 和util:properties 博客:https://blog.csdn.net/u010476739/article/details/76735527
主要介绍了Spring整合Mybatis使用<context>时的坑 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
SSH笔记-通过property-placeholder使用外部属性文件的demo
使用该插件(jquery.placeholder.js)解决IE9及以版本下对placeholder的兼容性问题
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans ...
本篇文章主要介绍了Spring中property-placeholder的使用与解析详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
input type="text" placeholder="搜索" value=" "> 默认的placeholder字体颜色是呈浅灰色,如果想改变这个默认颜色,解决方案如下: XML/HTML Code复制内容到剪贴板 input:-moz-placeholder, textarea:-moz-...
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">...
webpack-image-placeholder-loader 该加载器从给定图像生成彩色或纯色图像,用作占位符。 该程序包在内部使用。 有关从图像派生的,请参见。 支持JPEG,PNG,WebP,TIFF,GIF和SVG图像。 例子 安装 使用npm安装:...
Laravel开发-placeholder 用于使用占位符图像服务向应用程序添加图像的Laravel包。
骨架占位符 SkeletonPlaceholder 是一个 React Native 库,可以使用 FlexBox 轻松创建惊人的加载效果。 安卓和iOS 安装 注意:这个包需要@react-native-masked-view/masked-view和react-native-linear-gradient ...
var getPlaceholderModule = require ( 'quill-placeholder-module' ) . default // umd var getPlaceholderModule = PlaceholderModule . default Quill . register ( 'modules/placeholder' , ...
一个简单的图像占位符组件,它使用很多占位符站点中的一些+时髦的赃物组件。 为什么? 这里有很多占位符站点,我将它们重新组合为一个不错的小组件,以及他的弟弟(NicolasCage,BillMurray,StevenSeagal和...
在您的应用程序中同时包含 jquery.html5-placeholder-shim.js 和 angular-placeholder-shim.js。 您还需要在您的项目中使用 jQuery。 < script src =" components/jquery-...
防暴占位符 Riot.js插件,用于生成占位符文本。 关于 riot-placeholder是一个简单的riot标签,用于基于riot.js的网站和应用程序的...import placeholder from 'riot-placeholder' placeholder ( riot ) console . l
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method...
03. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 04. xmlns:p=...
该软件包是教程的最终产品, 教程描述了如何创建“占位符”功能,该功能允许用户将预定义的占位符(例如日期或姓氏)插入文档中。 安装 npm install --save ckeditor5-placeholder 设定档 import ClassicEditor from...
gem 'jquery-placeholder-rails' 然后执行: $ bundle 或者自己安装: $ gem install jquery-placeholder-rails 然后在您的app/assets/javascripts/application.js文件中要求jquery-placeholder : //= ...
扩展TextView添加placeholder和maxInputLenth属性,将扩展文件导入即可使用对代码无污染 ...github:https://github.com/cccgoodboy/UITextView-placeholder-maxInputLenth 觉得好用请给个star 谢谢!