先准备测试程序:

package org.jediael.util;
import static org.junit.Assert.*;
import org.junit.Test; public class BasicConfigurationTest {
@Test
public 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. spring入门(二)【加载properties文件】

    在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...

  2. Spring加载properties文件的属性的值

    要使用配置文件的值首先在spring.xml配置加载properties文件 <context:property-placeholder location="classpath:ife ...

  3. java加载properties文件的六中基本方式实现

    java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...

  4. 加载properties文件的三种方法

    源代码: package a.one; import java.io.FileInputStream; import java.io.InputStream; import java.util.Pro ...

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

    在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...

  6. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(12):XML配置自动扫描包,自动加载*.properties文件

    一.XML和注解组合使用 前几篇的测试案例都是在Java类中配置,现在换一种使用方式,在XML中配置,使Spring IoC容器在启动之后自动去扫描配置的包路径,扫描加载指定路径下的propertie ...

  7. java加载properties文件的六种方法总结

    java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...

  8. xml文件 加载properties文件的两种方法与注意事项

    1.遇到的问题: 配置redisSpringContext.xml 时,遇到 properties加载失败,提示BeanDefinitionStoreException  和   java.lang. ...

  9. Java开发学习(八)----IOC/DI配置管理第三方bean、加载properties文件

    前面的博客都是基于我们自己写的类,现在如果有需求让我们去管理第三方jar包中的类,该如何管理? 一.案例:数据源对象管理 本次案例将使用数据源Druid和C3P0来配置学习下. 1.1 环境准备 学习 ...

  10. spring 如何动态加载properties文件的内容

    1. 在xml中配置你的properties路径: <bean id="messageSource" class="org.springframework.cont ...

随机推荐

  1. sqlplus 一次奇葩问题 HTTP proxy setting has incorrect value

    y@y:~$ sqlplus Error 46 initializing SQL*PlusHTTP proxy setting has incorrect valueSP2-1502: The HTT ...

  2. Windows 8.1 with update 官方最新镜像汇总(全)

    Windows 8.1 with update 官方最新镜像汇总,发布日期: 2014/12/16,Microsoft MSDN. 镜像更新日志: 12/29:32位大客户专业版中文版12/24:64 ...

  3. Windows 8.1 正式版镜像下载大全

    该系统已有更新的版本,请转至<Windows 8.1 with update 官方最新镜像汇总>下载. [声明:所有资源均来自于网络,请购买正版授权后再使用.]Win8.1 正式版原版镜像 ...

  4. 【转】Git与Repo入门----不错

    原文网址:http://www.cnblogs.com/angeldevil/p/3238470.html Git与Repo入门   版本控制 版本控制是什么已不用在说了,就是记录我们对文件.目录或工 ...

  5. JS获取按下的键盘字符

    <html> <head> KeyPress Test!<hr> <script language="javascript"> fu ...

  6. 【转】使用vnc连接linux服务器方便hadoop开发调试

    VNC(Virtual Network Computing)它能将完整的窗口界面通过网络,传输到另一台计算机的屏幕上. 类似的软件在Windows服务器中包含的"Terminal Serve ...

  7. Direct3D 对X模型载入

    今天我们来学习Direct3D对模型的导入使用,Direct3D支持.X模型文件导入使用,.X文件是微软定义的3D模型文件格式,其中包含网格,动画,纹理等等一些信息. 目前3DS Max 和 Maya ...

  8. PC--CSS维护

    一.在样式表开头添加一个注释块,用以描述这个样式表的创建日期.创建者.标记等备注信息. /**Site: www.daqianduan.com*Author: 浩子*Updated: 2010.5.7 ...

  9. Oracle 学习笔记 19 -- 触发器和包浅析(PL/SQL)

    触发器是存放在数据库中的一种特殊类型的子程序.不能被用户直接调用,而是当特定事件或操作发生时由系统自己主动 调用执行.触发器不能接受參数.所以执行触发器就叫做触发或点火.Oracle事件指的是数据库的 ...

  10. uva 11210 Chinese Mahjong(暴力搜索)

    Chinese Mahjong Mahjong () is a game of Chinese origin usually played by four persons with tiles res ...