1. 程式人生 > >Spring原始碼追蹤3——AOP機制

Spring原始碼追蹤3——AOP機制

研究程式碼:

spring配置檔案

<cache:annotation-driven />

Java程式碼

@Cacheable(value = "test", key = "#city")
public Map load(String city) {}

【cache:annotation-driven機制】

本來以為會有遍歷package找類的程式碼(mybatis那個應該是這麼幹的),不過實際上只有這個。

org.springframework.cache.config.AnnotationDrivenCacheBeanDefinitionParser#parse

    public BeanDefinition parse(Element element, ParserContext parserContext) {
        String mode = element.getAttribute("mode");
        if ("aspectj".equals(mode)) {
            // mode="aspectj"
            registerCacheAspect(element, parserContext);
        }
        else {
            
// mode="proxy" registerCacheAdvisor(element, parserContext); } return null; }

姑且不管aspectj,正常是走proxy模式,註冊Advisor。

【攔截機制】

執行方法時通過代理執行。

org.springframework.aop.framework.JdkDynamicAopProxy#invoke

    public Object invoke(Object proxy, Method method, Object[] args) throws
Throwable { MethodInvocation invocation; Object oldProxy = null; boolean setProxyContext = false; TargetSource targetSource = this.advised.targetSource; Class<?> targetClass = null; Object target = null; try { if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) { // The target does not implement the equals(Object) method itself. return equals(args[0]); } if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) { // The target does not implement the hashCode() method itself. return hashCode(); } if (!this.advised.opaque && method.getDeclaringClass().isInterface() && method.getDeclaringClass().isAssignableFrom(Advised.class)) { // Service invocations on ProxyConfig with the proxy config... return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args); } Object retVal; if (this.advised.exposeProxy) { // Make invocation available if necessary. oldProxy = AopContext.setCurrentProxy(proxy); setProxyContext = true; } // 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(); } // Get the interception chain for this method. List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(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 = AopUtils.invokeJoinpointUsingReflection(target, method, args); } else { // We need to create a method invocation... 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. Class<?> returnType = method.getReturnType(); if (retVal != null && retVal == target && returnType.isInstance(proxy) && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) { // Special case: it returned "this" and the return type of the method // is type-compatible. Note that we can't help if the target sets // a reference to itself in another returned object. retVal = proxy; } else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) { throw new AopInvocationException( "Null return value from advice does not match primitive return type for: " + method); } return retVal; } finally { if (target != null && !targetSource.isStatic()) { // Must have come from TargetSource. targetSource.releaseTarget(target); } if (setProxyContext) { // Restore old proxy. AopContext.setCurrentProxy(oldProxy); } } }

通過getInterceptorsAndDynamicInterceptionAdvice獲取生效的Interceptor,中間走了個快取,此處忽略。

org.springframework.aop.framework.DefaultAdvisorChainFactory#getInterceptorsAndDynamicInterceptionAdvice

    public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
            Advised config, Method method, Class<?> targetClass) {

        // This is somewhat tricky... We have to process introductions first,
        // but we need to preserve order in the ultimate list.
        List<Object> interceptorList = new ArrayList<Object>(config.getAdvisors().length);
        Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
        boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
        AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();

        for (Advisor advisor : config.getAdvisors()) { // 遍歷註冊的Advisor
            if (advisor instanceof PointcutAdvisor) {
                // Add it conditionally.
                PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
                if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
                    MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
                    MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
                    if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) { // 判斷方法是否適用Advisor
                        if (mm.isRuntime()) {
                            // Creating a new object instance in the getInterceptors() method
                            // isn't a problem as we normally cache created chains.
                            for (MethodInterceptor interceptor : interceptors) {
                                interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
                            }
                        }
                        else {
                            interceptorList.addAll(Arrays.asList(interceptors));
                        }
                    }
                }
            }
            else if (advisor instanceof IntroductionAdvisor) {
                IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
                if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
                    Interceptor[] interceptors = registry.getInterceptors(advisor);
                    interceptorList.addAll(Arrays.asList(interceptors));
                }
            }
            else {
                Interceptor[] interceptors = registry.getInterceptors(advisor);
                interceptorList.addAll(Arrays.asList(interceptors));
            }
        }

        return interceptorList;
    }

最終解析annotation。

spring cache:org.springframework.cache.annotation.AnnotationCacheOperationSource#determineCacheOperations

    protected Collection<CacheOperation> determineCacheOperations(AnnotatedElement ae) {
        Collection<CacheOperation> ops = null;
        for (CacheAnnotationParser annotationParser : this.annotationParsers) {
            Collection<CacheOperation> annOps = annotationParser.parseCacheAnnotations(ae);
            if (annOps != null) {
                if (ops == null) {
                    ops = new ArrayList<CacheOperation>();
                }
                ops.addAll(annOps);
            }
        }
        return ops;
    }

【advisor的註冊】

在spring完成bean建立時經過AbstractAutoProxyCreator類:

org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#wrapIfNecessary

    protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
        if (beanName != null && this.targetSourcedBeans.contains(beanName)) {
            return bean;
        }
        if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
            return bean;
        }
        if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
            this.advisedBeans.put(cacheKey, Boolean.FALSE);
            return bean;
        }

        // Create proxy if we have advice.
        Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
        if (specificInterceptors != DO_NOT_PROXY) {
            this.advisedBeans.put(cacheKey, Boolean.TRUE);
            Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
            this.proxyTypes.put(cacheKey, proxy.getClass());
            return proxy;
        }

        this.advisedBeans.put(cacheKey, Boolean.FALSE);
        return bean;
    }

獲取specificInterceptors:

org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator#findEligibleAdvisors

    protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
        List<Advisor> candidateAdvisors = findCandidateAdvisors(); // 獲取候選advisors
        List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName); // 過濾出可用的advisors
        extendAdvisors(eligibleAdvisors);
        if (!eligibleAdvisors.isEmpty()) {
            eligibleAdvisors = sortAdvisors(eligibleAdvisors);
        }
        return eligibleAdvisors;
    }

org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator#findAdvisorsThatCanApply

    protected List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) {

        ProxyCreationContext.setCurrentProxiedBeanName(beanName);

try { return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass); } finally { ProxyCreationContext.setCurrentProxiedBeanName(null); } }

org.springframework.aop.framework.autoproxy.ProxyCreationContext#setCurrentProxiedBeanName

    static void setCurrentProxiedBeanName(String beanName) {
        if (beanName != null) {
            currentProxiedBeanName.set(beanName); // ThreadLocal
        } else {
            currentProxiedBeanName.remove();
        }
    }

org.springframework.aop.support.AopUtils#canApply(org.springframework.aop.Advisor, java.lang.Class<?>, boolean)

    public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) {
        if (advisor instanceof IntroductionAdvisor) {
            return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);
        }
        else if (advisor instanceof PointcutAdvisor) {
            PointcutAdvisor pca = (PointcutAdvisor) advisor;
            return canApply(pca.getPointcut(), targetClass, hasIntroductions);
        }
        else {
            // It doesn't have a pointcut so we assume it applies.
            return true;
        }
    }

org.springframework.aop.support.AopUtils#canApply(org.springframework.aop.Pointcut, java.lang.Class<?>, boolean)

    public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {
        Assert.notNull(pc, "Pointcut must not be null");
        if (!pc.getClassFilter().matches(targetClass)) {
            return false;
        }

        MethodMatcher methodMatcher = pc.getMethodMatcher();
        IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
        if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
            introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;
        }

        Set<Class<?>> classes = new HashSet<Class<?>>(ClassUtils.getAllInterfacesForClassAsSet(targetClass)); // 獲取目標類和所有父類
        classes.add(targetClass);
        for (Class<?> clazz : classes) {
            Method[] methods = clazz.getMethods();
            for (Method method : methods) { // 遍歷方法如果有滿足的則返回true
                if ((introductionAwareMethodMatcher != null &&
                        introductionAwareMethodMatcher.matches(method, targetClass, hasIntroductions)) ||
                        methodMatcher.matches(method, targetClass)) {
                    return true;
                }
            }
        }

        return false;
    }

常用methodMatcher:

1. org.springframework.transaction.interceptor.TransactionAttributeSourcePointcut 事務

2. org.springframework.aop.aspectj.AspectJExpressionPointcut aspectj表示式

建立代理:

org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#createProxy

    protected Object createProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.copyFrom(this);

        if (!proxyFactory.isProxyTargetClass()) {
            if (shouldProxyTargetClass(beanClass, beanName)) {
                proxyFactory.setProxyTargetClass(true);
            }
            else {
                evaluateProxyInterfaces(beanClass, proxyFactory);
            }
        }

        Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
        for (Advisor advisor : advisors) {
            proxyFactory.addAdvisor(advisor);
        }

        proxyFactory.setTargetSource(targetSource);
        customizeProxyFactory(proxyFactory);

        proxyFactory.setFrozen(this.freezeProxy);
        if (advisorsPreFiltered()) {
            proxyFactory.setPreFiltered(true);
        }

        return proxyFactory.getProxy(getProxyClassLoader());
    }

相關推薦

Spring原始碼追蹤3——AOP機制

研究程式碼: spring配置檔案 <cache:annotation-driven /> Java程式碼 @Cacheable(value = "test", key = "#city") public Map load(String city) {} 【cach

spring原始碼解析之AOP原理

一、準備工作   在這裡我先簡單記錄下如何實現一個aop: AOP:【動態代理】 指在程式執行期間動態的將某段程式碼切入到指定方法指定位置進行執行的程式設計方式; 1、匯入aop模組;Spring AOP:(spring-aspects) 2、定義一個業務邏輯類(

spring原始碼-BeanPostProcessor-3.3 spring原始碼-bean之載入-2

  一、BeanPostProcessor這個是spring容器的拓展之一,是用於獲取bean的時候處理對應的物件;   二、常用場景,在獲取bean的時候,重新初始化bean的屬性等。   三、實現方式(加入容器後,呼叫其他bean的時候,通過BeanPostProcessor來進行處理) im

spring原始碼-Aware-3.4 spring原始碼-bean之載入-2

  一、Aware介面,這個也是spring的拓展之一,為啥要單獨拿出來講呢,因為他相比於BeanFactoryPostProcessor,BeanPostProcessor的實用性更加高,並且在具體的業務中也可以靈活使用,主要是能夠達到解耦的目的。   二、常用的Aware介面有:第一類:BeanName

Spring原始碼分析:AOP分析(三)

個人扯淡部落格:http://blog.ztgreat.cn 前言 在上篇部落格中,分析了Spring Aop的Advice的實現過程,其中Spring對Advice 使用了介面卡模式,將Advice包裝成了Interceptor,在最後,我們通過Spring提供的

Spring 原始碼分析(三) —— AOP(一)AOP原理

AOP概論         AOP(Aspect-Oriented Programming,面向切面的程式設計),談起AOP,則一定會追溯到OOP(Object Oriented Programming,面向物件程式設計),因為AOP可以說是對OOP的補充和完善,而這一切的

Spring 原始碼分析(三) —— AOP(二)Spring AOP 整體架構

Spring AOP 架構         先是生成代理物件,然後是攔截器的作用,最後是編織的具體實現。這是AOP實現的三個步驟,當然Spring AOP也是一樣。         而從Spring AOP整體架構上看,其核心都是建立在代理上的。當我們建立增強例項時,我們

Spring原始碼分析-深入淺出AOP(圖文分析)

這篇文章主要解決三個問題 什麼是AOP Spring 中怎麼實現的AOP AOP的應用場景 首先我們看下 到底什麼是AOP AOP的基本概念 AOP 官方定義 Aspect-Oriented Programm

Spring原始碼學習之AOP

我們都站在巨人的肩膀上 宣告:參考《spring原始碼深度解析》 1.Spring AOP主要採用動態代理實現,而動態代理分為兩種: JDK動態代理:其代理物件必須是某個介面的實現,它是通過在執行時期建立一個介面的實現類來完成對目標物件的代理。 CG

Spring原始碼追蹤2——xml解析入口

解析xml節點入口 org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(Element root) protected void doRegisterBe

Spring原始碼追蹤4——SpringMVC View解析

這次的議題是返回json和返回普通view經過的路線差異。 --------------------------------------------------------------------------------- org.springframework.web.servlet.Dispatche

Spring原始碼分析3spring XML配置檔案的解析流程

1 介紹 建立並初始化spring容器中,關鍵一步就是讀取並解析spring XML配置檔案。這個過程比較複雜,本文將詳細分析整個流程。先看涉及到的關鍵類。 XmlWebApplicationContext:web應用的預設Spring容器 XmlBean

Spring原始碼分析:AOP分析(一)

前言 Spring 的兩大核心,一是IOC,我們之前已經學習過,而另一個則是大名鼎鼎的 AOP,AOP的具體概念我就不介紹了,我們今天重點是要從原始碼層面去看看 spring 的 AOP 是如何實現的。 在Spring AOP實現中,使用的核心技術是動態代理,

3Spring原始碼分析3之載入Bean

1、Bean的載入 // 前面兩篇已經分析了讀取配置檔案,並註冊BeanDefinition BeanFactory bf = n

Spring原始碼分析之AOP從解析到呼叫

正文: 在上一篇,我們對IOC核心部分流程已經分析完畢,相信小夥伴們有所收穫,從這一篇開始,我們將會踏上新的旅程,即Spring的另一核心:AOP! 首先,為了讓大家能更有效的理解AOP,先帶大家過一下AOP中的術語: - **切面(Aspect)**:指關注點模組化,這個關注點可能會橫切多個物件。事務

spring原始碼-aop動態代理-5.3

  一、動態代理,這是一個很強大的東西哦。研發過程中我們會常用很多業務類,但是存在一個問題。如何在不修改原始碼邏輯的情況下,加入自己的相關邏輯。比如異常處理,日誌記錄等!   二、Java動態代理的兩種方式JDK、CGLIB   三、動態代理的例子   1)需要代理的類  public inte

spring筆記3-AOP

父類 ride 支持 ack beans 方法參數 cep 結構 express 一.概述 AOP:(Aspect Oriented Programming)即:面向切面編程。把我們程序重復的代碼抽取出來,在需要執行的時候,使用動態代理的技術,在不修改源碼的基礎上,對我們

Spring原始碼解析(十六)——AOP原理——獲取攔截器鏈——MethodInterceptor

   *     3)、目標方法執行    ;  *         容器中儲存了元件的代理物件(cglib增強後的物件),這個物件裡面儲存了詳細

Spring原始碼解析(十五)——AOP原理——建立aop代理

   * AnnotationAwareAspectJAutoProxyCreator【InstantiationAwareBeanPostProcessor】    的作用:  * 1)、每一個bean建立之前,呼叫postProce

Spring原始碼解析(十四)——AOP原理——AnnotationAwareAspectJAutoProxyCreator執行時機

     *             AnnotationAwareAspectJAutoProxyCreator => InstantiationAwareBean