一. 简介

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

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

  • 对于 web 项目来说,可以通过相对路径得到配置文件的路径,而对于可执行项目,在团队开发中就需要根据各自的环境来指定 properties 配置文件的路径了。对于这种情况可以将配置文件的路径放在 java 虚拟机 JVM 的自定义变量(运行时参数)中,例如:-Ddev.config=/dev.properties 寻找的是本机根目录下

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

二. XML 方式

方式1

<?xml version="1.0" encoding="UTF-8"?>
<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:db.properties"/>--> <!--对于读取多个配置文件采取的方案-->
<property name="locations">
<list>
<value>classpath:db.properties</value>
<value>classpath:db2.properties</value>
</list>
</property>
</bean>
</beans>
#db.properties
jdbc.driverClass==net.sourceforge.jtds.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?
jdbc.username=anqi
jdbc.password=123456
#db2.properties
name=anqi
age=23
import org.junit.Test; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class TestPropertyPlaceHoder2 {
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${name}")
private String name;
@Value("${age}")
private int age; @Test
public void testResource() {
System.out.println("username: " + username);
System.out.println("password: " + password);
System.out.println("name: " + name);
System.out.println("age: " + age);
}
}
/* username: anqi password: 123456 name: anqi age: 23 */

方式2

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<context:property-placeholder location="classpath:db.properties,classpath:db2.properties"/> </beans>

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

 jdbc.username
jdbc.password

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

<context:property-placeholder location="classpath:db.properties,classpath:db2.properties"
system-properties-mode="NEVER"/> SYSTEM_PROPERTIES_MODE_FALLBACK:在解析一个占位符的变量的时候。假设不能获取到该变量的值。就会拿系统属性来尝试,
SYSTEM_PROPERTIES_MODE_OVERRIDE:在解析一个占位符的时候。会先用系统属性来尝试,然后才会用指定的属性文件,
SYSTEM_PROPERTIES_MODE_NEVER:从来都不会使用系统属性来尝试。

三. Java 编码方式

采取编码的方式显然更加灵活,当我们在做一个项目时,在线下本地跑和在服务器线上跑时,需要的参数肯定有诸多不同,我们可以通过 xml java 编码的方式来指定采用哪一个配置方案,同一个配置方案中也可以将线上配置文件的地址放在前面,没有线上配置文件就采用本地配置的方式来运行项目。

spring-context.xml

<bean>
<!-- 配置 preoperties文件的加载类 -->
<bean class="com.anqi.testPropertyPlaceHoder.PropertiesUtil">
<!-- 配置方案1 优先级更高 配置方案1找不到 key 才会去配置方案 2 里面找-->
<property name="locations">
<list>
<!-- 这里支持多种寻址方式:classpath 和 file -->
<!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
<!--<value>file:/conf/localpro.properties</value>-->
<value>classpath:db.properties</value>
<value>classpath:db2.properties</value>
</list>
</property>
<!-- 配置方案2 -->
<property name="programConfig">
<list>
<value>classpath:db3.properties</value>
</list>
</property>
</bean>
</beans>

db.properties

jdbc.driverClass==net.sourceforge.jtds.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?
jdbc.username=anqi jdbc.
password=123456
pro=1
version=db1

db2.properties

name=anqi
age=23
pro=2
version=db2

db3.properties

pro=3

dev.properties

company=abc version=dev.config

读取配置的工具类

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import java.io.File;
import java.io.IOException;
import java.util.*; public class PropertiesUtil extends PropertyPlaceholderConfigurer { private static Resource electResource; private static Properties configProperties = new Properties();
private static Properties programProperties = new Properties(); public PropertiesUtil() {} /**
* 根据 spring-context 配置文件中的配置,来将项目下对应的 properties 文件加载到系统中
* 并且经过特殊处理 db2.properties 不允许覆盖掉 db1.properties 中相同的 key
* @param locations
*/
public void setLocations(Resource... locations) {
List<Resource> existResourceList = new ArrayList<>(); Resource devConfig = getDevConfig();
if (devConfig != null) {
existResourceList.add(devConfig);
} Resource resource;
for(int i = 0; i < locations.length; ++i) {
resource = locations[i];
if (resource.exists()) {
existResourceList.add(resource);
//dev db.properties db2.properties
}
} Collections.reverse(existResourceList);
//db2.properties db.properties dev if (!existResourceList.isEmpty()) {
electResource = existResourceList.get(existResourceList.size() - 1);
//dev
} try {
configProperties.load(electResource.getURL().openStream());
if (existResourceList != null && existResourceList.size() > 1) {
for(int i = existResourceList.size() - 2; i >= 0; --i) {
Properties backupConfig = new Properties();
//从后往前加载 db1 db2
backupConfig.load(((Resource)existResourceList.get(i)).getURL().openStream()); Iterator iterator = backupConfig.keySet().iterator(); //通过后面新添加的 db3.properties、db4.peoperties 进行更新 db.properties
//添加没有的 key 不能覆盖前面的 key
while(iterator.hasNext()) {
Object key = iterator.next();
if (!configProperties.containsKey(key)) {
configProperties.put(key, backupConfig.get(key));
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 将 programConfig 的配置方案加载到 programeConfig 中
* (即将 db3.properties 加载到 programeConfig)
* 包含运行时方案(运行时配置优先级最高)会覆盖 key 相同的 value
* @param locations
*/
public void setProgramConfig (Resource... locations){ List<Resource> existResourceList = new ArrayList<>(); Resource resource;
for(int i = 0; i < locations.length; ++i) {
resource = locations[i];
if (resource.exists()) {
existResourceList.add(resource);
}
} if (!existResourceList.isEmpty()) {
try {
Iterator<Resource> iterator = existResourceList.iterator();
while (iterator.hasNext()) {
resource = iterator.next();
programProperties.load(resource.getURL().openStream());
}
} catch (IOException e) {
e.printStackTrace();
}
} Resource devConfig = getDevConfig();
if (devConfig != null) {
try {
Properties devProperties = new Properties();
devProperties.load(devConfig.getURL().openStream());
Iterator iterator = devProperties.keySet().iterator(); while(iterator.hasNext()) {
Object key = iterator.next();
programProperties.put(String.valueOf(key),
devProperties.getProperty(String.valueOf(key), ""));
}
} catch (Exception e) {
e.printStackTrace();
}
}
} /**
* 在运行期间传入配置参数(可以将配置文件放在本机或服务器上)
* @return
*/
private static Resource getDevConfig() {
String s = System.getProperty("dev.config", "");
File devConfig = new File(s);
return !s.trim().equals("") && devConfig.exists() && devConfig.isFile() ?
new FileSystemResource(s) : null;
} /**
* 外部访问 properties 配置文件中的某个 key
* @param key
* @return
*/
public static String get(String key){
return programProperties.containsKey(key) ?
programProperties.getProperty(key) : configProperties.getProperty(key);
} public static void show() {
System.out.println("db_1 keys: "+configProperties.keySet());
System.out.println("db_2 keys: "+programProperties.keySet());
}
}

测试类

package com.anqi.testPropertyPlaceHoder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestPropertyPlaceHoder {
public static void main(String[] args) {
ApplicationContext al = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
PropertiesUtil.show();
System.out.println(PropertiesUtil.get("version")); //-Ddev.config=/dev.properties 传入运行时参数
System.out.println(PropertiesUtil.get("company"));
System.out.println(PropertiesUtil.get("pro"));
//db_1 keys: [name, jdbc.password, version, company, jdbc.url, pro, jdbc.driverClass, jdbc.username, age]
//db_2 keys: [company, version, pro]
//dev.config
//abc
//3
}
}

使用Spring中的PropertyPlaceholderConfigurer读取文件的更多相关文章

  1. Spring 中 用 ${xxx} 读取properties文件的说明

    properties 如果在 spring 中通过 PropertyPlaceholderConfigurer 加载,当spring 中需要 用到 properties 中的一些 key 和value ...

  2. C#中StreamReader类读取文件使用示例

    C#中StreamReader类读取文件使用示例 1.需要导入的命名空间是:System.IO; 2.操作的是字符,所以打开的是文本文件. 常用属性:   CurrentEncoding:对象正在使用 ...

  3. 在Spring Boot快捷地读取文件内容的若干种方式

    引言: 在Spring Boot构建的项目中,在某些情况下,需要自行去读取项目中的某些文件内容,那该如何以一种轻快简单的方式读取文件内容呢?  基于ApplicationContext读取 在Spri ...

  4. android中常用的读取文件的用法如下

    1. 从resource的raw中读取文件数据: String res = ""; try{ //得到资源中的Raw数据流 InputStream in = getResource ...

  5. Spring中利用applicationContext.xml文件实例化对象和调用方法

    Spring中实例化对象和调用方法入门 1.jar包和xml的准备 已上传至百度云盘,链接: https://pan.baidu.com/s/1CY0xQq3GLK06iX7tVLnp3Q 提取码: ...

  6. Go_18: Golang 中三种读取文件发放性能对比

    Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...

  7. Golang 中三种读取文件发放性能对比

    Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...

  8. java 在MySQL中存储文件,读取文件(包括图片,word文档,excel表格,ppt,zip文件等)

    转自:https://blog.csdn.net/u014475796/article/details/49893261 在设计到数据库的开发中,难免要将图片或文档文件(如word)插入到数据库中的情 ...

  9. Spring中配置和读取多个Properties文件--转

    public class PropertiesFactoryBeanextends PropertiesLoaderSupportimplements FactoryBean, Initializin ...

随机推荐

  1. Tomcat源码分析 (九)----- HTTP请求处理过程(二)

    我们接着上一篇文章的容器处理来讲,当postParseRequest方法返回true时,则由容器继续处理,在service方法中有connector.getService().getContainer ...

  2. LoRaWAN_stack移植笔记(三)__SPI

    stm32相关的配置 由于例程使用的主控芯片为STM32L151C8T6,而在本设计中使用的主控芯片为STM32L051C8T6,内核不一样,并且Cube库相关的函数接口及配置也会有不同,所以芯片的驱 ...

  3. 玩转SpringBoot 2 快速搭建 | Spring Initializr 篇

    SpringBoot 为我们提供了外网 Spring Initializr 网页版来帮助我们快速搭建 SpringBoot 项目,如果你不想用 IDEA 中的插件,这种方式也是不错的选择.闲话少说,直 ...

  4. python画混淆矩阵(confusion matrix)

    混淆矩阵(Confusion Matrix),是一种在深度学习中常用的辅助工具,可以让你直观地了解你的模型在哪一类样本里面表现得不是很好. 如上图,我们就可以看到,有一个样本原本是0的,却被预测成了1 ...

  5. POJ 3186

    题意略. 思路:有一点区间dp的意思. 我令dp[ i ][ j ]表示:区间[1 , i]和区间[j , N]按某种顺序插值排好,所能获得的最大值. 状态转移方程:dp[ i ][ j ] = ma ...

  6. GCN和GCN在文本分类中应用

    1.GCN的概念        传统CNN卷积可以处理图片等欧式结构的数据,却很难处理社交网络.信息网络等非欧式结构的数据.一般图片是由c个通道h行w列的矩阵组成的,结构非常规整.而社交网络.信息网络 ...

  7. 分布式任务调度框架 Azkaban —— Flow 2.0 的使用

    一.Flow 2.0 简介 1.1 Flow 2.0 的产生 Azkaban 目前同时支持 Flow 1.0 和 Flow2.0 ,但是官方文档上更推荐使用 Flow 2.0,因为 Flow 1.0 ...

  8. Python实现的一些常见简单问题(持续更新)

    提纲: 1.汉诺塔 2.找到某个范围内的所有质数 3.杨辉三角 4.用闭包实现一个计数器,调用一次计数器加1 5.将类构造成可迭代对象,实现斐波那契数列 ...... 1.汉诺塔(汉诺塔) 用递归函数 ...

  9. Delphi - 通过WinAPI WinExec直接调用系统工具

    看如下代码: WinExec('mspaint.exe', SW_SHOWNORMAL); // SW_SHOWNORMAL = 1 系统画图 WinExec('write.exe', SW_SHOW ...

  10. C#装箱与拆箱总结

    装箱和拆箱是值类型和引用类型之间相互转换是要执行的操作.  1. 装箱在值类型向引用类型转换时发生 2. 拆箱在引用类型向值类型转换时发生 光上述两句话不难理解,但是往深处了解,就需要一些篇幅来解释了 ...