術語理解:
cross-cutting concerns:散落在業務中,但又與業務無關的程式碼片段。
|
|識別為Aspect
|
v
Aspect:通過整理cross-cutting concerns,需要從中識別出可重用的、功能單一的切面,例如日誌/許可權檢查切面等。
|
|============分割線:上面是業務中的概念,下面是代碼中的概念==========
|
|實現為Advisor(主要是PointcutAdvisor = Pointcut +Advise)
|
v
Pointcut:觸發時機,即做的前提條件,通常需要target/client符合某些情況時才觸發Advise。例如target的類名/方法名符合指定Regex時觸發Advise。Pointcut支援取交集/並集。+Advise:觸發內容,即做什麼。例如日誌動作/許可權檢查動作的具體實現。|||_ _ _ Joinpoint: Advise的屬性,表明做什麼的時刻,spring只能在以下情況中任選:target的方法前/後/環繞/拋出異常時。Spring AOP的核心思想是代理:client ----透明調用目標----> proxy ----織入目標----> target | v 改變行為 / 增加行為 | | v v PointcutAdvisor IntroductionAdvisor配置方式:ProxyBean|||_ _ _ proxyInterfaces:targetInterface|||_ _ _ target:targetBean|||_ _ _ interceptorNames:可以填入Advise/ Advisor/ Interceptor直接填Advise即省略了Pointcut,表示proxyInterfaces中聲明的所有方法都要觸發Advise。Interceptor和Advisor類似,只不過前者是AOP Alliance制定的標準,後者是spring自己的標準。PointcutAdvisor(以DefaultPointcutAdvisor為例)|||_ _ _ pointcut:pointcutBean|||_ _ _ advise:adviseBean動態改變了targetInterface中描述的行為,在pointcutBean中指定了觸發時機,在adviseBean中指定了觸發內容。IntroductionAdvisor(以DefaultIntroductionAdvisor為例)|||_ _ _ constructor-arg0:IntroductionBean|||_ _ _ constructor-arg1:IntroductionInterface為target動態增加了IntroductionInterface中描述的行為,其具體實現在IntroductionBean中。