当前位置:首页 >> 跨学科知识体系 >> 【Spring】Spring中的设计模式,索爱w508c

【Spring】Spring中的设计模式,索爱w508c

cpugpu芯片开发光刻机 跨学科知识体系 1
文件名:【Spring】Spring中的设计模式,索爱w508c 【Spring】Spring中的设计模式

文章目录 责任链模式工厂模式适配器模式代理模式模版方法观察者模式构造器模式

责任链模式 Spring中的Aop的通知调用会使用责任链模式

责任链模式介绍

角色:抽象处理者(Handler)具体处理者(ConcreteHandler1)客户类角色(Client)

Spring源码介绍

spring中Aop的责任链模式,相对于传统的责任链模式做了一定的改造。 传统的设计模式,抽象处理者会有一个方法设置和获取具体处理者的下一个处理者的方法。 如:

public abstract class Handler {private Handler next;public Handler getNext() {return next;}public void setNext(Handler next) {this.next = next;}//处理请求的方法public abstract void handleRequest(String request);}

但是Spring中的责任链模式没有这两个方法,而是抽出一个公共的处理方法,方法内有数组和下标来完成链式。

public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Cloneable {// 环绕通知类protected final List<?> interceptorsAndDynamicMethodMatchers;// 下标private int currentInterceptorIndex = -1;/*** 递归获取通知,然后执行* @return* @throws Throwable*/@Override@Nullablepublic Object proceed() throws Throwable {// We start with an index of -1 and increment early.// 从索引为-1的拦截器开始调用,并按序递增,如果拦截器链中的拦截器迭代调用完毕,开始调用target的函数,这个函数是通过反射机制完成的// 具体实现在AopUtils.invokeJoinpointUsingReflection方法中if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {return invokeJoinpoint();}// 获取下一个要执行的拦截器,沿着定义好的interceptorOrInterceptionAdvice链进行处理Object interceptorOrInterceptionAdvice =this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {// Evaluate dynamic method matcher here: static part will already have// been evaluated and found to match.// 这里对拦截器进行动态匹配的判断,这里是对pointcut触发进行匹配的地方,如果和定义的pointcut匹配,那么这个advice将会得到执行InterceptorAndDynamicMethodMatcher dm =(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;Class<?> targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass());if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {return dm.interceptor.invoke(this);}else {// Dynamic matching failed.// Skip this interceptor and invoke the next in the chain.// 如果不匹配,那么proceed会被递归调用,知道所有的拦截器都被运行过位置return proceed();}}else {// It's an interceptor, so we just invoke it: The pointcut will have// been evaluated statically before this object was constructed.// 普通拦截器,直接调用拦截器,将this作为参数传递以保证当前实例中调用链的执行return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);}}}

其中的最后一句

// 普通拦截器,直接调用拦截器,将this作为参数传递以保证当前实例中调用链的执行return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);

MethodInterceptor就是抽象处理者

@FunctionalInterfacepublic interface MethodInterceptor extends Interceptor {/*** */Object invoke(MethodInvocation invocation) throws Throwable;}

具体的执行者有 AspectJAfterAdvice、AspectJAfterReturningAdvice、AspectJAfterThrowingAdvice、AspectJMethodBeforeAdvice、AspectJAroundAdvice

工厂模式 Spring中的获取Bean就是工厂模式,如:BeanFactory获取

工厂模式介绍

角色:抽象产品具体产品抽象工厂具体工厂

Spring源码介绍

抽象工厂

public interface BeanFactory {Object getBean(String name) throws BeansException;...}

具体工厂

适配器模式 Spring中的根据通知的时候,将Advisor适配为MethodInterceptor

适配器介绍

角色目标接口:抽象适配器:具体适配器:抽象源接口:具体源接口:适配器就是将源接口适配为目标接口

Spring中的源码介绍

抽象适配器:

public interface AdvisorAdapter {/*** 适配方法,将Advisor适配为MethodInterceptor Advisor就是源接口:MethodInterceptor就是目标接口*/MethodInterceptor getInterceptor(Advisor advisor);}

具体适配器:

class AfterReturningAdviceAdapter implements AdvisorAdapter, Serializable {@Overridepublic boolean supportsAdvice(Advice advice) {return (advice instanceof AfterReturningAdvice);}@Overridepublic MethodInterceptor getInterceptor(Advisor advisor) {AfterReturningAdvice advice = (AfterReturningAdvice) advisor.getAdvice();return new AfterReturningAdviceInterceptor(advice);}}

具体源接口:

代理模式 cglib、gdk代理 模版方法 Spring中的refresh方法中的postProcessFactory、onRefresh等 观察者模式 Spring中的事件监听

角色: 抽象目标, 具体目标, 具体观察者, 抽象观察者

抽象目标里面会有一个数组,存放具体的观察者,并且会有一个添加删除观察者的方法,还有一个通知所有观察者的方法。具体目标需要通知观察者的时候,遍历数组通知观察者

Spring中的事件监听做了一定的变动 有四个角色 广播器:其实就是我们的抽象目标,包含了添加删除,广播事件方法 监听器:监听广播器广播的事件 事件: 事件源:触发事件的人,将事件添加到广播器中

构造器模式 Spring中解析xml或者注解为BeanDefinition信息的时候会使用BeanDefinitionHandler类

该类里面包含了一个 BeanDefinition 字段,可以调佣BeanDefinitionHandler中的方法给该字段设值,最后可以调用方法获取BeanDefinition

协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接