申明:在一个项目中必不可少的是Logger和错误信息的配置,现在给出在我们常用的处理方法。

—、创建一个ConfigUtils类和他对应的rah.properties文件和Test测试类

ConfigUtis:

package com.rah;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; public class ConfigUtils { private static final String PROPERTIES_FILE = "com/rah/rah.properties"; private static Properties prop = null; static{
InputStream propStream = ConfigUtils.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE);
prop = new Properties();
try {
prop.load(propStream);
} catch (IOException e) {
System.out.println("读取文件失败");
}
} public static String getProperty(String key){
return prop.getProperty(key);
}
}

rah.properties

photoDir=d:/temp/photo
videoDir=d:/temp/video

test

package com.rah;

public class Test {
public static void main(String[] args) {
String photoDir = ConfigUtils.getProperty("photoDir");
String videoDir = ConfigUtils.getProperty("videoDir");
System.out.println("photoDir Path is: " + photoDir);
System.out.println("videoDir path is: " + videoDir);
}
}

测试结果:

photoDir Path is: d:/temp/photo
videoDir path is: d:/temp/video

二、创建MessageManager类、message.properties和测试类Test.

MessageManager

package com.rah;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties; public class MessageManager {
    private static final String PROPERTIES_FILE = "/properties/message.properties";
    private static Properties prop = null;
    static{
        InputStream propStream = MessageManager.class.getResourceAsStream(PROPERTIES_FILE);
        prop = new Properties();
        try {
            prop.load(propStream);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("读取文件失败");
        }
    }
    
    public static String getProperty(String messageCode){
        return prop.getProperty(messageCode);
    }
    
    public static String getProperty(String messageCode, String arg1){
        Object[] args = new Object[1];
        args[0] = arg1;
        
        return getFormatMessage(messageCode, args);
    }
    
    public static String getProperty(String messageCode, String arg1, String arg2){
        Object[] args = new Object[2];
        args[0] = arg1;
        args[1] = arg2;
        
        return getFormatMessage(messageCode, args);
    }
    
    public static String getProperty(String messageCode, String arg1, String arg2, String arg3){
        Object[] args = new Object[3];
        args[0] = arg1;
        args[1] = arg2;
        args[2] = arg3;
        
        return getFormatMessage(messageCode, args);
    }
    
    private static String getFormatMessage(String messageCode, Object[] args) {
        String argMessage = getProperty(messageCode);
        
        return MessageFormat.format(argMessage, args);
    } }

Message.properties

MSG_E00001=password is not correct
MSG_E00002=country is {0}
MSG_E00003=country is {0} provice is {1}
MSG_E00004=country is {0} provice is {1} city is {2}

Test

package com.rah;

public class Test {
public static void main(String[] args) {
System.out.println("MSG_E00001 data is: " + MessageManager.getProperty("MSG_E00001"));
System.out.println("MSG_E00002 data is: " + MessageManager.getProperty("MSG_E00002", "CHINA"));
System.out.println("MSG_E00003 data is: " + MessageManager.getProperty("MSG_E00003", "CHINA", "JIANGXI"));
System.out.println("MSG_E00004 data is: " + MessageManager.getProperty("MSG_E00004", "CHINA", "JIANGXI", "SHANGRAO"));
}
}

测试结果:

MSG_E00001 data is: password is not correct
MSG_E00002 data is: country is CHINA
MSG_E00003 data is: country is CHINA provice is JIANGXI
MSG_E00004 data is: country is CHINA provice is JIANGXI city is SHANGRAO

三、Loger日志输出,其实就是对log4j的一点封装,方便开发人员使用

log4j-1.2.13.jar下载

public class Logger {

    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
.getLogger(org.apache.log4j.Logger.class); public static void debug(String message) {
logger.debug(message);
} public static void debug(String message, Throwable ex) {
logger.debug(message, ex);
} public static void info(String message) {
logger.info(message);
} public static void info(String message, Throwable ex) {
logger.info(message, ex);
} public static void error(String message) {
logger.error(message);
} public static void error(String message, Throwable ex) {
logger.error(message, ex);
} public static void fatal(String message) {
logger.fatal(message);
} public static void fatal(String message, Throwable ex) {
logger.fatal(message, ex);
} public static void warn(String message) {http://i.cnblogs.com/EditPosts.aspx?opt=1
logger.warn(message);
} public static void warn(String message, Throwable ex) {http://i.cnblogs.com/EditPosts.aspx?opt=1
logger.warn(message, ex);
}
}

四、对class.getResourceAsStream()、class.getClassLoader().getResourceAsStream()区别的分析

  思心的网友肯定会发现我上面的两个测试分别采用了class.getResourceAsStream(),和class.getClassLoader().getResourceAsStream().其实一开始我也没有注意,是在查API的时候发现有不同的方法,于是为了试试他们的用法特地采用了不同的写法。

class.getResourceAsStream()会指定的加载的资源路径与当前类所在的包的路径一致

  像上面的MessageManager类如果写成getResourceAsStream("message.properties")则他就只会在ciom.rah包下寻找,此时我们采用"/"开头,那么就会从classpath的根路径开始查找(SRC根目录)getResourceAsStream("/properties/message.properties")就是在SRC目录下创建了properties目录接着创建了message.properties文件。

ClassLoader.gettResourceAsStream()无论要查找的资源前面是否有"/"都是从classpath的根路径下查找。

  像上面的ConfigUtil类getResourceAsStream("/rah.properties")和("rah.properties")都是直接从SRC目录下找rah.properties文件。

最后补充:

程序运行的是最后编译成.class的文件。这个SRC目录下的所有东西都会编译在bin目录下。

  

项目中logger、message错误信息的配置的更多相关文章

  1. SpringBoot项目中应用Jedis和一些常见配置

    优雅的使用Jedis Redis的Java客户端有很多,Jedis是其中使用比较广泛和性能比较稳定的一个.并且其API和RedisAPI命名风格类似,推荐大家使用 在项目中引入Jedis 可以通过Ma ...

  2. 项目中调用ExcelCom组件时的配置流程

    异常提示如下:         Microsoft Office Excel 不能访问文件“*.xls”. 可能的原因有:        1 文件名称或路径不存在.       2 文件正被其他程序使 ...

  3. windows下cmd时复制dos中的内容 错误信息等

    16:28 2015/11/23小发现 windows下cmd时复制dos中的内容,错误信息等:鼠标右键选择标记,然后ctrl c 即可.

  4. 遍历ModelState中存储的错误信息

    在服务器端验证中,有时我们添加了一个ModelError,然后还需要将该信息以JS的形式返回到客户端.如: [HttpPost] public ActionResult Index(LogOnMode ...

  5. ANT 发布项目中 build.xml 文件的详细配置

    xml 代码 <?xml version="1.0" encoding="UTF-8"?> <!-- name:对应工程名字 default: ...

  6. 在项目中创建单元测试时junit的配置和使用

    首先配置项目中AndroidMainfest.xml文件,加入 <instrumentation android:name="android.test.InstrumentationT ...

  7. ASP.Net MVC3/4中Model验证错误信息的本地化

    最近使用ASP.Net MVC4做一个B/S的管理系统,里面有N多的Action和View Model,View Model上又有N多的验证. 一开始写的时候虽然知道要实现多语言,但是没有过多考虑,本 ...

  8. creat-react-app搭建的项目中按需引入antd以及配置Less和如何修改antd的主题色

    在creat-react-app搭建的项目环境中按需引入antd以及配置less,首先需要暴露出来webpack文件.(此操作不可逆). create-react-app myapp 创建同一个rea ...

  9. vue项目中net::ERR_CONNECTION_TIMED_OUT错误

    我出错的原因时network地址与我本机ip地址不一致 Network: http://192.168.13.30:8080/ 处理方法: 在vue项目中新建一个vue.config.js文件 配置上 ...

随机推荐

  1. POJ3155 Hard Life

    Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 8482   Accepted: 2461 Case Time Limit:  ...

  2. myBatis性能优化【转】

    官方doc文档 http://www.mybatis.org/mybatis-3/configuration.html#settings 最近测试发现个myBatis 有个比较严重的性能问题, 描述如 ...

  3. IOS获取物理尺寸中7Plus中获取的是7的物理尺寸

    IOS获取物理尺寸中7Plus中获取的是7的物理尺寸: 在开发调试过程中我的7Plus手机获取[uiscreen mainscreen].bounds为750  .1334. 解决方案:在手机中的显示 ...

  4. js 数组详解(javascript array)

    Array Array 对象用于在单个的变量中存储多个值. 构造函数: 1)   new Array(); 2)   new Array(size); 3)   new Array(element0, ...

  5. html传參中?和&amp;

    <a href="MealServlet?type=findbyid&mid=<%=m1.getMealId()%> 在这句传參中?之后的代表要传递的參数当中有两个 ...

  6. Android架构分析之Android消息处理机制(二)

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android版本号:4.4.2 在上一篇文章中我们看了一个使用Handler处理Message消息的样例,本文我们 ...

  7. ThinkPHP3.1快速入门(2)数据CURD

    上一篇中,我们了解了ThinkPHP的基础部分,以及如何创建一个控制器和模板,并知道了M方法的用法,本篇将会讲解下数据的CURD操作,探索下更多的数据操作. CURD CURD是一个数据库技术中的缩写 ...

  8. MySQL主主复制+LVS+Keepalived实现MySQL高可用性1

    http://bestvivi.com/2015/09/09/MySQL%E4%B8%BB%E4%B8%BB%E5%A4%8D%E5%88%B6+LVS+Keepalived%E5%AE%9E%E7% ...

  9. Java synchronized 总结

    在Java开发的时候经常会用到关键字synchronized来对代码进行同步,在使用的过程中,对于synchronized确不是很熟悉,最近在看Spring源码时,发现有不少地方都用到同步,因此,趁此 ...

  10. Windows 7 SP1 x64 旗舰版 微软官方安装U盘的制作

    [ 本主题由 中山艹泥喵 于 2013-08-20 23:14:33 设为精华1,原因:不错~ ] 最后由 风中枯萎 于 2015-12-15 17:44:15 修改 安装Windows 7操作系统主 ...