在项目中我们一般将配置信息(如数据库的配置信息)配置在一个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. stl常用的查找算法

    #include<iostream> using namespace std; #include"vector" #include"algorithm&quo ...

  2. windows开启Apache的mod_rewrite模块

    windows下安装apache默认是没有开启mod_rewrite模块的,启用也很简单,修改apache配置文件httpd.conf,查找rewrite_module, 找到这行:#LoadModu ...

  3. ireport部署到Linux服务器上遇到的问题解决

    ireport报表在本地Windows环境运行正常,一旦部署到Linux环境上出现了如下问题: 1.打开报表,后台直接报net.sf.jasperreports.engine.util.JRFontN ...

  4. java中equals与==的比较

    总结来说: 1)对于==,如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等: 如果作用于引用类型的变量,则比较的是所指向的对象的地址 2)对于equals方法,注意:equals方法不 ...

  5. cookie VS localstorage

    http://jerryzou.com/posts/cookie-and-web-storage/ cookie: 1. 数据上限4KB左右 2. 一般由服务器生成,可设置失效时间.如果在浏览器端生成 ...

  6. 一、I/O操作(缓存流,数据流,对象流)

    一.缓存流 以介质是硬盘为例子说明,字节流和字符流的缺点: 每次读写的时候,都会访问硬盘,如果读写频率比较高的时候,性能不佳.为了解决问题,采用缓存流. 缓存流在读取的时候,会一次性读较多的数据到缓存 ...

  7. Hadoop---18/06/03 20:15:52 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable starting yarn daemons

    WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin- ...

  8. oracle 常用字符串函数

    select  initcap('guodongdong') from dual;                                  /返回字符串并将字符串的第一个字母变为大写;  s ...

  9. maven聚合工程使用如何debug

    maven聚合工程在正常情况下,使用debug时会出错,因为没有源码,就不会显示代码和断点行数条. 进行如下操作: 默认情况下source下只有默认的default文件夹,点击remove进行删除(这 ...

  10. 窗体应用程序防腾讯QQ源码

    窗体应用程序防腾讯QQ源码 using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...