设计模式课程 设计模式精讲 12-3 适配器模式源码解析

1    源码解析

1.1  源码解析1(在jdk中的应用)

1.2  源码解析2(Spring中的通知管理)

1.3  源码解析3(SpringMVC中的应用)

1    源码解析

1.1  源码解析1(在jdk中的应用)

xmlAdapter(此类是用于适配xml的一个类,是处理xml序列化和反序列化的一个类)

public abstract class XmlAdapter<ValueType,BoundType> {

    /**
     * Do-nothing constructor for the derived classes.
     */
    protected XmlAdapter() {}

    /**
     *  处理反序列化*/
    public abstract BoundType unmarshal(ValueType v) throws Exception;

    /**
      * 处理序列化*/
    public abstract ValueType marshal(BoundType v) throws Exception;
}

对于xml序列化的时候,我们时间可以写一个date类,可以继承xmlAdapter抽象类,实现它的序列化和反序列化方法。

1.2  源码解析2(Spring中的通知管理)

功能描述:

通知适配器将通知类适配成各种类型的通知,如:前置通知,后置通知等等(关于通知的课程可以详细查看我的spring笔记)

通知适配器接口:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.aop.framework.adapter;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.Advisor;

public interface AdvisorAdapter {
    boolean supportsAdvice(Advice var1);

    MethodInterceptor getInterceptor(Advisor var1);
}

前置通知适配器实现类:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.aop.framework.adapter;

import java.io.Serializable;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.Advisor;
import org.springframework.aop.MethodBeforeAdvice;

class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {
    MethodBeforeAdviceAdapter() {
    }

    public boolean supportsAdvice(Advice advice) {
        return advice instanceof MethodBeforeAdvice;
    }

    public MethodInterceptor getInterceptor(Advisor advisor) {
        MethodBeforeAdvice advice = (MethodBeforeAdvice)advisor.getAdvice();
        return new MethodBeforeAdviceInterceptor(advice);
    }
}

advisor组合得到通知类:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.aop;

import org.aopalliance.aop.Advice;

public interface Advisor {
    Advice getAdvice();

    boolean isPerInstance();
}

MethodBeforeAdvice继承超类(隔了好几层继承实现):

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.aopalliance.intercept;

import org.aopalliance.aop.Advice;

public interface Interceptor extends Advice {
}

1.3  源码解析3(SpringMVC中的应用)

重点关注:

springmvc中,通过处理映射器(handlerMapper)找到对应的handler(controler,servlet或者httpRequestHandler)

现在是多个适配器,对应多个

适配器接口:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.web.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface HandlerAdapter {
    boolean supports(Object var1);

    ModelAndView handle(HttpServletRequest var1, HttpServletResponse var2, Object var3) throws Exception;

    long getLastModified(HttpServletRequest var1, Object var2);
}

适配器类:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.web.servlet.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;

public class SimpleControllerHandlerAdapter implements HandlerAdapter {
    public SimpleControllerHandlerAdapter() {
    }
  /**  如果对应的handler有确切的Controller并支持的话,返回true,否则,返回false   */
    public boolean supports(Object handler) {
        return handler instanceof Controller;
    }

    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return ((Controller)handler).handleRequest(request, response);
    }

    public long getLastModified(HttpServletRequest request, Object handler) {
        return handler instanceof LastModified ? ((LastModified)handler).getLastModified(request) : -1L;
    }
}

DispatcherServlet(通过handlermapper找到对应的handler,适配器中的client)

 protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpServletRequest processedRequest = request;
        HandlerExecutionChain mappedHandler = null;
        boolean multipartRequestParsed = false;
        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

        try {
            try {
                ModelAndView mv = null;
                Exception dispatchException = null;

                try {
                    processedRequest = this.checkMultipart(request);
                    multipartRequestParsed = processedRequest != request;
                    mappedHandler = this.getHandler(processedRequest);
                    if (mappedHandler == null || mappedHandler.getHandler() == null) {
                        this.noHandlerFound(processedRequest, response);
                        return;
                    }

                    HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
                    String method = request.getMethod();
                    boolean isGet = "GET".equals(method);
                    if (isGet || "HEAD".equals(method)) {
                        long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
                        if (this.logger.isDebugEnabled()) {
                            this.logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
                        }

                        if ((new ServletWebRequest(request, response)).checkNotModified(lastModified) && isGet) {
                            return;
                        }
                    }

                    if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                        return;
                    }

                    try {            //通过适配器handle方法,返回对应的model&View对象
                        mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
                    } finally {
                        if (asyncManager.isConcurrentHandlingStarted()) {
                            return;
                        }

                    }

                    this.applyDefaultViewName(request, mv);
                    mappedHandler.applyPostHandle(processedRequest, response, mv);
                } catch (Exception var27) {
                    dispatchException = var27;
                }

                this.processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
            } catch (Exception var28) {
                this.triggerAfterCompletion(processedRequest, response, mappedHandler, var28);
            } catch (Error var29) {
                this.triggerAfterCompletionWithError(processedRequest, response, mappedHandler, var29);
            }

        } finally {
            if (asyncManager.isConcurrentHandlingStarted()) {
                mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
                return;
            } else {
                if (multipartRequestParsed) {
                    this.cleanupMultipart(processedRequest);
                }

            }
        }
    }

controller接口:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.web.servlet.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

public interface Controller {
    ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws Exception;
}

原文地址:https://www.cnblogs.com/1446358788-qq/p/11518244.html

时间: 2024-11-02 03:03:44

设计模式课程 设计模式精讲 12-3 适配器模式源码解析的相关文章

设计模式课程 设计模式精讲 4-3 简单工厂源码解析

1 源码解析 1.1 Calendar源码解析 1.2 DriverManager源码解析 1 源码解析 1.1 Calendar源码解析 /** * Gets a calendar using the specified time zone and default locale. * The <code>Calendar</code> returned is based on the current time * in the given time zone with the d

设计模式课程 设计模式精讲 6-3 抽象工厂源码解析

1 源码解析 1.1 mysql源码解析 1.2 mybaties 的sqlsession源码解析 1 源码解析 1.1 mysql源码解析 1.2 mybaties 的sqlsession源码解析 原文地址:https://www.cnblogs.com/1446358788-qq/p/11295158.html

设计模式课程 设计模式精讲 10-2 外观模式源码解析

1 源码解析 1.1 源码解析1(jdk中的JDBCUtils工具类) 1.2 源码解析2 1.3 源码解析3 1.4 源码解析4 1 源码解析 1.1 源码解析1(jdk中的JDBCUtils工具类) jdbc在springJDBC中的封装 /** * Close the given JDBC Connection and ignore any thrown exception. * This is useful for typical finally blocks in manual JDB

设计模式课程 设计模式精讲 15-3 桥接模式源码解析

1 桥接模式源码解析 1.1 源码解析1 jdk中的应用(驱动类) 1 桥接模式源码解析 1.1 源码解析1 jdk中的应用(驱动类) 步骤: class.forName 调取驱动接口的静态块,触发驱动管理类DriverManager 的注册驱动方法,从而将该驱动放到CopyOnWriteArrayList中. getConnect方法是通过传入url用户名密码. 针对不同的数据库,通过driverManager中的不同方法,获取的都是相同的接口,jdbc在最初的时候设计了一套接口,再由各个数据

设计模式课程 设计模式精讲 17-3 模板方法模式源码解析

1 源码解析 1.1 源码解析1(在jdk中的使用) 1.2 源码解析2(在servlet中的应用) 1.3 源码解析3(在mybaties中的应用) 1 源码解析 1.1 源码解析1(在jdk中的使用) AbstractList(父类) public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { //get方法为抽象方法,完全交给子类去实现 abstr

设计模式课程 设计模式精讲 16-5 代理模式源码解析

1 源码解析 1.1 源码解析1(jdk中的应用) 1.2 源码解析2(spring中的应用) 1.3 源码解析3(mybaties中的应用) 1 源码解析 1.1 源码解析1(jdk中的应用) java.lang.reflect.Proxy public class Proxy implements java.io.Serializable { protected Proxy(InvocationHandler h) { doNewInstanceCheck(); this.h = h; }

设计模式课程 设计模式精讲 18-3 迭代器模式源码解析

1 源码解析 1.1 源码解析1(jdk中的应用) 1.2 源码解析2(mybaties中的应用)   1 源码解析 1.1 源码解析1(jdk中的应用) java.util.Iterator(接口) public interface Iterator<E> { /** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #nex

设计模式课程 设计模式精讲 19-3 策略模式源码解析

1 源码解析 1.1 源码解析1(jdk中的应用1) 1.2 源码解析2(jdk中的应用2) 1.3 源码解析3(Spring中的应用1) 1.4 源码解析4(Spring中的应用2) 1 源码解析 1.1 源码解析1(jdk中的应用1) java.util.Comparator(策略类)作为比较器的应用 package java.util; public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Obj

设计模式-简单工厂Coding+jdk源码解析

前面的软件设计七大原则,目前只有理论这块,因为最近参与项目重构,暂时没有时间把Coding的代码按照设计思路一点点写出来.本周周末会花时间整理出来,然后现在想的是白天更新一点并发编程,晚上回家学习设计模式.非科班出身,脑子也比较笨.博文都是自己根据学习的时候所想的思路,如果能有帮到各位的地方,那十分荣幸.如果有欠缺之处,希望能在评论中指出一起进步.好啦,开始正文了. 本套设计模式的博文,包含各种设计模式的定义.类型.适用场景及优缺点分析.并通过Coding去实际加深理论理解. 简单工厂: 该模式