Spring AOP basic component Advisor

Related Reading

Introduction

hold AOPAdviceand decisionAdviceApplicability filters (such asPointcut) base interface;
Spring AOP is based on the AOP interception API compliantMethodInterceptorsupport notifications,AdvisorThe interface allows to support different types of notifications, such as: pre-notification, post-notification which do not need to be implemented through interceptors;

source code

public  interface  Advisor  {

    // Empty 
    Advice Advice EMPTY_ADVICE =  new  Advice ( )  { } ;

    // Get notifications, which can be interceptors, pre-notifications, throwing exception notifications, etc. 
    Advice  getAdvice ( ) ;

    // Is it related to each notified instance 
    // The current framework has not yet used this method, you can ensure the correct life cycle of the Advisor through singleton/multiple instance, or appropriate proxy creation 
    boolean  isPerInstance ( ) ; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

implement subclass

Advisor

IntroductionAdvisor

Introduction

AOP supportintroductionfunctionalAdvisorbasic interface;

core code

public  interface  IntroductionAdvisor  extends  Advisor ,  IntroductionInfo  {

    // Get the class filter that determines the applicability of the referral 
    ClassFilter  getClassFilter ( ) ;

    // Verify that the notified interface can apply the IntroductionAdvisor 
    void  validateInterfaces ( )  throws  IllegalArgumentException ; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

DefaultIntroductionAdvisor

Introduction

IntroductionAdvisorSimple implementation of , applied by defaultintroductionto any type;

core code

public  class  DefaultIntroductionAdvisor  implements  IntroductionAdvisor ,  ClassFilter ,  Ordered ,  Serializable  {

    private  final  advice advice ;

    private  final  Set < Class < ? > > interfaces =  new  LinkedHashSet < > ( ) ;


    public  DefaultIntroductionAdvisor ( Advice advice ,  @Nullable  IntroductionInfo introductionInfo )  { 
        // Check Advice 
        Assert . notNull ( advice ,  "Advice must not be null" ) ; 
        this . advice = advice ; 
        if  ( introductionInfo !=  null )  { 
            Class < ? > [ ] introducedInterfaces = introductionInfo . getInterfaces( ) ; 
            if  ( introducedInterfaces . length ==  0 )  { 
                throw  new  IllegalArgumentException ( 
                        "IntroductionInfo defines no interfaces to introduce: "  + introductionInfo ) ; 
            } 
            // apply the introduction interface info 
            for  ( Class < ? > ifc : introducedInterfaces )  { 
                addInterface ( ifc ) ; 
            } 
        } 
    }

    public  DefaultIntroductionAdvisor ( DynamicIntroductionAdvice advice ,  Class < ? > ifc )  { 
        // Validate Advice 
        Assert . notNull ( advice ,  "Advice must not be null" ) ; 
        this . advice = advice ; 
        addInterface ( ifc ) ; 
    }

    public  void  addInterface ( Class < ? > ifc )  { 
        Assert . notNull ( ifc ,  "Interface must not be null" ) ; 
        if  ( ! ifc . isInterface ( ) )  { 
            throw  new  IllegalArgumentException ( "Specified class ["  + ifc . getName ( )  +  "] must be an interface" ) ; 
        } 
        this . interfaces.add ( ifc ) ; 
    } _

    @Override 
    public  Class < ? > [ ]  getInterfaces ( )  { 
        return  ClassUtils . toClassArray ( this . interfaces ) ; 
    }

    @Override 
    public  void  validateInterfaces ( )  throws  IllegalArgumentException  { 
        for  ( Class < ? > ifc :  this . interfaces )  { 
            // Verify that the current Advice supports all interfaces 
            if  ( this . advice instanceof  DynamicIntroductionAdvice  && 
                    ! ( ( DynamicIntroductionAdvice )  this . advice ) . implementsInterface ( ifc ) ) { 
                throw  new  IllegalArgumentException ( "DynamicIntroductionAdvice ["  +  this . advice +  "] "  + 
                        "does not implement interface ["  + ifc . getName ( )  +  "] specified for introduction" ) ; 
            } 
        } 
    }

    @Override 
    public  Advice  getAdvice ( )  { 
        return  this . advice ; 
    }

    @Override 
    public  boolean  isPerInstance ( )  { 
        return  true ; 
    }

    @Override 
    public  ClassFilter  getClassFilter ( )  { 
        // return itself 
        return  this ; 
    }

    @Override 
    public  boolean  matches ( Class < ? > clazz )  { 
        // matches any type 
        return  true ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

PointcutAdvisor

Introduction

Depend onPointcutDrivenAdvisorbase interface;
covers all butReferral Advisoralmost allAdvisor;

core code

public  interface  PointcutAdvisor  extends  Advisor  {

    // Get Pointcut 
    Pointcut  getPointcut ( ) ; 
}
  • 1
  • 2
  • 3
  • 4
  • 5

StaticMethodMatcherPointcutAdvisor

Introduction

at the same time asStaticMethodMatcherPointcutofAdvisorThe base abstract class of ;

core code

public  abstract  class  StaticMethodMatcherPointcutAdvisor  extends  StaticMethodMatcherPointcut 
        implements  PointcutAdvisor ,  Ordered ,  Serializable  {

    // default empty advice 
    private  Advice advice = EMPTY_ADVICE ;


    public  StaticMethodMatcherPointcutAdvisor ( Advice advice )  { 
        // Check Advice 
        Assert . notNull ( advice ,  "Advice must not be null" ) ; 
        this . advice = advice ; 
    }

    @Override 
    public  Advice  getAdvice ( )  { 
        return  this . advice ; 
    }

    @Override 
    public  boolean  isPerInstance ( )  { 
        return  true ; 
    }

    @Override 
    public  Pointcut  getPointcut ( )  { 
        // return itself 
        return  this ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

AspectJPointcutAdvisor

Introduction

adaptationAbstractAspectJAdviceforPointcutAdvisorinterface;

core code

public  class  AspectJPointcutAdvisor  implements  PointcutAdvisor ,  Ordered  {

    private  final  AbstractAspectJAdvice advice ;

    private  final  Pointcut pointcut ;


    public  AspectJPointcutAdvisor ( AbstractAspectJAdvice advice )  { 
        // Validate Advice 
        Assert . notNull ( advice ,  "Advice must not be null" ) ; 
        this . advice = advice ; 
        this . pointcut = advice . buildSafePointcut ( ) ; 
    }

    @Override 
    public  boolean  isPerInstance ( )  { 
        return  true ; 
    }

    @Override 
    public  Advice  getAdvice ( )  { 
        return  this . advice ; 
    }

    @Override 
    public  Pointcut  getPointcut ( )  { 
        return  this . pointcut ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

AsyncAnnotationAdvisor

Introduction

The implementation of Spring Async function, support through @Asyncannotations andjavax.ejb.AsynchronousAnnotation to activate asynchronous method executionAdvisor;

core code

public  class  AsyncAnnotationAdvisor  extends  AbstractPointcutAdvisor  implements  BeanFactoryAware  {

    private  Advice advice ;

    private  Pointcut pointcut ;


    public  AsyncAnnotationAdvisor ( 
            @Nullable  Supplier < Executor > executor ,  @Nullable  Supplier < AsyncUncaughtExceptionHandler > exceptionHandler )  {

        // Async and javax.ejb.Asynchronous are supported by default, so the size is 2 
        Set < Class < ?  extends  Annotation >> > asyncAnnotationTypes =  new  LinkedHashSet < > ( 2 ) ; 
        asyncAnnotationTypes . add ( Async . class ) ; 
        try  { 
            asyncAnnotationTypes . add ( ( Class < ?  extends  Annotation > ) 
                    ClassUtils .forName _( "javax.ejb.Asynchronous" ,  AsyncAnnotationAdvisor . class . getClassLoader ( ) ) ) ; 
        } 
        catch  ( ClassNotFoundException ex )  { 
            // If EJB 3.1 API not present, simply ignore. 
        } 
        // Generated from async executor and exception handler Advice 
        this . advice =  buildAdvice ( executor , exceptionHandler ) ; 
        // generate Pointcut 
        this . pointcut =  buildPointcut ( asyncAnnotationTypes ); 
    }

    @Override 
    public  Advice  getAdvice ( )  { 
        return  this . advice ; 
    }

    @Override 
    public  Pointcut  getPointcut ( )  { 
        return  this . pointcut ; 
    }

    protected  Advice  buildAdvice ( 
            @Nullable  Supplier < Executor > executor ,  @Nullable  Supplier < AsyncUncaughtExceptionHandler > exceptionHandler )  { 
        // use AnnotationAsyncExecutionInterceptor 
        AnnotationAsyncExecutionInterceptor interceptor =  new  AnnotationAsyncExecutionInterceptor ( null ) ; 
        interceptor . configure ( executor , exceptionHandler ) ; 
        returninterceptor ; 
    }

    protected  Pointcut  buildPointcut ( Set < Class < ?  extends  Annotation > > asyncAnnotationTypes )  { 
        ComposablePointcut result =  null ; 
        // Traverse the set of annotation types to get a ComposablePointcut 
        for  ( Class < ?  extends  Annotation > asyncAnnotationType : asyncAnnotationTypes )  { 
            // match classes, ignore method(MethodMatcher.TRUE) 
            Pointcut cpc =  new  AnnotationMatchingPointcut (asyncAnnotationType ,  true ) ; 
            // match method, ignore class (ClassFilter.TRUE) 
            Pointcut mpc =  new  AnnotationMatchingPointcut ( null , asyncAnnotationType ,  true ) ; 
            if  ( result ==  null )  { 
                result =  new  ComposablePointcut ( cpc ) ; 
            } 
            else  { 
                result .union ( cpc ) ; } _
            
            result = result . union ( mpc ) ; 
        } 
        // If there is no annotation type, it is considered a full match 
        return  ( result !=  null  ? result :  Pointcut . TRUE ) ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

BeanFactoryCacheOperationSourceAdvisor

Introduction

The implementation of the Spring Cache function, based onCacheOperationSourcerealizedAdvisor, internally heldAdviceinstance;

core code

public  class  BeanFactoryCacheOperationSourceAdvisor  extends  AbstractBeanFactoryPointcutAdvisor  {

    // Determine whether the current method has a cache annotation, which is used by CacheOperationSourcePointcut to implement the entry point logic 
    @Nullable 
    private  CacheOperationSource cacheOperationSource ;

    // Use CacheOperationSourcePointcut based on CacheOperationSource as pointcut 
    private  final  CacheOperationSourcePointcut pointcut =  new  CacheOperationSourcePointcut ( )  { 
        @Override 
        @Nullable 
        protected  CacheOperationSource  getCacheOperationSource ( )  { 
            return cacheOperationSource ; 
        } 
    } ;

    @Override 
    public  Pointcut  getPointcut ( )  { 
        return  this . pointcut ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

BeanFactoryTransactionAttributeSourceAdvisor

Introduction

The implementation of Spring annotation-driven transaction management function, based onTransactionAttributeSourcerealizedAdvisor, internally heldAdviceinstance;

core method

public  class  BeanFactoryTransactionAttributeSourceAdvisor  extends  AbstractBeanFactoryPointcutAdvisor  {

    // Determine whether the current method has the @Transactional annotation, which is used by TransactionAttributeSourcePointcut to implement the entry point logic 
    @Nullable 
    private  TransactionAttributeSource transactionAttributeSource ;

    // Use TransactionAttributeSourcePointcut based on TransactionAttributeSource as pointcut 
    private  final  TransactionAttributeSourcePointcut pointcut =  new  TransactionAttributeSourcePointcut ( )  { 
        @Override 
        @Nullable 
        protected  TransactionAttributeSource  getTransactionAttributeSource ( )  { 
            return transactionAttributeSource ; 
        } 
    } ;

    @Override 
    public  Pointcut  getPointcut ( )  { 
        return  this . pointcut ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

TransactionAttributeSourceAdvisor

Introduction

The implementation of Spring annotation-driven transaction management function, based onTransactionAttributeSourcerealizedAdvisor, internally heldTransactionInterceptorinstance;

core code

public  class  TransactionAttributeSourceAdvisor  extends  AbstractPointcutAdvisor  {

    // Used by TransactionAttributeSourcePointcut to implement entry point logic 
    @Nullable 
    private  TransactionInterceptor transactionInterceptor ;

    // Use TransactionAttributeSourcePointcut based on TransactionAttributeSource as pointcut 
    private  final  TransactionAttributeSourcePointcut pointcut =  new  TransactionAttributeSourcePointcut ( )  { 
        @Override 
        @Nullable 
        protected  TransactionAttributeSource  getTransactionAttributeSource ( )  { 
            return  ( transactionInterceptor !=  null  ? transactionInterceptor . getTransactionAttributeSource ( )  :  null ) ; 
        } 
    } ;


    @Override 
    public  Advice  getAdvice ( )  { 
        Assert . state ( this . transactionInterceptor !=  null ,  "No TransactionInterceptor set" ) ; 
        return  this . transactionInterceptor ; 
    }

    @Override 
    public  Pointcut  getPointcut ( )  { 
        return  this . pointcut ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

DefaultPointcutAdvisor

Introduction

almost the most versatilePointcutAdvisorimplementation, can use any typePointcutand divideintroductionany typeAdvice;

core code

public  class  DefaultPointcutAdvisor  extends  AbstractGenericPointcutAdvisor  implements  Serializable  {

    private  Pointcut pointcut =  Pointcut .TRUE ; _


    public  DefaultPointcutAdvisor ( Pointcut pointcut ,  Advice advice )  { 
        this . pointcut = pointcut ; 
        setAdvice ( advice ) ; 
    }

    public  void  setPointcut ( @Nullable  Pointcut pointcut )  { 
        this . pointcut =  ( pointcut !=  null  ? pointcut :  Pointcut . TRUE ) ; 
    }

    @Override 
    public  Pointcut  getPointcut ( )  { 
        return  this . pointcut ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

NameMatchMethodPointcutAdvisor

Introduction

useNameMatchMethodPointcutofAdvisor;

core code

public  class  NameMatchMethodPointcutAdvisor  extends  AbstractGenericPointcutAdvisor  {

    private  final  NameMatchMethodPointcut pointcut =  new  NameMatchMethodPointcut ( ) ;


    public  NameMatchMethodPointcutAdvisor ( Advice advice )  { 
        setAdvice ( advice ) ; 
    }

    public  void  setMappedName ( String mappedName )  { 
        this . pointcut . setMappedName ( mappedName ) ; 
    }

    public  void  setMappedNames ( String . . . mappedNames )  { 
        this . pointcut . setMappedNames ( mappedNames ) ; 
    }

    public  NameMatchMethodPointcut  addMethodName ( String name )  { 
        return  this . pointcut . addMethodName ( name ) ; 
    }

    @Override 
    public  Pointcut  getPointcut ( )  { 
        return  this . pointcut ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

RegexpMethodPointcutAdvisor

Introduction

useAbstractRegexpMethodPointcutofAdvisor;

core code

public  class  RegexpMethodPointcutAdvisor  extends  AbstractGenericPointcutAdvisor  {

    @Nullable 
    private  String [ ] patterns ;

    @Nullable 
    private  AbstractRegexpMethodPointcut pointcut ;


    public  RegexpMethodPointcutAdvisor ( String pattern ,  Advice advice )  { 
        setPattern ( pattern ) ; 
        setAdvice ( advice ) ; 
    }

    public  RegexpMethodPointcutAdvisor ( String [ ] patterns ,  Advice advice )  { 
        setPatterns ( patterns ) ; 
        setAdvice ( advice ) ; 
    }

    @Override 
    public  Pointcut  getPointcut ( )  { 
        synchronized  ( this . pointcutMonitor )  { 
            if  ( this . pointcut ==  null )  { 
                this . pointcut =  createPointcut ( ) ; 
                if  ( this . patterns !=  null )  { 
                    //Set the regular expression 
                    this .pointcut.setPatterns ( this .patterns _ _ _ _) ; 
                } 
            } 
            return  this . pointcut ; 
        } 
    }

    protected  AbstractRegexpMethodPointcut  createPointcut ( )  { 
        // use JdkRegexpMethodPointcut 
        return  new  JdkRegexpMethodPointcut ( ) ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

Tags: Spring AOP basic component Advisor

Spring AOP spring aop

Related: Spring AOP basic component Advisor