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

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. ZooKeeperEclipse 小工具

    插件地址:ZooKeeperEclipse  http://www.massedynamic.org/eclipse/updates/ 安装ZooKeeperEclipse插件过程例如以下: Step ...

  2. 《Javascript高级程序设计》读书笔记之继承

    1.原型链继承 让构造函数的原型对象等于另一个类型的实例,利用原型让一个引用类型继承另一个引用类型的属性和方法 function SuperType() { this.property=true; } ...

  3. mysql替换字段里数据内容部分字符串(亦可用于增加字段中的内容)

    mysql替换表的字段里面内容,如例子: mysql> select host,user from user  where user='testuser'; +----------------- ...

  4. windows phone (18) Border元素

    原文:windows phone (18) Border元素 Border类是对某一个对象的周围边框,背景,或者同时绘制两者,首先看一个简单的例子进行分析[作者:神舟龍] xaml文件: <!- ...

  5. python学习之print输出不换行

    print的即时打印会导致换行,要使得print的输出不换行,可以在字符串或者变量后面加个逗号(“,”),如下: s = "A bird in the hand..." for c ...

  6. 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(一)

    ArcGIS 10.2 for Desktop是在10.1的成功基础上进行的改进,它的改进包括:性能提升.附加的安全性.40多个新的分析工具.3D功能提高.栅格增强.新的地理数据管理能力以及其它更多的 ...

  7. Java的Log系统介绍和切换(转)

    Java的log系统比较繁杂.在这里梳理一下.本文只涉及log系统介绍和处理log系统之间的切换.不涉及如何配置和使用. 具体的log系统 Log4j:准确的说是log4j 1.x版.是之前使用最广泛 ...

  8. iOS执行时与method swizzling

    C语言是静态语言,它的工作方式是通过函数调用,这样在编译时我们就已经确定程序怎样执行的.而Objective-C是动态语言,它并不是通过调用类的方法来执行功能,而是给对象发送消息,对象在接收到消息之后 ...

  9. 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告

    时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: ...

  10. 记View跨界平局

    <?xml version="1.0" encoding="utf-8"? > <RelativeLayout xmlns:android=& ...