依照绑定实现类的方式是基于约定原则:推断分下面几个步骤

1.LoggerFactory扫描实现类路径有几个实现类,即在org/slf4j/impl/下有几个StaticLoggerBinder.class
2.假设有多个实现类,向开发者报告多个实现类的路径
3.假设有多个实现类,向开发者报告真正绑定的是哪一个实现类
4.假设没有实现类,怎么办?

详细代码实现

//要扫描的文件路径 
  private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class";

//扫描全部的StaticLoggerBinder.class路径放入到set集合

  private static Set findPossibleStaticLoggerBinderPathSet() {

    // use Set instead of list in order to deal with  bug #138

    // LinkedHashSet appropriate here because it preserves insertion order during iteration

    Set staticLoggerBinderPathSet = new LinkedHashSet();

    try {

      ClassLoader loggerFactoryClassLoader = LoggerFactory.class

              .getClassLoader();

      Enumeration paths;

      if (loggerFactoryClassLoader == null) {

        paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH);

      } else {

        paths = loggerFactoryClassLoader

                .getResources(STATIC_LOGGER_BINDER_PATH);

      }

      while (paths.hasMoreElements()) {

        URL path = (URL) paths.nextElement();

        staticLoggerBinderPathSet.add(path);

      }

    } catch (IOException ioe) {

      Util.report("Error getting resources from path", ioe);

    }

    return staticLoggerBinderPathSet;

  }

//报告全部的实现类路径
  private static void reportMultipleBindingAmbiguity(Set staticLoggerBinderPathSet) {

    if (isAmbiguousStaticLoggerBinderPathSet(staticLoggerBinderPathSet)) {

      Util.report("Class path contains multiple SLF4J bindings.");

      Iterator iterator = staticLoggerBinderPathSet.iterator();

      while (iterator.hasNext()) {

        URL path = (URL) iterator.next();

        Util.report("Found binding in [" + path + "]");

      }

      Util.report("See " + MULTIPLE_BINDINGS_URL + " for an explanation.");

    }

  }

//报告真正用到的实现类名称
private static void reportActualBinding(Set staticLoggerBinderPathSet) {

    if (isAmbiguousStaticLoggerBinderPathSet(staticLoggerBinderPathSet)) {

      Util.report("Actual binding is of type ["+StaticLoggerBinder.getSingleton().getLoggerFactoryClassStr()+"]");

    }

  }



//去拿到实现类的实例,由于StaticLoggerBinder类不一定存在,所以要捕获NoClassDefFoundError异常

private final static void bind() {

    try {

      Set staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();

      reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);

      // the next line does the binding

      StaticLoggerBinder.getSingleton();

      INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;

      reportActualBinding(staticLoggerBinderPathSet);

      emitSubstituteLoggerWarning();

    } catch (NoClassDefFoundError ncde) {

      String msg = ncde.getMessage();

      if (messageContainsOrgSlf4jImplStaticLoggerBinder(msg)) {

        INITIALIZATION_STATE = NOP_FALLBACK_INITIALIZATION;

        Util.report("Failed to load class \"org.slf4j.impl.StaticLoggerBinder\".");

        Util.report("Defaulting to no-operation (NOP) logger implementation");

        Util.report("See " + NO_STATICLOGGERBINDER_URL

                + " for further details.");

      } else {

        failedBinding(ncde);

        throw ncde;

      }

    } catch (java.lang.NoSuchMethodError nsme) {

      String msg = nsme.getMessage();

      if (msg != null && msg.indexOf("org.slf4j.impl.StaticLoggerBinder.getSingleton()") != -1) {

        INITIALIZATION_STATE = FAILED_INITIALIZATION;

        Util.report("slf4j-api 1.6.x (or later) is incompatible with this binding.");

        Util.report("Your binding is version 1.5.5 or earlier.");

        Util.report("Upgrade your binding to version 1.6.x.");

      }

      throw nsme;

    } catch (Exception e) {

      failedBinding(e);

      throw new IllegalStateException("Unexpected initialization failure", e);

    }

  }

到此,就真正拿到实现类的实例了

slf自己主动绑定实现类过程推断的更多相关文章

  1. Cocos2d-x JSB 自己主动绑定bindings

    Javascript Binding (简称JSB) 自己主动绑定教程. Cocos2d-x JSB 自己主动绑定bindings-generator (以下简称B-G) 使用心得 假设想弄清深入原理 ...

  2. cocos2dx 自己主动绑定js

    依照教程把全部资源下载好后....... 找到cocos2dx project下的tools/bindings-generator/test 发现里面有test.sh , test.ini , 去掉s ...

  3. Spring绑定请求参数过程以及使用@InitBinder来注册自己的属性处理器

    在工作中,经常会出现前台的请求参数由于无法被正常转型,导致请求无法进到后台的问题. 比如,我有一个User.其性别的属性被定义成了枚举,如下: public enum Gender { MALE(&q ...

  4. cocos2dx lua 绑定之二:手动绑定自定义类中的函数

    cococs2dx 3.13.1 + vs2013 + win10 1.首先按照<cocos2dx lua 绑定之一:自动绑定自定义类>绑定Student类 2.在Student类中增加一 ...

  5. cocos2dx 2.x版本:简化提炼tolua++绑定自定义类到lua中使用

    cocos2dx的3.x版本已经提供了更好地绑定方式,网上有很多相关的教程,这里给一个链接:http://www.cocoachina.com/bbs/read.php?tid=196416. 由于目 ...

  6. WPF——传实体类及绑定实体类属性

    public class User: private string _User; public string User1 { get { return _User; } set { _User = v ...

  7. silverlight datagrid绑定匿名类

    原文 http://www.cnblogs.com/luweis/archive/2011/10/21/2220587.html 刚开始遇到的一个问题是这样的,我有一个datagrid,根据不同的条件 ...

  8. 使用MethodType函数将方法绑定到类或实例上

    在开始正文之前,需要了解下Python的绑定方法(bound method)和非绑定方法. 简单做个测试: 定义一个类,类中由实例方法.静态方法和类方法. class ClassA: def inst ...

  9. cocos2dx lua 绑定之一:自动绑定自定义类中的函数

    cococs2dx 3.13.1 + vs2013 + win10 1.首先定义C++类Student 在cocos2d-x\cocos文件夹下新建一个user_define的文件夹放置两个文件. 注 ...

随机推荐

  1. Deploy 11.2.0.3 RAC+DG on Windows 2008 R2 Step by Step

    环境规划: 节点1: tc1 192.168.56.101 内存:2G 节点2: tc2 192.168.56.102 内存:2G 物理备库:tcdg192.168.56.108内存:1.5G 操作系 ...

  2. net MVC 的八个扩展点

    net MVC 的八个扩展点 MVC模型以低耦合.可重用.可维护性高等众多优点已逐渐代替了WebForm模型.能够灵活使用MVC提供的扩展点可以达到事半功倍的效果,另一方面Asp.net MVC优秀的 ...

  3. java 配置及安装Eclipse

    jdk下载 点我~ Java SE Development Kit 8u20 You must accept the Oracle Binary Code License Agreement for ...

  4. mysql安装前的系统准备工作(转)

    一.系统环境总结:

  5. android面试题 不单单为了面试也是一次非常好的学习

    以以下试题都是在网上找的总结出来的,谢谢大家的分享!希望,我们共同进步,找到自己梦想的公司: 1.android dvm 的进程和Linux的进程,应用程序的进程是否为同一个概念: 答:dvm是dal ...

  6. shell程序之逐行读取一文件里的參数且使用此參数每次运行5分钟

    /*********************************************************************  * Author  : Samson  * Date   ...

  7. Mac下改动Android Studio 所用的JDK版本号

    Mac下改动Android Studio 所用的JDK版本号 @author ASCE1885 近期项目从Eclipse+Ant构建模式转移到了Android Studio+Gradle构建模式.自然 ...

  8. cidaemon.exe过程cpu入住率和关闭cidaemon.exe加工方法

    问题叙述性说明:   这个时间机器始终是一个奇怪的问题:cidaemon.exe这个过程需要CUP率98%以上,大大影响了正常使用电脑.多个资源管理器出现cidaemon.exe过程,cpu率最高的一 ...

  9. C#判断操作系统是32位还是64位(超简单)

    由于项目需要在64位和32位系统运行,需要判断当前系统是32位还是64位. 网上很多方法,但是都感觉不是很简洁,最后发现可以使用int的长度来判断:看代码 /// <summary>    ...

  10. Spring+Mybatis+SpringMVC后台与前台分页展示实例(附工程)(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文实现了一个后台由Spring+Mybatis+SpringMVC组成,分页采用Pag ...