大型项目中,我们往往会对我们的系统的配置信息进行统一管理,一般做法是将配置信息配置与一个cfg.properties的文件中,然后在我们系统初始化的时候,系统自动读取cfg.properties配置文件中的key value(键值对),然后对我们系统进行定制的初始化。

  那么一般情况下,我们使用的java.util.Properties,也就是java自带的。往往有一个问题是,每一次加载的时候,我们都需要手工的去读取这个配置文件,一来编码麻烦,二来代码不优雅,往往我们也会自己创建一个类来专门读取,并储存这些配置信息。

 但是,在于我看来,这些原本Spring就有的东西,你还自己重复造轮子,费功夫。而且未必没有那么方便维护和管理升级。
用起来的通用性比较差,各个企业有各个企业的开发规范,这样子一来,学习成本也提高。(这个是废话)。

Spring中提供着一个PropertyPlaceholderConfigurer

  这个类是BeanFactoryPostProcessor的子类。(不懂自己度娘,可了解可不了解,下面我会讲大概)

其主要的原理在是。Spring容器初始化的时候,会读取xml或者annotation对Bean进行初始化。
初始化的时候,这个PropertyPlaceholderConfigurer会拦截Bean的初始化,
初始化的时候会对配置的${pname}进行替换,根据我们Properties中配置的进行替换。从而实现表达式的替换操作 。

了解完原理之后,我们来看其实现的几种方式。

在我们的classpath下新建一个cfg.properties

#cfg.properties配置文件的内容
username=jay
password=123

下面是Spring配置文件代码

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 对于读取一个配置文件采取的方案 -->
<property name="location" value="classpath:cfg.properties"></property>
<!-- 对于读取两个以上配置文件采取的处理方案 -->
<!--
<property name="locations">
<list>
<value>classpath:cfg.properties</value>
<value>classpath:cfg2.properties</value>
</list>
</property>
-->
</bean>
</beans>
//测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationCtx.xml")
public class SpringCtxTst { @Value("${username}")
private String uname; @Value("${password}")
private String pwd; @Test
public void test(){
System.out.println("username:"+uname);
System.out.println("password:"+pwd);
}
}

控制台输出

username:jay
password:123

当然,Spring 为此也为我们提供了另外一个的解决方案,我们直接在Spring 配置文件中书写如下代码即可实现

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--采用这种方式简化配置文件-->
<context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties"/> </beans>

重要注意

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

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

这种方式来尽量减少与系统配置信息的冲突! 
  同时,我们也可以使用下面这种配置方式进行配置,这里我配NEVER的意思是不读取系统配置信息。如果

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

浅谈Spring的PropertyPlaceholderConfigurer的更多相关文章

  1. 转:浅谈Spring的PropertyPlaceholderConfigurer

    大型项目中,我们往往会对我们的系统的配置信息进行统一管理,一般做法是将配置信息配置与一个cfg.properties的文件中,然后在我们系统初始化的时候,系统自动读取cfg.properties配置文 ...

  2. 浅谈Spring中的Quartz配置

    浅谈Spring中的Quartz配置 2009-06-26 14:04 樊凯 博客园 字号:T | T Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在 ...

  3. 浅谈Spring的两种配置容器

    浅谈Spring的两种配置容器 原文:https://www.jb51.net/article/126295.htm 更新时间:2017年10月20日 08:44:41   作者:黄小鱼ZZZ     ...

  4. 1.1浅谈Spring(一个叫春的框架)

    如今各种Spring框架甚嚣尘上,但是终归还是属于spring的东西.所以在这里,个人谈一谈对spring的认识,笔者觉得掌握spring原理以及spring所涉及到的设计模式对我们具有极大的帮助.我 ...

  5. 浅谈Spring MVC知识

    关于MVC框架,我相信大家都不陌生,都会说也就是模型-视图-控制器这三层的框架结构,如果你参加面试的时候考官会问:“MVC框架是什么?你说一说.”其实我们都知道这个问题还需要问的,只要你是一个开发人员 ...

  6. 浅谈Spring发展史

    1 码农的春天----------Spring来了 Spring官网 :http://www.springframework.org 关于Spring的发展起源要回溯到2002年,当时正是Java E ...

  7. 浅谈spring 声明式事物

    此处主要讲讲事物的属性. 事物属性包含了五个方面: 1.传播行为 2.隔离规则 3.回滚规则 4.事物超时 5.是否只读 一.传播行为 事务的第一个方面是传播行为(propagation behavi ...

  8. 浅谈spring security 403机制

    403就是access denied ,就是请求拒绝,因为权限不足 三种权限级别 一.无权限访问 <security:http security="none" pattern ...

  9. [SSH 3]以网上商城项目浅谈spring配置

    导读:在做ITOO项目的时候,就用到了容器+反射,从而运用了依赖注入和依赖查找.如果看过WCF端的配置文件,那么对于这个spring的配置就很容易理解.本篇博客,是对于自己做的一个小项目中所运用到的s ...

随机推荐

  1. Caffe常用层参数介绍

    版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/Cheese_pop/article/details/52024980 DATA crop:截取原图像中一个 ...

  2. Chapter 4 -- Throwables

    TODO: rewrite with more examples Guava's Throwables utility can frequently simplify dealing with exc ...

  3. httpd 不带反斜杠 出现 301重定向

    [root@VM_64_69_centos httpd]# curl http://localhost:9001/pay <!DOCTYPE HTML PUBLIC "-//IETF/ ...

  4. go语言之进阶篇网络编程

    一.网络编程 1.网络分层架构 2.每层协议的功能 3.网络通信条件 网卡,mac地址(不需要用户处理)  arp --->通过IP找mac 逻辑地址,ip地址(需要用户指定)  ---> ...

  5. Windows 安装配置 JIRA

    MySQL-5.5.28 JDK1.6.0_21 JIRA功能全面,界面友好,安装简单,配置灵活,权限管理以及可扩展性方面都十分出色. 一.MySQL建库和建账号 1. mysql中创建数据库jira ...

  6. android api doc 一

    Manifest.permission 用于记录相应权限类 ACCESS_CHECKIN_PROPERTIES 允许在登入数据库的时候读写其中的属性表,并上传改变的值 ACCESS_COARSE_LO ...

  7. spark0.8.0安装与学习

    spark0.8.0安装与学习       原文地址:http://www.yanjiuyanjiu.com/blog/20131017/ 环境:CentOS 6.4, Hadoop 1.1.2, J ...

  8. Using Timers in MFC Applications

    Timer Events in MFC Applications Event timers are always handy to have around and useful in nearly e ...

  9. CreateFont函数为什么改变不了字体?该怎么解决

    CreateFont函数为什么改变不了字体?CFont   *   f;             f   =   new   CFont;             f-> CreateFont( ...

  10. CareerCup Facebook Total number of substring palindrome

    Write a function for retrieving the total number of substring palindromes.  For example the input is ...