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

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

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

方式一:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 对于读取一个配置文件采取的方案 -->
<property name="location" value="classpath:config.properties"></property>
<!-- 对于读取两个以上配置文件采取的处理方案 -->
<!--
<property name="locations">
<list>
<value>classpath:config.properties</value>
<value>classpath:config2.properties</value>
</list>
</property>
-->
</bean>

方式二:

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

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

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

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

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

测试用例:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-core.xml")
public class SpringContextTest { @Value("${userName}")
private String uname; @Value("${userPassword}")
private String pwd; @Test
public void test() {
System.out.println("username:" + uname);
System.out.println("password:" + pwd);
} }
username:root
password:123456

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

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<!-- 数据库基本信息配置 -->
<property name="url"
value="${jdbcUrl}" />
<property name="username" value="${userName}" />
<property name="password" value="${userPassword}" />
<property name="driverClassName" value="${jdbcDriver}" />
</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. 我的第一个C语言程序

    从自学开始到现在应该有块一个月了,之前一直想要写博客一直没想好要自己建博客还是找平台来写.现在想想 其实都一样,不论在哪里,都可以记录自己学习的成长记录.这是我的第一篇关于C语言学习的博客,希望这只是 ...

  2. Beta阶段——第2篇 Scrum 冲刺博客

    Beta阶段--第2篇 Scrum 冲刺博客 标签:软件工程 一.站立式会议照片 二.每个人的工作 (有work item 的ID) 昨日已完成的工作 人员 工作 林羽晴 完成https安全连接的问题 ...

  3. 十七、Spring框架(IOC/DI)

    一.Spring框架 Spring是一个基于IOC和AOP的结构J2EE系统的框架. 1.IOC反转控制是Spring的基础(Inversion Of Control).也就是说创建对象由以前的程序员 ...

  4. [LeetCode] 104. Maximum Depth of Binary Tree ☆(二叉树的最大深度)

    描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

  5. 文件查找命令find

    十六. 文件查找命令find: 下面给出find命令的主要应用示例:    /> ls -l     #列出当前目录下所包含的测试文件    -rw-r--r--. 1 root root 48 ...

  6. win7 忘记密码

    你可以找个PE来修改密码,用光盘或U盘做PE都行,现在很多PE都支持密码修改的!不过下面这个方法还是要用到PE:1. 进入pe2.进入c:\windows\system32下 更改magnify.ex ...

  7. svn分支使用 SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤

    比较好的介绍了分支的创建和使用. 转:http://blog.csdn.net/vbirdbest/article/details/51122637

  8. bootstrap 弹框使用

    首先需要准备bootstrap.css,bootstrap .js  jquery 我这里有写好的下载地址如下: https://pan.baidu.com/s/1miMahXe  秘钥:tgts & ...

  9. 比较不错的几款开源的WPF Charts报表控件

    UWP Community Toolkit 1. ModernUIChart Code: http://modernuicharts.codeplex.com/ Available Charts Co ...

  10. TiDB 深度实践之旅--真实“踩坑”经历

    美团点评 TiDB 深度实践之旅(9000 字长文 / 真实“踩坑”经历) 4   PingCAP · 154 天前 · 3956 次点击 这是一个创建于 154 天前的主题,其中的信息可能已经有所发 ...