【设计模式:单例模式】使用单例模式加载properties文件
先准备测试程序:
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文件的更多相关文章
- spring入门(二)【加载properties文件】
在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...
- Spring加载properties文件的属性的值
要使用配置文件的值首先在spring.xml配置加载properties文件 <context:property-placeholder location="classpath:ife ...
- java加载properties文件的六中基本方式实现
java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...
- 加载properties文件的三种方法
源代码: package a.one; import java.io.FileInputStream; import java.io.InputStream; import java.util.Pro ...
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(12):XML配置自动扫描包,自动加载*.properties文件
一.XML和注解组合使用 前几篇的测试案例都是在Java类中配置,现在换一种使用方式,在XML中配置,使Spring IoC容器在启动之后自动去扫描配置的包路径,扫描加载指定路径下的propertie ...
- java加载properties文件的六种方法总结
java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...
- xml文件 加载properties文件的两种方法与注意事项
1.遇到的问题: 配置redisSpringContext.xml 时,遇到 properties加载失败,提示BeanDefinitionStoreException 和 java.lang. ...
- Java开发学习(八)----IOC/DI配置管理第三方bean、加载properties文件
前面的博客都是基于我们自己写的类,现在如果有需求让我们去管理第三方jar包中的类,该如何管理? 一.案例:数据源对象管理 本次案例将使用数据源Druid和C3P0来配置学习下. 1.1 环境准备 学习 ...
- spring 如何动态加载properties文件的内容
1. 在xml中配置你的properties路径: <bean id="messageSource" class="org.springframework.cont ...
随机推荐
- Python(四):数字连珠2
对上次的代码作了一些修改.在码的过程中发现,最核心的部分是在横向.竖向和两个对角方向上找到5个以上相同的数字. 自己的思路是将x行y列所在的x行.y列,以及以此为交叉点的两点对角线上的数字,转化成字符 ...
- linux 文件查找和压缩工具
文件查找 1,which命行查找可执行文件,which 只会搜索系统$PATH目录 2,whereis,查找可执行文件,并显示出此文件的man page文件,并且可以查找到系统的库目录 3,locat ...
- 小试牛刀——爬topit.me的图片,附github简易上传教程
接触了scrapy ,发现爬虫效率高了许多,借鉴大神们的文章,做了一个爬虫练练手: 我的环境是:Ubuntu14.04 + python 2.7 + scrapy 0.24 目标 topit.me 一 ...
- [TYVJ] P1017 冗余关系
冗余关系 背景 Background 太原成成中学第3次模拟赛 第4题 描述 Description Mrs.Chen是一个很认真很称职的语文老师 ......所以,当她看到学生作文里的人物关系描 ...
- Oracle trunc函数
--Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual --2011 ...
- find详解
一.简介 在目录结构中搜索文件,并执行指定的操作. 二.语法 find path -option [ -print ] [ -exec -ok command ] {} \;path: find命令所 ...
- uva 10401 Injured Queen Problem(dp)
题目链接:10401 - Injured Queen Problem 题目大意:给出一个字符串,要求在n * n(n为字符串的长度)的棋盘上摆放n个受伤的皇后,受伤的皇后只能攻击到同一列和它周围8个格 ...
- C++字符串指针与字符数组的区别
今天发现这样一个问题 #include <iostream> using namespace std; int main() { ]; strcpy_s(ch1,");//编译通 ...
- 选择排序(Selection Sort)
选择排序就是在选择数组元素上做文章,关键是如何选择?选择的标准是什么?选择之后放在哪?所有这些都是选择排序的问题. 选择排序算法中,通常会有以下操作: 从数组第一个元素开始. 遍历整个数组,找到最小的 ...
- WPF动画
System.Windows.Media.Animation 这个命名空间中,包含了三种动画类:线性插值动画类(17个).关键帧动画(22个).路径动画(3个). 线性插值动画类(17个)如下: By ...