2.自己动手写Java Web框架-读取配置

来源:互联网 发布:mysql 分组 字段累加 编辑:程序博客网 时间:2024/05/16 10:57

jw的github地址是https://github.com/menyouping/jw

读取配置文件

Web工程都是从WEB-INF/web.xml开始。Spring中接着会转到applicationContext.xml读取xml中的各种配置,由Spring管理各项任务。jw框架没有使用xml做IOC管理,而是采用properties文件保存必须的配置。

因此,本文主要讲解如何读取项目的配置。

查找配置文件

为了查找固定后缀名的文件,创建类jw-core/src/main/java/com/jw/util/FileUtils.java

package com.jw.util;import java.io.File;import java.io.FileFilter;import java.io.IOException;import java.net.URL;import java.net.URLDecoder;import java.util.Arrays;import java.util.LinkedList;import java.util.List;public class FileUtils {    public static List<File> findFiles(final String fileExtension) {        List<File> files = new LinkedList<File>();        try {            URL url = FileUtils.class.getResource("/");            if (url == null)                return files;            String protocol = url.getProtocol();            if ("file".equals(protocol)) {// 如果是以文件的形式保存在服务器上                String filePath = URLDecoder.decode(url.getFile(), "UTF-8");// 获取包的物理路径                File dir = new File(filePath);                if (!dir.exists() || !dir.isDirectory())                    return files;                File[] targetFiles = dir.listFiles(new FileFilter() {                    public boolean accept(File file) {                        return file.getName().endsWith(fileExtension);                    }                });                if (targetFiles != null && targetFiles.length > 0) {                    files.addAll(Arrays.asList(targetFiles));                }            }        } catch (IOException e) {            e.printStackTrace();        }        return files;    }}

如何查找classpath下的资源文件可参看此文 http://www.cnblogs.com/yejg1212/p/3270152.html 或者 http://swiftlet.net/archives/868

读取配置

为了循环读取所有配置文件,创建类jw-core/src/main/java/com/jw/util/ConfigUtils.java。

package com.jw.util;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.List;import java.util.Properties;public class ConfigUtils {    private static Properties config = new Properties();    static {        List<File> files = FileUtils.findFiles(".properties");        Properties prop = null;        for (File file : files) {            prop = new Properties();            try {                prop.load(new FileReader(file));                config.putAll(prop);            } catch (IOException e) {                e.printStackTrace();            }        }    }    public static void init() {    }    public static String getProperty(String key) {        return config.getProperty(key);    }}

ConfigUtils采用了代理模式,所有的请求都转给config这个Properties对象。

读取资源文件的逻辑在类的static代码块中实现。init()方法是一个空实现,这样在程序启动时调用ConfigUtils.init(), 系统就会调用static代码块完成配置数据加载。

测试

修改pom.xml

测试用例写在jw-web中,首先在jw-web的pom.xml中加入依赖jw-core.

<dependency>    <groupId>com.jw</groupId>    <artifactId>jw-core</artifactId>    <version>0.0.1</version></dependency>

添加资源文件

新增资源文件jw-web/src/test/resources/config.properties

jw.welcome=Nice to meet you!jw.author=Jay Zhangjw.birthday=2016

添加测试类

新增类jw-web/src/test/java/com/jw/test/ConfigTest.java

package com.jw.test;import com.jw.util.ConfigUtils;public class ConfigTest {    public static void main(String[] args) {        ConfigUtils.init();        String msg = "jw was created by %s in %s, %s";        System.out.println(String.format(msg, ConfigUtils.getProperty("jw.author"),                ConfigUtils.getProperty("jw.birthday"), ConfigUtils.getProperty("jw.welcome")));    }}

build

命令行中cd到jw-parent文件夹内,输入以下命令

mvn clean install

待成功后,运行ConfigTest

得到

jw was created by Jay Zhang in 2016, Nice to meet you!

本节资源地址: https://github.com/menyouping/jw-example/blob/master/2/jw-parent.zip

0 0
原创粉丝点击