java学习(一)-配置文件加载

介绍

  • 背景:在java开发中,往往需要自己定义配置文件、解析方式
  • java类中,进行配置文件加载的类是:Properties
    • Properties的是一个持久的属性值,可保存在流中,或者从流中加载。配置里的每个键值对,都是String类型
    • Properties是一个线程安全的类,多个线程可共享同一个Properties对象:因为load和set操作,使用了syncronized修饰

加载方式:

  • 方式1:文件系统加载
  • 方式2:类加载器加载
  • 方式3【第三方库,这里不做介绍】:使用apache的配置包:org.apache.commons.configuration
  • 方式4:使用注解方式,待完成!!!
  • 示例:
    • 创建配置文件1:ref2.properties,放入到工程的resources目录下:
    cat=reflect2.Cat
    bird=reflect2.Bird
    
    • 创建配置文件2:ref2.yaml,放入到工程的resources目录下:
    cat: reflect2.Cat
    bird: reflect2.Bird
    
    • 创建测试代码:
        class Main1 {
    
        public static void main(String[] args) throws IOException {
            // 方式1:文件系统加载:
            // 问题:文件系统加载的时候,不容易找到配置文件的路径
            System.out.println("################### 方式1:文件系统加载: ###################");
            FileInputStream fis1 = new FileInputStream(
                    "/Users/zhaoyue/codes/springAopMaven/src/main/resources/ref2.properties");
            Properties p1 = new Properties();
            p1.load(fis1);
            System.out.println("获取cat的value:" + p1.getProperty("cat"));
    
            FileInputStream fis2 = new FileInputStream(
                    "/Users/zhaoyue/codes/springAopMaven/src/main/resources/ref2.yaml");
            Properties p2 = new Properties();
            p2.load(fis2);
            System.out.println("获取bird的value:" + p2.getProperty("bird"));
    
            // 方式2:通过类加载器
            // 一般情况下,配置文件都放置在:resources目录下
            System.out.println("################### 方式2:通过类加载器: ###################");
            InputStream resourceAsStream = Main1.class.getClassLoader().getResourceAsStream("ref2.properties");
            Properties p3 = new Properties();
            p3.load(resourceAsStream);
            System.out.println("获取bird的value:" + p3.getProperty("bird"));
        }
    }
    
    • 结果说明:
      不论是properties还是yaml文件,Properties都可进行加载、解析
      通常情况下,优先推荐使用类加载器方式加载配置文件

Properties类:

  • 属性:
    protected Properties defaults;

  • 构造方法:
    public Properties()
    Public Properties(Properties defaults)

  • 加载配置:
    public synchronized void load(Reader reader) throws IOException // 按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
    public synchronized void load(InputStream inStream) throws IOException // 从输入流中读取属性列表(键和元素对)。

  • 获取配置:
    public String getProperty(String key) // 用指定的键在此属性列表中搜索属性
    public String getProperty(String key, String defaultValue) // 用指定的键在属性列表中搜索属性,不存在时设置默认值
    public Enumeration<?> propertyNames() // 返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键。
    public Set stringPropertyNames() // 返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键

  • 修改配置:线程安全
    public synchronized Object setProperty(String key, String value) // 调用 Hashtable 的方法 put。

  • 保存配置:
    public void save(OutputStream out, String comments) // 已废弃,不用care
    public void store(Writer writer, String comments) throws IOException // 以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符。
    public void store(OutputStream out, String comments) throws IOException // 以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流
    public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException // 将指定输入流中由 XML 文档所表示的所有属性加载到此属性表中。
    public void storeToXML(OutputStream os, String comment) throws IOException // 发出一个表示此表中包含的所有属性的 XML 文档。
    public void storeToXML(OutputStream os, String comment, String encoding) throws IOException // 使用指定的编码发出一个表示此表中包含的所有属性的 XML 文档。
    public void list(PrintStream out) // 将属性列表输出到指定的输出流。
    public void list(PrintWriter out) // 将属性列表输出到指定的输出流。

  • 匿名内部类:
    class LineReader:用于读取配置文件中的每一行
    private static class XmlSupport : Xml解析的支持

在这里插入图片描述


  • 简单的使用(保存配置相关这里不做介绍)
        // 方式2:通过类加载器
        // 一般情况下,配置文件都放置在:resources目录下
        System.out.println("################### 方式2:通过类加载器: ###################");
        InputStream resourceAsStream = Main1.class.getClassLoader().getResourceAsStream("ref2.properties");
        Properties p3 = new Properties();
        p3.load(resourceAsStream);
        System.out.println("获取bird的value:" + p3.getProperty("bird"));

        System.out.println("################### 测试get方法 ###################");
        System.out.println("获取birdsss的value(默认为null):" + p3.getProperty("birdsss"));
        System.out.println("获取birdsss的value(可传入默认值):" + p3.getProperty("birdsss", "Nothing"));

        System.out.println("################### 打印所有的属性值 ###################");
        System.out.println("方式1:通过枚举值获取");
        Enumeration<?> enumeration = p3.propertyNames();    // 可火速所有的属性值
        while (enumeration.hasMoreElements()) {
            System.out.println(enumeration.nextElement());
        }
        System.out.println("方式2:set<String>集合");
        System.out.println(p3.stringPropertyNames());

        System.out.println("################### 修改属性值 ###################");
        p3.setProperty("cat", "NoCat");
        System.out.println("修改后的cat为:" + p3.getProperty("cat"));

  • 输出:
################### 方式2:通过类加载器: ###################
获取bird的value:reflect2.Bird
################### 测试get方法 ###################
获取birdsss的value(默认为null):null
获取birdsss的value(可传入默认值):Nothing
################### 打印所有的属性值 ###################
方式1:通过枚举值获取
cat
bird
方式2:set<String>集合
[cat, bird]
################### 修改属性值 ###################
修改后的cat为:NoCat
  • 除了自身去load之外,还可以调用System.getProperties()
        System.out.println("################### 获取系统的属性值 ###################");
        Properties p4 = System.getProperties();
        System.out.println(p4.stringPropertyNames());

输出:可以看到,打印了java运行时的所有入参

################### 获取系统的属性值 ###################
[java.runtime.name, sun.boot.library.path, java.vm.version, gopherProxySet, java.vm.vendor, java.vendor.url, path.separator, java.vm.name, file.encoding.pkg, user.country, sun.java.launcher, sun.os.patch.level, java.vm.specification.name, user.dir, java.runtime.version, java.awt.graphicsenv, java.endorsed.dirs, os.arch, java.io.tmpdir, line.separator, java.vm.specification.vendor, os.name, sun.jnu.encoding, java.library.path, java.specification.name, java.class.version, sun.management.compiler, os.version, user.home, user.timezone, java.awt.printerjob, file.encoding, java.specification.version, user.name, java.class.path, java.vm.specification.version, sun.arch.data.model, java.home, sun.java.command, java.specification.vendor, user.language, awt.toolkit, java.vm.info, java.version, java.ext.dirs, sun.boot.class.path, java.vendor, file.separator, java.vendor.url.bug, sun.cpu.endian, sun.io.unicode.encoding, sun.cpu.isalist]


其它

  • 参考1:https://www.jianshu.com/p/efdd1a526939
  • 参考2:https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
  • 参考3:https://www.iteye.com/blog/liuzidong-831440
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章