Spring原始碼中的一段設計

2021-07-25 06:03:20 字數 1167 閱讀 2299

在spring的原始碼裡看到下面一段**,使用方法感覺挺巧妙的,記錄下來一下。

reflectiveaspectjadviso***ctory:

private listgetadvisormethods(class<?> aspectclass) 

}});

collections.sort(methods, method_comparator);

return methods;

}

下面看一下reflectionutils.dowithmethods裡面呼叫的dowithmethods方法的實現:

public static void dowithmethods(class<?> clazz, methodcallback mc, methodfilter mf) 

try

catch (illegalacces***ception ex)

} if (clazz.getsuperclass() != null)

else if (clazz.isinte***ce())

} }

這樣的模式使用在什麼地方呢?感覺這個模式和模板模式有點像,把共同的處理集中起來,把特殊處理寫成介面,讓使用它的地方自己實現。實現介面的地方可以做自己想做的事,例如:

「執行我們想要做的處理」這個使用方法,曾經做過一次。使用mybatis和mysql的系統上,要做批量插入。dao方法是用註解(@insert)寫的,例如:

@(insert "insert into *** ......")

void insertuserinfo(user user);

很多地方要這麼寫,於是寫了乙個抽象類,抽象類的作用:

實現如下:

public abstract class abstractbatchcommit

}// 小於n條,或者n條以上的零頭時,執行一次

if(!commitlist.isempty())

} catch(exception e)}/*

* 呼叫sql語句

*/public abstract void callsql(listlist) throws sqlexception;

}

一段sniffer原始碼

include include include typedef struct tcpheader tcpheader,ptcpheader void main char buff 1024 int nret dword dwvalue 1 socket sraw socket af inet,soc...

一段用ASP檢視ASP檔案原始碼的程式

sub printline byval strline strline server.htmlencode strline strline replace strline,strline replace strline,strline replace strline,1,1,1 strline re...

spring中aop原始碼理解 一

spring中aop涉及的類我們衝 annotationawareaspectjautoproxycreator我們看下它的繼承結構 由於它實現了beanpostprocessor介面所有在獲取bean時可以進行一定的處理,aop正是利用這個特性來做的,public object postproce...