在项目中我们一般将配置信息(如数据库的配置信息)配置在一个properties文件中,如下:

  1. jdbcUrl=jdbc:mysql://localhost:3306/flowable?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&useSSL=false&serverTimezone=UTC
  2. userName=root
  3. userPassword=
  4. jdbcDriver=com.mysql.cj.jdbc.Driver

接着在Spring的配置文件中读取,有两种方式:

方式一:

  1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  2. <!-- 对于读取一个配置文件采取的方案 -->
  3. <property name="location" value="classpath:config.properties"></property>
  4. <!-- 对于读取两个以上配置文件采取的处理方案 -->
  5. <!--
  6. <property name="locations">
  7. <list>
  8. <value>classpath:config.properties</value>
  9. <value>classpath:config2.properties</value>
  10. </list>
  11. </property>
  12. -->
  13. </bean>

方式二:

  1. <!--采用这种方式简化配置文件-->
  2. <context:property-placeholder location="classpath:config.properties"/>

我们知道,不论是使用PropertyPlaceholderConfigurer还是通过context:property-placeholder这种方式进行实现,都需要记住,Spring框架不仅仅会读取我们的配置文件中的键值对,而且还会读取Jvm 初始化的一下系统的信息。有时候,我们需要将配置Key定一套命名规则 ,例如

  1. 项目名称.组名.功能名=配置值
  2. org.team.tfunction=0001

同时,我们也可以使用下面这种配置方式进行配置,这里我配NEVER的意思是不读取系统配置信息。如:

  1. <context:property-placeholder location="classpath:config.properties" system-properties-mode="NEVER"/>

测试用例:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration("classpath:spring-core.xml")
  3. public class SpringContextTest {
  4.  
  5. @Value("${userName}")
  6. private String uname;
  7.  
  8. @Value("${userPassword}")
  9. private String pwd;
  10.  
  11. @Test
  12. public void test() {
  13. System.out.println("username:" + uname);
  14. System.out.println("password:" + pwd);
  15. }
  16.  
  17. }
  1. username:root
    password:123456

配置文件中使用就比较简单了:

  1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  2. destroy-method="close">
  3. <!-- 数据库基本信息配置 -->
  4. <property name="url"
  5. value="${jdbcUrl}" />
  6. <property name="username" value="${userName}" />
  7. <property name="password" value="${userPassword}" />
  8. <property name="driverClassName" value="${jdbcDriver}" />
  9. </bean>

Spring的PropertyPlaceholderConfigurer的更多相关文章

  1. Spring中PropertyPlaceholderConfigurer的使用

    Spring中PropertyPlaceholderConfigurer的使用     在使用Spring配置获取properties文件时,在网上查到相关的资料,分享哈!! (1)获取一个配置文件 ...

  2. Spring的PropertyPlaceholderConfigurer应用

    Spring 利用PropertyPlaceholderConfigurer占位符 1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFa ...

  3. Spring 利用PropertyPlaceholderConfigurer占位符

      Hey Girl   博客园    首页    博问    闪存    新随笔    订阅     管理 posts - 42,  comments - 3,  trackbacks - 0 Sp ...

  4. Spring的PropertyPlaceholderConfigurer应用(转)

    转自:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html Spring 利用PropertyPlaceholderConfigu ...

  5. Spring里PropertyPlaceholderConfigurer类的使用

    1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...

  6. spring中propertyplaceholderconfigurer简介

    Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyP ...

  7. spring 的 PropertyPlaceholderConfigurer读取的属性怎么访问 (java访问方式,不是xml中的占位符哦)及此类的应用

    一.1.占位符的应用:(@Autowired注解方式,不需要建立set与get方法了,xml注入也不需要写了) http://www.cnblogs.com/susuyu/archive/2012/0 ...

  8. Spring的PropertyPlaceholderConfigurer强制使用默认值的坑

    1.问题 dubbo client配置: <dubbo:reference id="channelCustomerClient" interface="com.gt ...

  9. Spring的PropertyPlaceholderConfigurer事例应用

    在开发的过程中,经常发现一些类似:${log4j.level}之类的内容,后来才知道原因.下面解释一下: 1.PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现 ...

随机推荐

  1. CF-517C-思维/math

    http://codeforces.com/contest/1072/problem/C 题目大意是给出两个数a,b ,找出若干个数p,使得 SUM{p}<=a ,找出若干个数q使得SUM{q} ...

  2. 1002. Find Common Characters查找常用字符

    参考:https://leetcode.com/problems/find-common-characters/discuss/247573/C%2B%2B-O(n)-or-O(1)-two-vect ...

  3. echarts3使用总结2

    接着上一篇文章补充一点项目中遇到的问题及解决方法 1.y轴正负轴调换  yAxis: {     inverse: false,  //y轴正负轴调换 }, 2.去掉图表背景线 yAxis: [   ...

  4. ubuntu14静态ip配置

    0.配置ip需要掌握的一些基本指令 打开/创建文件      sudo vim ... 插入信息      i 保存并强制退出      先按Esc,再键入:wq!,回车 1.使用命令 sudo vi ...

  5. js 时间戳转特定格式的日期

    var Tools = {}; Tools.formatDate = function (fmt,timestamp) { if(timestamp){ var date = new Date(par ...

  6. 通过AssemblyResolve事件打包合并exe和dll文件

    使用WPF开发的安装包,在创建快捷方式的时候,需要用到COM程序集Windows Script Host Object Model,引用COM程序集后,会在debug目录生成Interop.IWshR ...

  7. WPF客户端实现.net升级

    客户端.net版本由3.5升级到4.5,首先把.net4.5的离线安装包添加到资源,程序运行的时候,从资源中生成离线安装包,并通过传递参数的方式执行静默安装命令,具体代码如下: private sta ...

  8. ID基本操作(创建主页,复制主页,把主页应用到多个页面)5.11

    主页上的对象将会显示在应用在这个主页上的所有页面. 一.创建主页的方法: 1.页面面板,右上方点击,可以新建主页..前缀:用来识别页面面板中的各个页面所应用的主页.最多可输入四个字符.名称:输入主页跨 ...

  9. Win10系列:VC++绘制几何图形2

    新建了Direct2D中的资源后,接下来初始化用于绘制图形的应用窗口.在解决方案资源管理器窗口中右键点击项目图标,在弹出的菜单栏中选中"添加", 并在"添加"的 ...

  10. Java中的 JDK下载和环境配置(方式一)

    第一步:需要安装JDK. JDK下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...