码迷,mamicode.com
首页 > 其他好文 > 详细

slf4j - 门面模式 - 架构分析

时间:2018-10-07 12:06:40      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:tee   static   gsl   门面模式   new   lang   contains   opera   mes   

所谓门面模式:提供一个统一接口给用户去访问多个子系统的多个不同接口;

优点:

1.子系统之间解耦

2.简化用户对子系统的使用

3.子系统易扩展增加

缺点:

子系统实现与客户程序存在很大依赖性,需预制客户程序的功能

下以slf4j 为例分析其门面模式实现原理

1. 门面架构分析

    技术分享图片

2. 源码实现分析

  1>  用户通过 Logger logger = LoggerFactory.getLogger(TestMain.class) 获取Logger实例

  2> slf4j 通过门面类LoggerFactory查找子系统的实现,可以debug 跟踪 slf4j 源码

    public static Logger getLogger(String name) {
        ILoggerFactory iLoggerFactory = getILoggerFactory();
        return iLoggerFactory.getLogger(name);
    }

  3> 通过实例类路径"org/slf4j/impl/StaticLoggerBinder.class" 查找加载实现类StaticLoggerBinder(发现多个子系统实现则由JVM选择加载实现类), 通过单例获取子系统实现类LoggerFactory ,未找到实现类则加载NOP类,修改初始化状态。

   private final static void bind() {
        try {
            Set<URL> staticLoggerBinderPathSet = null;
            // skip check under android, see also
            // http://jira.qos.ch/browse/SLF4J-328
            if (!isAndroid()) {
                staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();
                reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);
            }
            // the next line does the binding
            StaticLoggerBinder.getSingleton();
            INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
            reportActualBinding(staticLoggerBinderPathSet);
            fixSubstituteLoggers();
            replayEvents();
            // release all resources in SUBST_FACTORY
            SUBST_FACTORY.clear();
        } 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.contains("org.slf4j.impl.StaticLoggerBinder.getSingleton()")) {
                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);
        }
    }

  4> 至此完成绑定子系统日志实现框架

后续会更新 slf4j + logback 实现方式及部分源码分析

 

slf4j - 门面模式 - 架构分析

标签:tee   static   gsl   门面模式   new   lang   contains   opera   mes   

原文地址:https://www.cnblogs.com/neverm-one/p/9728192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!