Spring中的各种Utils(二):PropertiesLoaderUtils详解

今天介绍的是PropertiesLoaderUtils,这个工具类主要是针对Properties文件的加载操作,在Spring对.properties文件和.factories文件的操作都有使用到。

原文地址

更多详细内容请阅读原文!
先来简单看看这个类提供的有用方法:

//从一个资源文件加载Properties;
Properties loadProperties(Resource resource) throws IOException;

//加载资源文件,传入的是提供了编码的资源类(EncodedResource);和上面方法基本一致;
Properties loadProperties(EncodedResource resource) throws IOException;

//从一个资源类中加载资源,并填充到指定的Properties对象中;
void fillProperties(Properties props, Resource resource) throws IOException;

//从一个编码资源类中加载资源,并填充到指定的Properties对象中;和上面方法基本一致;
void fillProperties(Properties props, EncodedResource resource)throws IOException;

//根据资源文件名称,加载并合并classpath中的所有资源文件;
Properties loadAllProperties(String resourceName) throws IOException;

//从指定的ClassLoader中,根据资源文件名称,加载并合并classpath中的所有资源文件;
Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException;

loadProperties

测试方法很简单,我们首先准备一个test.properties文件,放到resources下面:

#test.properties
key=value
key2=中文

测试代码:

@Test
public void testLoadPropertiesResource() throws Exception {
    Properties ret = PropertiesLoaderUtils
            .loadProperties(new ClassPathResource("test.properties"));
    assertEquals("value", ret.getProperty("key"));
    assertEquals("中文", ret.getProperty("key2"));
}

测试完成。没有太大难度;
但是,其实Properties对象不仅仅支持.properties文件,还支持XML格式的资源配置文件。

那么我们就可以有以下测试。创建一个text.xml文件在classpath下:

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>一些自定义说明</comment>
    <entry key="key">value</entry>
    <entry key="key2">中文</entry>
</properties>

测试代码:

@Test
public void testLoadPropertiesResourceXml() throws Exception {
    Properties ret = PropertiesLoaderUtils
            .loadProperties(new ClassPathResource("test.xml"));
    assertEquals("value", ret.getProperty("key"));
    assertEquals("中文", ret.getProperty("key2"));
}

测试通过。当然,我们是非常不建议使用XML的方式来做配置的。

接下来使用EncodeResource来测试,EncodeResource在Resource上增加了字符编码设置。同样使用之前的test.properties:

@Test
public void testLoadPropertiesEncodedResource() throws Exception {
    Properties ret = PropertiesLoaderUtils.loadProperties(
            new EncodedResource(new ClassPathResource("test.properties"),
                    "UTF-8"));
    assertEquals("value", ret.getProperty("key"));
    assertEquals("中文", ret.getProperty("key2"));
}

可以看到,只是在之前的ClassPathResource基础之上包装了UTF-8字符编码的EncodeResource;

loadAllProperties

loadProperties方法,是从当前classpath下加载properties文件,如果使用loadAllProperties,可以从当前classpath下加载所有的相同名称的properties文件,并执行合并。

来完成一个测试。把上一个例子中的test.properties文件保留,放到src/main/resources中;然后在src/test/resources目录下再创建一个test.properties文件,内容为:

key3=value

测试代码:

@Test
public void testLoadAllPropertiesString() throws Exception {
    Properties ret = PropertiesLoaderUtils
            .loadAllProperties("test.properties");
    assertEquals("value", ret.getProperty("key"));
    assertEquals("value", ret.getProperty("key3"));
}

执行测试通过;可以看到,main下的test.properties和test下的test.properties都被识别并执行了合并;

那如果在test/resources中的test.properties中增加

key=update

再次执行测试,仍然通过。

说明在多个配置文件中如果有重复key,以最近的classpath为准(比如当前应用的properties内容会优先于jar包中的properties)。

但是,如果这种情况下,使用loadProperties方法,那么只会加载到test/resources中的test.properties;

fillProperties

fillProperties方法其实是loadProperties方法的真正调用方法。先来看看测试代码:

@Test
public void testFillPropertiesPropertiesResource() throws Exception {
    Resource res = new ClassPathResource("test.properties");
    Properties ret = new Properties();
    PropertiesLoaderUtils.fillProperties(ret, res);
    assertEquals("value", ret.getProperty("key"));
}

使用非常简单。

我们来看一下loadProperties方法的源代码:

public static Properties loadProperties(EncodedResource resource) throws IOException {
    Properties props = new Properties();
    fillProperties(props, resource);
    return props;
}

可以看到,其实调用的就是fileProperties方法,而这个方法的实现其实也很简单:

public static void fillProperties(Properties props, EncodedResource resource)
        throws IOException {

    fillProperties(props, resource, new DefaultPropertiesPersister());
}

代理给了fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)方法;只是最后一个参数是一个PropertiesPersister,抽取了一个统一的从XML或者properties资源加载和装载接口;来看看具体实现(我只保留了最基本的关键代码):

static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
        throws IOException {

    InputStream stream = null;
    Reader reader = null;
    try {
        String filename = resource.getResource().getFilename();
        //如果是XML文件,
        if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
            stream = resource.getInputStream();
            persister.loadFromXml(props, stream);
        }else {
            stream = resource.getInputStream();
            persister.load(props, stream);
        }
    }finally {
        //close方法
    }
}

可以看到,其实就是调用了PropertiesPersister的loadFromXml和load方法来分别加载XML或properties;

有兴趣的童鞋可以看看DefaultPropertiesPersister的相关的这两个方法,其实核心还是使用Properties对象的load和loadFromXML方法完成的。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值