使用单例模式加载properties文件

@(JAVA)[java]

* 这里只是用于示范单例模式,更好的办法是使用java.util.ResourceBundle解释proerties文件。详见java.util.ResourceBundle的介绍文章 *

先准备测试程序

package org.jediael.util;
import static org.junit.Assert.*;
import org.junit.Test;public class BasicConfigurationTest {@Testpublic void testGetValue(){BasicConfiguration configuration = BasicConfiguration.getInstance();assertTrue(configuration.getValue("key").equals("value"));}
}

其中properties文件中有一行如下:

key=value

优先选择方案三

方式一:懒汉方式

到第一次使用实例时,才加载实例

package org.jediael.util;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class BasicConfiguration {private static BasicConfiguration configuration = null;private Properties pros = null;public static synchronized BasicConfiguration getInstance(){if(configuration == null){configuration = new BasicConfiguration();}return configuration;}public String getValue(String key){return pros.getProperty(key);}private BasicConfiguration(){readConfig();}private void readConfig() {pros = new Properties();InputStream in = null;try {in = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("").getPath() + "search.properties");pros.load(in);} catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally{try {in.close();} catch (IOException e) {e.printStackTrace();}}}
}

上述程序中,产生了BasicConfiguration的一个单例。

好处是只有到第一次调用getInstance才生成对象,节省了空间。不足之处在于同步锁导致有可能执行过慢。

2、饿汉方式

package org.jediael.util;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class BasicConfiguration {private static BasicConfiguration configuration = new BasicConfiguration();private Properties pros = null;public static BasicConfiguration getInstance(){return configuration;}public String getValue(String key){return pros.getProperty(key);}private BasicConfiguration(){readConfig();}private void readConfig() {pros = new Properties();InputStream in = null;try {in = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("").getPath() + "search.properties");pros.load(in);} catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally{try {in.close();} catch (IOException e) {e.printStackTrace();}}}
}

由于BasicConfiguration的实例是static,因此,当类被加载时就会初始化,但这样即使并不需要使用此实例,也会被初始化,导致内存空间的浪费。

方式三:内部类方式

由于初始化放在内部类中,只有当此内部类被使用时,才会进行初始化。从而既节省了空间,也无需同步代码。

package org.jediael.util;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class BasicConfiguration {private Properties pros = null;private static class ConfigurationHolder{private static BasicConfiguration configuration = new BasicConfiguration();}public static BasicConfiguration getInstance(){return ConfigurationHolder.configuration;}public String getValue(String key){return pros.getProperty(key);}private BasicConfiguration(){readConfig();}private void readConfig() {pros = new Properties();InputStream in = null;try {in = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("").getPath() + "search.properties");pros.load(in);} catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally{try {in.close();} catch (IOException e) {e.printStackTrace();}}}
}

使用单例模式加载properties文件相关推荐

  1. 【设计模式:单例模式】使用单例模式加载properties文件

    先准备测试程序: package org.jediael.util; import static org.junit.Assert.*; import org.junit.Test;public cl ...

  2. Spring加载properties文件的两种方式

    2019独角兽企业重金招聘Python工程师标准>>> 在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取pro ...

  3. 使用ResourceBundle加载properties文件

    使用ResourceBundle加载properties文件 @(JAVA)[java] 1.ResourceBundle介绍 说的简单点,这个类的作用就是读取资源属性文件(properties),然 ...

  4. 自动注入、加载 properties 文件、scope 属性、单例设计模式

    一.自动注入 在 Spring 配置文件中对象名和 ref="id"id 名相同使用自动注入,可以不配置<property/> 两种配置办法 2.1 在<bean ...

  5. java加载properties文件的几种方式,java高级面试笔试题

    我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家. 扫描二维码或搜索下图红色VX号,加VX好友,拉你进[程序员面试学习交流群]免费领取.也欢迎各位一起 ...

  6. spring配置中加载properties文件方法

    首先,遇到一个问题,spring配置中加载properties文件配置如下: <context:property-placeholder ignore-unresolvable="tr ...

  7. java不重启服务动态加载properties文件

    动态加载properties文件内容,不需要重启服务! 1 .Maven 工程,在resource下新建一个properties文件 target/classes/config.properties ...

  8. spring加载properties文件顺序

    我们在使用spring是,在配置文件中经常需要使用到<context:property-placeholder location="" />标签.这样系统配置就能直接写 ...

  9. Java中加载properties文件的6种方法

    .使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream( ...

最新文章

  1. nonatomic, retain,weak,strong用法详解
  2. C++编程进阶2(编译器在类内默认生成的函数讨论以及纯虚析构函数)
  3. 为什么 Python 的 Range 要设计成左开右闭区间?
  4. 逆向最大匹配分词算法C#
  5. [机器学习] 半监督学习---伪标签Pseudo-Label
  6. 在运行时访问工件的Maven和SCM版本
  7. aws v2.2.exe_如何在AWS Elastic Beanstalk上部署Rails 5.2 PostgreSQL应用
  8. hook 循环点击事件用哪个_JS 事件循环 event loop,看完你可以答对 90% 的事件循环题...
  9. java设置面板的大小_java – 设置面板的大小
  10. shell 循环控制语句
  11. mysql java 问题_【Java】连接MySQL问题总结
  12. pandas数据处理实践一(简单走一遍)
  13. Mysql 数据库中Exists 关键字的使用
  14. Java学习笔记2——java的安装和配置
  15. QinQ、VLAN Mapping原理和配置
  16. MySQL查询某天(内)的数据
  17. 「模拟8.19 A嚎叫..(set) B主仆..(DFS) C征程..(DP+堆优化)」
  18. 照相机的计算机微处理器芯片是什么,芯片到底是什么?
  19. python批量修改文件的后缀名
  20. Navicat Premium 12 for Mac 破解

热门文章

  1. 【解决没有该选项问题】使Clion运行编译运行单个C/C++文件(Single File Execution插件)
  2. 代码分析+原理图解——棋盘覆盖问题-分治法
  3. [leetcode] 740.删除并获得点数
  4. 数据库原理与应用(SQL Server)笔记 第七章 流程控制语句、系统内置函数
  5. 未公开接口主要指以下哪几类_Java8的 Stream 函数式接口,你了解多少?
  6. MySQL——高阶语句(中)
  7. docker容器的本地局域网yum源优化
  8. 华为鸿蒙OS合作方,华为发布鸿蒙OS元服务,超300家应用合作伙伴加入
  9. 工作日报模板_千份财会人通用工作模板:自动核算工资、财务分析报表等等
  10. java arraylist 字符串_Java ArrayList、string、string[]之间的转换