責(zé)任鏈模式
Spring中的Aop的通知調(diào)用會(huì)使用責(zé)任鏈模式
責(zé)任鏈模式介紹
角色:
抽象處理者(Handler)
具體處理者(ConcreteHandler1)
客戶類角色(Client)
Spring源碼介紹
spring中Aop的責(zé)任鏈模式,相對(duì)于傳統(tǒng)的責(zé)任鏈模式做了一定的改造。
傳統(tǒng)的設(shè)計(jì)模式,抽象處理者會(huì)有一個(gè)方法設(shè)置和獲取具體處理者的下一個(gè)處理者的方法。
如:
public abstract class Handler {
private Handler next;
public Handler getNext() {
return next;
}
public void setNext(Handler next) {
this.next = next;
}
//處理請(qǐng)求的方法
public abstract void handleRequest(String request);
}
但是Spring中的責(zé)任鏈模式?jīng)]有這兩個(gè)方法,而是抽出一個(gè)公共的處理方法,方法內(nèi)有數(shù)組和下標(biāo)來完成鏈?zhǔn)健?/p>
public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Cloneable {
// 環(huán)繞通知類
protected final List<?> interceptorsAndDynamicMethodMatchers;
// 下標(biāo)
private int currentInterceptorIndex = -1;
/**
* 遞歸獲取通知,然后執(zhí)行
* @return
* @throws Throwable
*/
@Override
@Nullable
public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
// 從索引為-1的攔截器開始調(diào)用,并按序遞增,如果攔截器鏈中的攔截器迭代調(diào)用完畢,開始調(diào)用target的函數(shù),這個(gè)函數(shù)是通過反射機(jī)制完成的
// 具體實(shí)現(xiàn)在AopUtils.invokeJoinpointUsingReflection方法中
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
// 獲取下一個(gè)要執(zhí)行的攔截器,沿著定義好的interceptorOrInterceptionAdvice鏈進(jìn)行處理
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.
// 這里對(duì)攔截器進(jìn)行動(dòng)態(tài)匹配的判斷,這里是對(duì)pointcut觸發(fā)進(jìn)行匹配的地方,如果和定義的pointcut匹配,那么這個(gè)advice將會(huì)得到執(zhí)行
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會(huì)被遞歸調(diào)用,知道所有的攔截器都被運(yùn)行過位置
return proceed();
}
}
else {
// It's an interceptor, so we just invoke it: The pointcut will have
// been evaluated statically before this object was constructed.
// 普通攔截器,直接調(diào)用攔截器,將this作為參數(shù)傳遞以保證當(dāng)前實(shí)例中調(diào)用鏈的執(zhí)行
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}
}
其中的最后一句
// 普通攔截器,直接調(diào)用攔截器,將this作為參數(shù)傳遞以保證當(dāng)前實(shí)例中調(diào)用鏈的執(zhí)行
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
MethodInterceptor就是抽象處理者
@FunctionalInterface
public interface MethodInterceptor extends Interceptor {
/**
*
*/
Object invoke(MethodInvocation invocation) throws Throwable;
}
具體的執(zhí)行者有
AspectJAfterAdvice、AspectJAfterReturningAdvice、AspectJAfterThrowingAdvice、AspectJMethodBeforeAdvice、AspectJAroundAdvice
工廠模式
Spring中的獲取Bean就是工廠模式,如:BeanFactory獲取
工廠模式介紹
角色:
抽象產(chǎn)品
具體產(chǎn)品
抽象工廠
具體工廠
Spring源碼介紹
抽象工廠
public interface BeanFactory {
Object getBean(String name) throws BeansException;
...
}
具體工廠
適配器模式
Spring中的根據(jù)通知的時(shí)候,將Advisor適配為MethodInterceptor
適配器介紹
角色
目標(biāo)接口:
抽象適配器:
具體適配器:
抽象源接口:
具體源接口:
適配器就是將源接口適配為目標(biāo)接口
Spring中的源碼介紹
抽象適配器:
public interface AdvisorAdapter {
/**
* 適配方法,將Advisor適配為MethodInterceptor Advisor就是源接口:MethodInterceptor就是目標(biāo)接口
*/
MethodInterceptor getInterceptor(Advisor advisor);
}
具體適配器:
class AfterReturningAdviceAdapter implements AdvisorAdapter, Serializable {
@Override
public boolean supportsAdvice(Advice advice) {
return (advice instanceof AfterReturningAdvice);
}
@Override
public MethodInterceptor getInterceptor(Advisor advisor) {
AfterReturningAdvice advice = (AfterReturningAdvice) advisor.getAdvice();
return new AfterReturningAdviceInterceptor(advice);
}
}
具體源接口:
代理模式
cglib、gdk代理
模版方法
Spring中的refresh方法中的postProcessFactory、onRefresh等
觀察者模式
Spring中的事件監(jiān)聽
角色:
抽象目標(biāo),
具體目標(biāo),
具體觀察者,
抽象觀察者
抽象目標(biāo)里面會(huì)有一個(gè)數(shù)組,存放具體的觀察者,并且會(huì)有一個(gè)添加刪除觀察者的方法,還有一個(gè)通知所有觀察者的方法。
具體目標(biāo)需要通知觀察者的時(shí)候,遍歷數(shù)組通知觀察者
Spring中的事件監(jiān)聽做了一定的變動(dòng)
有四個(gè)角色
廣播器:其實(shí)就是我們的抽象目標(biāo),包含了添加刪除,廣播事件方法
監(jiān)聽器:監(jiān)聽廣播器廣播的事件
事件:
事件源:觸發(fā)事件的人,將事件添加到廣播器中文章來源:http://www.zghlxwxcb.cn/news/detail-625202.html
構(gòu)造器模式
Spring中解析xml或者注解為BeanDefinition信息的時(shí)候會(huì)使用BeanDefinitionHandler類
該類里面包含了一個(gè) BeanDefinition 字段,可以調(diào)傭BeanDefinitionHandler中的方法給該字段設(shè)值,最后可以調(diào)用方法獲取BeanDefinition文章來源地址http://www.zghlxwxcb.cn/news/detail-625202.html
到了這里,關(guān)于【Spring】Spring中的設(shè)計(jì)模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!