福州 网站建设,企业网站效果图,温州网红打卡地,凡科删除建设的网站1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现#xff0c;也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文#xff08;配置文 件#xff09;中的属性值放在另一个单独的标准java Properties文件中去。在XML文件…1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文配置文 件中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话只需要对properties文件进 行修改而不用对xml配置文件进行修改。
2.在Spring中使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件当然也可以指定外部文件的编码如 bean idpropertyConfigurerclassorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namelocationvalueconf/sqlmap/jdbc.properties/value/propertyproperty namefileEncodingvalueUTF-8/value/property/bean 当然也可以引入多个属性文件如 bean idpropertyConfigurerclassorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namelocationslistvalue/WEB-INF/mail.properties/value valueclasspath: conf/sqlmap/jdbc.properties/value//注意这两种value值的写法/list/property/bean 3.譬如jdbc.properties的内容为 jdbc.driverClassNamecom.mysql.jdbc.Driverjdbc.urljdbc:mysql://localhost/mysqldb?useUnicodetrueamp;characterEncodingUTF-8amp;zeroDateTimeBehaviorround;jdbc.usernamerootjdbc.password123456 4.那么在spring配置文件中我们就可以这样写 bean idpropertyConfigurerclassorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namelocationslistvalueclasspath: conf/sqlmap/jdbc.properties /value/list/property/beanbean iddataSourceclassorg.apache.commons.dbcp.BasicDataSourcedestroy-methodcloseproperty namedriverClassNamevalue${jdbc.driverClassName} /property nameurl value${jdbc.url} /property nameusername value${jdbc.username}/property namepasswordvalue${jdbc.password} //bean 5.这样一个简单的数据源就设置完毕了。可以看出PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。
6.查看源代码可以发现locations属性定义在PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中而location只有 setter方法。类似于这样的配置在spring的源程序中很常见的。
PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性它还会在Java的System类属性中查找。
我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。