5

Java代码读取properties配置文件 - 我没有bug

 1 year ago
source link: https://www.cnblogs.com/LoginX/p/Login_X72.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

读取properties配置文件

package com.easycrud.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @BelongsProject: EasyCrud
 * @BelongsPackage: com.easycrud.utils
 * @Author: xpx
 * @Email: [email protected]
 * @CreateTime: 2023-05-02  16:05
 * @Description: 读取properties配置文件工具类
 * @Version: 1.0
 */

public class PropertiesUtils {
    private static Properties props = new Properties();
    private static Map<String,String> PROPER_MAP = new ConcurrentHashMap<String, String>();

    /**
     * 项目初始化时就去读取配置文件
     */
    static {
        InputStream is = null;
        try{
            is = PropertiesUtils.class.getClassLoader().getResourceAsStream("application.properties");
            props.load(is);

            Iterator<Object> iterator = props.keySet().iterator();
            while (iterator.hasNext()){
                String key = (String) iterator.next();
                PROPER_MAP.put(key,props.getProperty(key));
            }
        }catch (Exception e){

        }finally {
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 对外提供方法通过key取值
     * @param key
     * @return
     */
    public static String getString(String key) {
        return PROPER_MAP.get(key);
    }

    /**
     * 测试能否成功取值
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("测试打印:"+getString("db.driver.name"));
    }
}

新手引导#

Properties#

Properties 继承于 Hashtable。表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串。

ConcurrentHashMap#

HashTable是一个线程安全的类,它使用synchronized来锁住整张Hash表来实现线程安全,即每次锁住整张表让线程独占,相当于所有线程进行读写时都去竞争一把锁,导致效率非常低下。ConcurrentHashMap可以做到读取数据不加锁,并且其内部的结构可以让其在进行写操作的时候能够将锁的粒度保持地尽量地小,允许多个修改操作并发进行,其关键在于使用了锁分段技术。它使用了多个锁来控制对hash表的不同部分进行的修改。对于JDK1.7版本的实现, ConcurrentHashMap内部使用段(Segment)来表示这些不同的部分,每个段其实就是一个小的Hashtable,它们有自己的锁。只要多个修改操作发生在不同的段上,它们就可以并发进行。JDK1.8的实现降低锁的粒度,JDK1.7版本锁的粒度是基于Segment的,包含多个HashEntry,而JDK1.8锁的粒度就是HashEntry(首节点)。

static#

static代码块一般用于初始化类中的静态变量,该静态代码块在类加载过程中的初始化阶段执行,并且只执行一次。

InputStream#

字节输入流。

try...cache...finally#

​ 可能发生异常的代码

}catch(可能发生的异常类型 异常对象名){

​ 当前异常类型的处理方式

}finally{

​ 一定要执行的代码

IOException#

IOException 是在使用流、文件和目录访问信息时引发的异常的基类

e.printStackTrace()#

在命令行打印异常信息在程序中出错的位置及原因。

Class.getClassLoader().getResourceAsStream()#

Class是当前类的Class对象,Class.getClassLoader()是获取当前类的类加载器。类加载器的大概作用是当需要使用一个类时,加载该类的".class"文件,并创建对应的class对象,将class文件加载到虚拟机的内存。getResourceAsStream()是获取资源的输入流。类加载器默认是从classPath路径加载资源。

load()#

Properties的方法是逐行读取properties配置文件,分隔成两个字符串key和value,将他们放进Properties对象中。

Iterator#

迭代器 it 的三个基本操作是 next 、hasNext 和 remove。

调用 it.next() 会返回迭代器的下一个元素,并且更新迭代器的状态。

调用 it.hasNext() 用于检测集合中是否还有元素。

调用 it.remove() 将迭代器返回的元素删除。

keySet()#

如果有一个Map对象,可以使用 map.keySet()方法获取所有的key值。

A=xxx.iterator()#

获取迭代器保存在A中。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK