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

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. [WPF]不规则窗体的实现

    Microsoft Expression Design 4 导入做好的login.Png图片 调整美工板大小 导出,右边格式为XAML WPF 资源字典,实时效果为XAML效果 文件名login.xa ...

  2. oracle转Mysql中,varchar2(10)和number应该转换为什么类型? (转)

    一. varchar2(10)和number应该转换为什么类型? oracle转成mysql时:varchar2(10)可以转成varchar(10)number则要看oracle中存储的具体是什么类 ...

  3. 《深入了解Android:Wi-Fi、NFC和GPS音量》勘误表

    资源下载更新(我不知道为什么被115网盘屏蔽) 深入了解android它wifi-nfc-gps 115网盘礼包码:5lbee5qn4g5ghttp://115.com/lb/5lbee5qn4g5g ...

  4. 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础

    原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础 [源码下载] 重新想象 Windows 8 Store Apps (9) - 控件之 Sc ...

  5. 如何设置多个同一页的tinymce编辑

    的页面设置多个tinymce编辑器 This example shows how to setup multiple editors on the same page and with differe ...

  6. 【SICP读书笔记(一)】正则序展开的特殊情况

    scheme解释器有两种实现方式,一种是应用序,先对每个参数求值,再以首过程对所有求得的参数求值. 第二种是正则序,会“完全展开然后归约”(书中原文) SICP中的练习1.5,让我困惑了一下.原题如下 ...

  7. jsp简单练习-简单的下拉表单

    <%@ page contentType="text/html; charset=gb2312" %> <html> <body> <fo ...

  8. c#操作appsettiongs

    try { //指定要修改的配置文件的路径 Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWe ...

  9. Android运用自己的标题栏

    Android程序的标题栏TitleBar区域很单调,如果想个性化一些可以通过下面的方法来为自己软件的标题定制一个layout布局文件,比如浏览器的标题栏,它包含了网站的Favicon,自定义的进度条 ...

  10. 移动web:Tips消息弹出框

    在web开发中经常会用到像alert这样的弹出消息,每个浏览器自带的消息弹出框都不相同.为了统一外观,实现自定义的功能,动手写一个弹出框插件. 对弹出框的实现要求如下: 1. 仿照IOS系统弹出外观 ...