Properties常用于项目中参数的配置,当项目中某段程序需要获取动态参数时,就从Properties中读取该参数,使程序是可配置的、灵活的。

  有些配置参数要求立即生效,有些则未必:

  一、实时性要求非常高。项目中,有些参数要求实时性非常高,即在系统运行中,IT人员修改了该参数值,该新参数值要求立即在程序中生效;

  二、实时性要求不高。其实,并不是每个配置参数都要求实时性那么高,有些配置参数基本不会在项目运行当中修改,或即使在运行当中修改,也只要求其在下一次项目启动时生效。

  针对第二种情况,鉴于程序读取Properties文件,IO损耗大、效率较低的现状,我们可以在项目启动时,预先将Properties的信息缓存起来,以备程序运行当中方便、快捷地使用。

  初步想法:在项目启动加载Listener时,将需要缓存的Properties以键值对形式缓存起来。

kick off:

首先,需要一个类存储Properties,并提供接口实现“缓存Properties”和“读取Properties”的动作

 package com.nicchagil.propertiescache;

 import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties; import org.apache.log4j.Logger; public class PropertiesCacher { private static final Logger logger = Logger.getLogger(PropertiesCacher.class); // Properties Cache
public static Map<String, Properties> pMap = new Hashtable<String, Properties> (); /**
* Set properties to properties cache
* @param pName
* @throws IOException
*/
public static void setProperties(String pName) throws IOException { Properties properties = new Properties();
InputStream is = null; try {
is = PropertiesCacher.class.getResourceAsStream(pName);
properties.load(is); } finally { if (is != null) {
is.close();
} } logger.info("Load to properties cache : " + pName);
pMap.put(pName, properties);
} /**
* Get properties by properties path
* @param pName
* @return
*/
public static Properties getProperties(String pName) {
return pMap.get(pName);
} /**
* Get properties value by properties path & key
* @param pName
* @param key
* @return
*/
public static String getPropertiesValue(String pName, String key) {
if (pMap.get(pName) == null) {
return "";
} return pMap.get(pName).getProperty(key);
} }

PropertiesCacher

然后,我们需要在项目启动时,触发“缓存Properties”这个动作,这里使用Listener

 package com.nicchagil.propertiescache;

 import java.io.IOException;

 import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.apache.log4j.Logger; public class PropertiesListener implements ServletContextListener { private final Logger logger = Logger.getLogger(PropertiesListener.class); @Override
public void contextDestroyed(ServletContextEvent event) { } @Override
public void contextInitialized(ServletContextEvent event) {
try {
// Load config.properties
PropertiesCacher.setProperties("/resource/config.properties"); // Load log4j.properties
PropertiesCacher.setProperties("/log4j.properties"); } catch (IOException e) {
// TODO Auto-generated catch block logger.error(e.getMessage(), e);
e.printStackTrace();
}
} }

PropertiesListener

作为web项目,配置了Listener,当然需要在web.xml注册一下了

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>XlsExporterDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.nicchagil.propertiescache.PropertiesListener</listener-class>
</listener>
<servlet>
<description></description>
<display-name>DebugPropertiesCacheServlet</display-name>
<servlet-name>DebugPropertiesCacheServlet</servlet-name>
<servlet-class>com.nicchagil.propertiescache.DebugPropertiesCacheServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DebugPropertiesCacheServlet</servlet-name>
<url-pattern>/DebugPropertiesCacheServlet</url-pattern>
</servlet-mapping>
</web-app>

web.xml

最后,自己新建俩properties用于测试,一个是log4j.properties,放在编译根路径下;一个是config.properties,放在编译根路径的resource文件夹下。key值和value值自定义。

这两个properties是本人测试用的,当然你也可以用自己滴,但需要相应地修改PropertiesListener的加载动作哦~

dà gōng gào chéng

现在写一个简单滴Servlet来测试一下是否能成功读取,其中这个Servlet在上述滴web.xml一并注册了,可见“DebugPropertiesCacheServlet”

 package com.nicchagil.propertiescache;

 import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DebugPropertiesCacheServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public DebugPropertiesCacheServlet() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pfile = request.getParameter("pfile");
String key = request.getParameter("key"); if (pfile == null || key == null) {
System.out.println(PropertiesCacher.pMap);
} if (pfile != null && key == null) {
System.out.println(PropertiesCacher.getProperties(pfile));
} if (pfile != null && key != null) {
System.out.println(PropertiesCacher.getPropertiesValue(pfile, key));
} } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
} }

DebugPropertiesCacheServlet

最后,做个简单的测试,使用浏览器分别访问以下url(其中参数可能需要自行改一下),并查看console是否打印正确滴信息

 http://localhost:8080/XlsExporterDemo/DebugPropertiesCacheServlet

 http://localhost:8080/XlsExporterDemo/DebugPropertiesCacheServlet?pfile=/resource/config.properties

 http://localhost:8080/XlsExporterDemo/DebugPropertiesCacheServlet?pfile=/resource/config.properties&key=url

URL

Oh year,成功了!!!

针对Properties中实时性要求不高的配置参数,用Java缓存起来的更多相关文章

  1. 针对系统中磁盘IO负载过高的指导性操作

    针对系统中磁盘IO负载过高的指导性操作 主要命令:echo deadline > /sys/block/sda/queue/scheduler 注:以下的内容仅是提供参考,如果磁盘IO确实比较大 ...

  2. 磁盘IO过高时的处理办法 针对系统中磁盘IO负载过高的指导性操作

    磁盘IO过高时的处理办法 针对系统中磁盘IO负载过高的指导性操作 主要命令:echo deadline > /sys/block/sda/queue/scheduler 注:以下的内容仅是提供参 ...

  3. nginx 高并发配置参数(转载)

    声明:原文章来自http://blog.csdn.net/oonets334/article/details/7528558.如需知道更详细内容请访问. 一.一般来说nginx 配置文件中对优化比较有 ...

  4. nginx 高并发配置参数

    一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1.  worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu ...

  5. 在CentOS 6.5 中安装JDK 1.7 + Eclipse并配置opencv的java开发环境(二)

    一.安装JDK 1.7 1. 卸载OpenJDK rpm -qa | grep java rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.50.1.11.5. ...

  6. 【原创】有利于提高xenomai 实时性的一些配置建议

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正. @ 目录 一.影响因素 1.硬件 2.BISO(X86平台) 3.软件 4. 缓存使用策略与GPU 二.优化措施 1. BIO ...

  7. (转)AIX 中 Paging Space 使用率过高的分析与解决

    AIX 中 Paging Space 使用率过高的分析与解决 原文:https://www.ibm.com/developerworks/cn/aix/library/au-cn-pagingspac ...

  8. Linux 2.6 内核实时性分析 (完善中...)

      经过一个月的学习,目前对linux 下驱动程序的编写有了入门的认识,现在需要着手实践,编写相关的驱动程序. 因为飞控系统对实时性有一定的要求,所以先打算学习linux 2.6 内核的实时性与任务调 ...

  9. 物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)

    物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)   2015年11月14日|    by: nbboy|    Category: 系统设计, 缓存设计, 高性能系统 摘要 ...

随机推荐

  1. Python Module和Package辨析

    Python 基础学习 说明 这不是最基础的新手教程,如需了解Python的数据类型.变量等基础内容,请移步:https://docs.python.org/2/tutorial/index.html ...

  2. 关于CodeFirst的使用教程

    请参考:http://www.cnblogs.com/lxblog/archive/2013/05/22/3092428.html 很全面实用,谢谢作者的付出!

  3. js 面向对象式编程

    1.声明一个函数,在函数内进行初始化操作,,函数不能有返回值2.把需要的参数传递进去,参数最好以对象形式传入,如果有默认的设置默认参数3.把传入的参数都保存到对象的属性上面4.把初始化操作中需要用到的 ...

  4. Linux下MySQL链接被防火墙阻止

    Linux下安装了MySQL,不能从其它机器访问 帐号已经授权从任意主机进行访问 vi /etc/sysconfig/iptables 在后面添加 -A RH-Firewall-1-INPUT -m ...

  5. 微信小程序-通知滚动小提示

    代码地址如下:http://www.demodashi.com/demo/14044.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  6. HDUOJ--Bone Collector

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. Chrome禁用缓存

    Chrome默认对JS和CSS等静态资源进行缓存,对HTML不启用缓存. 在开发阶段,我们想要更改之后马上看到效果,那就必须禁用JS和CSS. 快捷键是F12+F1,F12相当于打开dev-tool, ...

  8. shell脚本条件判断

    http://blog.csdn.net/ws_zll/article/details/7515310

  9. Ubuntu用户root密码设置

    我们在安装Ubuntu后发现个问题,就是不像Linux系统那样会在安装过程中设置root的密码,那以后如果需要root的权限时该如何操作呢? Ubuntu里有个命令叫sudo,是以管理员的身份运行命令 ...

  10. jQuery on() 方法问题

    <!DOCTYPE html><html><head><script src="https://cdn.bootcss.com/jquery/1.1 ...