關於 AOP 的概念描述及相關術語能夠參考 完全征服 Spring AOP 之 理論篇 總結的很好; 本文將着重分析下 AOP 的實現過程。java
public interface UserService { void say (); }
接口實現類以下:spring
public class UserServiceImpl implements UserService { public void say() { System.out.println("do say method"); } }
public class UserAdvice implements MethodBeforeAdvice { public void before(Method m, Object[] args, Object target) throws Throwable { System.out.println("do before advice ...."); } }
<beans> <!-- 配置接口實現類 --> <bean id="userService" class="org.springframework.aop.UserServiceImpl" /> <!-- 配置通知類 --> <bean id="userAdvice" class="org.springframework.aop.UserAdvice" /> <!--代理類--> <bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--要代理的接口 建立代理對象時須要--> <!-- 配置該屬性會採用 jdk 動態代理,反之採用 cglib --> <property name="proxyInterfaces"> <value>org.springframework.aop.UserService</value> </property> <!--攔截器名字,也就是咱們定義的通知類,可配置多個通知類 --> <property name="interceptorNames"> <list> <value>userAdvice</value> </list> </property> <!--目標類,就是咱們業務的實現類--> <property name="target"> <ref bean="userService"/> </property> </bean> </beans>
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/org/springframework/aop/aop.xml"); UserService userService = (UserService) ctx.getBean("userProxy"); userService.say();
執行結果以下:segmentfault
do before advice .... do say method
從執行結果來看,前置通知對接口方法已經起加強做用。 下面咱們將看下 Spring AOP 的具體實現。app
從上面的示例能夠看出 Spring AOP 的配置主要基於類
ProxyFactoryBean
,那麼咱們就以此爲入口去剖析其實現。
從 ProxyFactoryBean
的類結構,咱們發現其實現了接口 BeanFactoryAware
,也就說明在其實例化過程當中會調用方法 setBeanFactory
; 源碼以下:測試
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { // 設置 beanFactory this.beanFactory = beanFactory; logger.debug("Set BeanFactory. Will configure interceptor beans..."); // 建立 advisor chain createAdvisorChain(); logger.info("ProxyFactoryBean config: " + this); if (singleton) { // Eagerly initialize the shared singleton instance getSingletonInstance(); // We must listen to superclass advice change events to recache singleton // instance if necessary addListener(this); } }
在 setBeanFactory
方法中除了設置 beanFactory
, 還有一個重要的動做就是 createAdvisorChain
建立 advisor chain (也能夠理解爲就是切面鏈)。 那麼下面咱們將看下具體是怎樣建立 advisor chain 的。ui
private void createAdvisorChain() throws AopConfigException, BeansException { // 檢測是否配置了 interceptorNames, 也就是是否配置相關 advice 通知; 若沒有配置直接返回 if (this.interceptorNames == null || this.interceptorNames.length == 0) { //throw new AopConfigException("Interceptor names are required"); return; } // Globals can't be last if (this.interceptorNames[this.interceptorNames.length - 1].endsWith(GLOBAL_SUFFIX)) { throw new AopConfigException("Target required after globals"); } // Materialize interceptor chain from bean names for (int i = 0; i < this.interceptorNames.length; i++) { String name = this.interceptorNames[i]; logger.debug("Configuring interceptor '" + name + "'"); // 判斷 interceptor name 是否以 * 結尾 if (name.endsWith(GLOBAL_SUFFIX)) { if (!(this.beanFactory instanceof ListableBeanFactory)) { throw new AopConfigException("Can only use global advisors or interceptors with a ListableBeanFactory"); } else { addGlobalAdvisor((ListableBeanFactory) this.beanFactory, name.substring(0, name.length() - GLOBAL_SUFFIX.length())); } } else { // add a named interceptor // 獲取 advice bean Object advice = this.beanFactory.getBean(this.interceptorNames[i]); // 將 advisor 加入到鏈表中 addAdvisor(advice, this.interceptorNames[i]); } } }
private void addAdvisor(Object next, String name) { logger.debug("Adding advisor or TargetSource [" + next + "] with name [" + name + "]"); // We need to add a method pointcut so that our source reference matches // what we find from superclass interceptors. // 查找 advice 通知匹配的 pointcut, 並建立一個 advisor Object advisor = namedBeanToAdvisorOrTargetSource(next); if (advisor instanceof Advisor) { // if it wasn't just updating the TargetSource logger.debug("Adding advisor with name [" + name + "]"); addAdvisor((Advisor) advisor); this.sourceMap.put(advisor, name); } else { logger.debug("Adding TargetSource [" + advisor + "] with name [" + name + "]"); setTargetSource((TargetSource) advisor); // save target name this.targetName = name; } }
從 addAdvisor
方法能夠看到,在添加 advisor 前,須要先建立 advisor , 會調用方法 namedBeanToAdvisorOrTargetSource
this
private Object namedBeanToAdvisorOrTargetSource(Object next) { try { // 將 advice 包裝成一個 advisor Advisor adv = GlobalAdvisorAdapterRegistry.getInstance().wrap(next); return adv; } catch (UnknownAdviceTypeException ex) { } }
namedBeanToAdvisorOrTargetSource
方法會調用單例模式的 GlobalAdvisorAdapterRegistry
的方法 wrap
將 advice 包裝成一個 advisor;
在查看 wrap
的實現以前,咱們能夠先看下 GlobalAdvisorAdapterRegistry
是作什麼的。lua
public class GlobalAdvisorAdapterRegistry extends DefaultAdvisorAdapterRegistry { private static GlobalAdvisorAdapterRegistry instance = new GlobalAdvisorAdapterRegistry(); public static GlobalAdvisorAdapterRegistry getInstance() { return instance; } private GlobalAdvisorAdapterRegistry() { } } public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry { private List adapters = new LinkedList(); public DefaultAdvisorAdapterRegistry() { // register well-known adapters registerAdvisorAdapter(new BeforeAdviceAdapter()); registerAdvisorAdapter(new AfterReturningAdviceAdapter()); registerAdvisorAdapter(new ThrowsAdviceAdapter()); } }
從上面 GlobalAdvisorAdapterRegistry
的實現能夠看出其採用了單例模式並繼承了類 DefaultAdvisorAdapterRegistry
在構造的過程當中內置了 3 種 advice adapter 用於匹配 advice 。 下面咱們在看下它是如何 wrap
包裝 advice 的。spa
public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException { if (adviceObject instanceof Advisor) { return (Advisor) adviceObject; } if (!(adviceObject instanceof Advice)) { throw new UnknownAdviceTypeException(adviceObject); } Advice advice = (Advice) adviceObject; if (advice instanceof Interceptor) { // So well-known it doesn't even need an adapter return new DefaultPointcutAdvisor(advice); } // 遍歷內置的 advice adapters for (int i = 0; i < this.adapters.size(); i++) { // Check that it is supported AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i); // 判斷當前 adapter 是否支付當前 advice if (adapter.supportsAdvice(advice)) { // 若是支持的話,返回一個 DefaultPointcutAdvisor return new DefaultPointcutAdvisor(advice); } } throw new UnknownAdviceTypeException(advice); }
從 wrap
的實現能夠發現,若 advice 匹配了某個 adapter 將會建立一個 DefaultPointcutAdvisor
實例並返回;翻譯
public class DefaultPointcutAdvisor implements PointcutAdvisor, Ordered { private int order = Integer.MAX_VALUE; private Pointcut pointcut; private Advice advice; public DefaultPointcutAdvisor() { } public DefaultPointcutAdvisor(Advice advice) { this(Pointcut.TRUE, advice); } public DefaultPointcutAdvisor(Pointcut pointcut, Advice advice) { this.pointcut = pointcut; this.advice = advice; } } /** * Canonical instance that matches everything. * 默認匹配全部的類及類下的全部方法 */ Pointcut TRUE = new Pointcut() { public ClassFilter getClassFilter() { return ClassFilter.TRUE; } public MethodMatcher getMethodMatcher() { return MethodMatcher.TRUE; } public String toString() { return "Pointcut.TRUE"; } };
從 DefaultPointcutAdvisor
的實例能夠看出建立 advisor (切面) 的過程實際就是將 advice (通知) 和 pointcut (切入點) 綁定的過程;同時在 Spring AOP 默認的 pointcut 是攔截全部類下的全部方法。
簡單點說也就是當前切面將會攔截哪些類下的哪些方法,攔截過程當中會採用哪些加強處理(前置通知,返回通知,異常通知)。
至此 advisor chain 的建立流程結束,其過程大概以下:
從 ProxyFactoryBean
類的名字及類結構,發現其實現接口 FactoryBean
, 也就是說當其 getBean
的時候會調用方法 getObject
, 源碼以下:
public Object getObject() throws BeansException { // 默認單例 return (this.singleton) ? getSingletonInstance() : newPrototypeInstance(); } private Object getSingletonInstance() { if (this.singletonInstance == null) { // This object can configure the proxy directly if it's // being used as a singleton. this.singletonInstance = createAopProxy().getProxy(); } return this.singletonInstance; } protected synchronized AopProxy createAopProxy() { if (!isActive) { activate(); } return getAopProxyFactory().createAopProxy(this); }
public AopProxy createAopProxy(AdvisedSupport advisedSupport) throws AopConfigException { // 是否採用 cglib 代理 boolean useCglib = advisedSupport.getOptimize() || advisedSupport.getProxyTargetClass() || advisedSupport.getProxiedInterfaces().length == 0; if (useCglib) { return CglibProxyFactory.createCglibProxy(advisedSupport); } else { // Depends on whether we have expose proxy or frozen or static ts return new JdkDynamicAopProxy(advisedSupport); } }
public Object getProxy(ClassLoader cl) { logger.debug("Creating JDK dynamic proxy"); Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised); return Proxy.newProxyInstance(cl, proxiedInterfaces, this); }
ProxyFactoryBean
經過判斷 proxyTargetClass , interfaceNames 的配置去選擇採用 cglib 或者 jdk 來建立目標代理對象。
上面簡單介紹了代理對象的建立,那麼在看下當咱們調用目標方法的時候,代理是如何執行的,以 jdk 動態代理爲例:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { MethodInvocation invocation = null; Object oldProxy = null; boolean setProxyContext = false; TargetSource targetSource = advised.targetSource; Class targetClass = null; Object target = null; try { // Try special rules for equals() method and implementation of the // Advised AOP configuration interface // Short-circuit expensive Method.equals() call, as Object.equals() isn't overloaded if (method.getDeclaringClass() == Object.class && "equals".equals(method.getName())) { // What if equals throws exception!? // This class implements the equals() method itself return new Boolean(equals(args[0])); } else if (Advised.class == method.getDeclaringClass()) { // Service invocations on ProxyConfig with the proxy config return AopProxyUtils.invokeJoinpointUsingReflection(this.advised, method, args); } Object retVal = null; // May be null. Get as late as possible to minimize the time we "own" the target, // in case it comes from a pool. // 目標實現類 target = targetSource.getTarget(); if (target != null) { targetClass = target.getClass(); } if (this.advised.exposeProxy) { // Make invocation available if necessary oldProxy = AopContext.setCurrentProxy(proxy); setProxyContext = true; } // Get the interception chain for this method // 獲取目標類,執行方法的 interception chain List chain = this.advised.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice( this.advised, proxy, method, targetClass); // Check whether we have any advice. If we don't, we can fallback on // direct reflective invocation of the target, and avoid creating a MethodInvocation if (chain.isEmpty()) { // We can skip creating a MethodInvocation: just invoke the target directly // Note that the final invoker must be an InvokerInterceptor so we know it does // nothing but a reflective operation on the target, and no hot swapping or fancy proxying retVal = AopProxyUtils.invokeJoinpointUsingReflection(target, method, args); } else { invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); // Proceed to the joinpoint through the interceptor chain // 方法調用 retVal = invocation.proceed(); } // Massage return value if necessary if (retVal != null && retVal == target) { retVal = proxy; } return retVal; } finally { } }
首先咱們看下如何獲取匹配當前 method 的攔截器, 參考 calculateInterceptorsAndDynamicInterceptionAdvice
的實現以下:
public static List calculateInterceptorsAndDynamicInterceptionAdvice(Advised config, Object proxy, Method method, Class targetClass) { // 用於存儲攔截器 List interceptors = new ArrayList(config.getAdvisors().length); // 遍歷 advisor (切面) for (int i = 0; i < config.getAdvisors().length; i++) { Advisor advisor = config.getAdvisors()[i]; if (advisor instanceof PointcutAdvisor) { // Add it conditionally PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor; // 判斷當前 target class 是否當前 pointcut if (pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) { // 獲取 advisor 對應的 method interceptor MethodInterceptor interceptor = (MethodInterceptor) GlobalAdvisorAdapterRegistry.getInstance().getInterceptor(advisor); MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher(); // 判斷當前 method 是否匹配 pointcut if (mm.matches(method, targetClass)) { if (mm.isRuntime()) { // Creating a new object instance in the getInterceptor() method // isn't a problem as we normally cache created chains interceptors.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm) ); } else { // 將攔截器加入鏈表中 interceptors.add(interceptor); } } } } else if (advisor instanceof IntroductionAdvisor) { IntroductionAdvisor ia = (IntroductionAdvisor) advisor; if (ia.getClassFilter().matches(targetClass)) { MethodInterceptor interceptor = (MethodInterceptor) GlobalAdvisorAdapterRegistry.getInstance().getInterceptor(advisor); interceptors.add(interceptor); } } } // for return interceptors; } // calculateInterceptorsAndDynamicInterceptionAdvice
咱們在詳細看下如何查找 advisor 匹配的攔截器呢,一樣與上文中 wrap
相似,以下:
public Interceptor getInterceptor(Advisor advisor) throws UnknownAdviceTypeException { Advice advice = advisor.getAdvice(); if (advice instanceof Interceptor) { return (Interceptor) advice; } // 遍歷內置的 advisor adapter for (int i = 0; i < this.adapters.size(); i++) { AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i); // 是否匹配當前 advice if (adapter.supportsAdvice(advice)) { // 匹配的話返回 interceptor return adapter.getInterceptor(advisor); } } throw new UnknownAdviceTypeException(advisor.getAdvice()); }
到目前爲止,咱們屢次發現 AdvisorAdapter
的身影,下面咱們看下其具體的實現, 以 BeforeAdviceAdapter
爲例:
class BeforeAdviceAdapter implements AdvisorAdapter { /** * @see org.springframework.aop.framework.adapter.AdvisorAdapter#supportsAdvice(java.lang.Object) */ public boolean supportsAdvice(Advice advice) { // 匹配 MethodBeforeAdvice return advice instanceof MethodBeforeAdvice; } /** * @see org.springframework.aop.framework.adapter.AdvisorAdapter#getInterceptor(org.springframework.aop.Advisor) */ public Interceptor getInterceptor(Advisor advisor) { MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice(); // 返回 MethodBeforeAdviceInterceptor return new MethodBeforeAdviceInterceptor(advice) ; } }
經過
AdvisorAdapter
很巧妙的將 Advice 和 Interceptor 結合起來,同時也會發現兩者關係是一一對應的
下面在看下方法的真正調用過程, 由 ReflectiveMethodInvocation
的方法 proceed
實現:
public Object proceed() throws Throwable { // We start with an index of -1 and increment early // 當執行到最後一個攔截器的時候將會調用目標方法 if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) { return invokeJoinpoint(); } // 獲取下一個攔截器 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 InterceptorAndDynamicMethodMatcher dm = (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice; if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) { return dm.interceptor.invoke(this); } else { // Dynamic matching failed // Skip this interceptor and invoke the next in the chain return proceed(); } } else { // It's an interceptor so we just invoke it: the pointcut will have // been evaluated statically before this object was constructed // 執行攔截器 return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this); } }
下面具體看下 MethodInterceptor
的實現,分別是前置通知,返回通知,異常通知
public Object invoke(MethodInvocation mi) throws Throwable { // 目標方法前執行 advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() ); return mi.proceed(); } public Object invoke(MethodInvocation mi) throws Throwable { // 先執行目標方法 Object retval = mi.proceed(); // 後置處理 advice.afterReturning(retval, mi.getMethod(), mi.getArguments(), mi.getThis() ); return retval; } public Object invoke(MethodInvocation mi) throws Throwable { try { // 執行目標方法 return mi.proceed(); } catch (Throwable t) { // 異常處理 Method handlerMethod = getExceptionHandler(t); if (handlerMethod != null) { invokeHandlerMethod(mi, t, handlerMethod); } throw t; } }
至此 Spring AOP 代理對象的執行過程處理結束,其流程可大概總結以下:
獲取當前目標方法的 interceptor chain
Spring AOP 中的對象關係小結下:
使用 Spring 1.0 版本時, 當咱們自定義 Advice 時,可不能夠同時支持多種 Advice 呢 ? 譬如:
public class UserAdvice implements MethodBeforeAdvice, AfterReturningAdvice { public void before(Method m, Object[] args, Object target) throws Throwable { System.out.println("do before advice ...."); } public void afterReturning(Object returnValue, Method m, Object[] args, Object target) throws Throwable { System.out.println("do after returning ...."); } }
那麼當測試後,您會發現只有 before 調用了,而 afterReturning 未調用了;這是爲何呢 ? (好好看源碼額)