spring aop api

  

* POINTCUT

一,concepts  

  spring's pointcut model enables pointcut reuse independent of advise type。so ,you can target different advice with the same pointcut。

 1. the org.springframework.aop.Pointcut interface is the central interface,used to connection advices to classes and methods。

1 public interface Pointcut {
2     Pointcut TRUE = TruePointcut.INSTANCE;
3 
4     ClassFilter getClassFilter();
5 
6     MethodMatcher getMethodMatcher();
7 }

 2. the ClassFilter  interface use matches method to decide those class or method will be adviced。

1 @FunctionalInterface
2 public interface ClassFilter {
3     ClassFilter TRUE = TrueClassFilter.INSTANCE;
4 
5     boolean matches(Class<?> var1);
6 }

  3.   the MethodMatcher interface is normally more important。

1 public interface MethodMatcher {
2     MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;
3 
4     boolean matches(Method var1, Class<?> var2);
5 
6     boolean isRuntime();
7 
8     boolean matches(Method var1, Class<?> var2, Object... var3);
9 }

 The matches(Method, Class) method is used to test whether this pointcut ever matches a given method on a target class. 

二, Operations on Pointcuts

  spring supports opertaions(notably,union and intersection)on pointcuts。i dont understand the three points。

  Union means the methods that either pointcut matches. Intersection means the methods that both pointcuts match. Union is usually more useful. 

   You can compose pointcuts by using the static methods in the org.springframework.aop.support.Pointcuts class or by using the ComposablePointcut class in the same package. However, using AspectJ pointcut expressions is usually a simpler approach.

三, Since 2.0, the most important type of pointcut used by Spring is org.springframework.aop.aspectj.AspectJExpressionPointcut. This is a pointcut that uses an AspectJ-supplied library to parse an AspectJ pointcut expression string.

四,Pointcut Superclasses

  spring provides useful pointcut superclasses to help you to implement your own pointcuts。the superclasses is ‘ org.springframework.aop.support.StaticMethodMatcherPointcut’。

 1 public abstract class StaticMethodMatcherPointcut extends StaticMethodMatcher implements Pointcut 
 2 
 3 public abstract class StaticMethodMatcher implements MethodMatcher {
 4     public StaticMethodMatcher() {
 5     }
 6 
 7     public final boolean isRuntime() {
 8         return false;
 9     }
10 
11     public final boolean matches(Method method, Class<?> targetClass, Object... args) {
12         throw new UnsupportedOperationException("Illegal MethodMatcher usage");
13     }
14 }

override matches(Method method, Class<?> targetClass, Object... args)   is ok,and you can override other methods to customize behavior。

五,Custom Pointcuts

  Because pointcuts in Spring AOP are Java classes rather than language features (as in AspectJ), you can declare custom pointcuts, whether static or dynamic. Custom pointcuts in Spring can be arbitrarily complex. However, we recommend using the AspectJ pointcut expression language, if you can.

* Advice 

一 ,Lifecycles

  Each advice is a Spring bean. An advice instance can be shared across all advised objects or be unique to each advised object. 

二,advice type in spring

  The most fundamental advice type in Spring is interception around advice.

  Spring is compliant with the AOP Alliance interface for around advice that uses method interception. Classes that implement MethodInterceptor。

1 @FunctionalInterface
2 public interface MethodInterceptor extends Interceptor {
3     Object invoke(MethodInvocation var1) throws Throwable;
4 }

  The MethodInvocation argument to the invoke() method exposes the method being invoked, the target join point, the AOP proxy, and the arguments to the method.The invoke() method should return the invocation’s result: the return value of the join point.

1 public interface MethodInvocation extends Invocation {
2     Method getMethod();
3 }
4 
5 public interface Invocation extends Joinpoint {
6     Object[] getArguments();
7 }
1 public class DebugInterceptor implements MethodInterceptor {
2 
3     public Object invoke(MethodInvocation invocation) throws Throwable {
4         System.out.println("Before: invocation=[" + invocation + "]");
5         Object rval = invocation.proceed();
6         System.out.println("Invocation returned");
7         return rval;
8     }
9 }
1. Before Advice
1 public interface MethodBeforeAdvice extends BeforeAdvice {
2     void before(Method var1, Object[] var2, @Nullable Object var3) throws Throwable;
3 }

  Note that the return type is void. Before advice can insert custom behavior before the join point executes but cannot change the return value. If a before advice throws an exception, it aborts further execution of the interceptor chain. The exception propagates back up the interceptor chain. If it is unchecked or on the signature of the invoked method, it is passed directly to the client. Otherwise, it is wrapped in an unchecked exception by the AOP proxy.

2. Throws Advice

  it‘s a tag interface,afterThrowing([Method, args, target], subclassOfThrowable),Only the last argument is required. 

  The following advice is invoked if a RemoteException is thrown (including from subclasses):

1 public class RemoteThrowsAdvice implements ThrowsAdvice {
2 
3     public void afterThrowing(RemoteException ex) throws Throwable {
4         // Do something with remote exception
5     }
6 }

  The final example illustrates how these two methods could be used in a single class that handles both RemoteException and ServletException. Any number of throws advice methods can be combined in a single class. The following listing shows the final example:

 1 public static class CombinedThrowsAdvice implements ThrowsAdvice {
 2 
 3     public void afterThrowing(RemoteException ex) throws Throwable {
 4         // Do something with remote exception
 5     }
 6 
 7     public void afterThrowing(Method m, Object[] args, Object target, ServletException ex) {
 8         // Do something with all arguments
 9     }
10 }

3. After Returning Advice

  An after returning advice has access to the return value (which it cannot modify), the invoked method, the method’s arguments, and the target.

1 public interface AfterReturningAdvice extends AfterAdvice {
2     void afterReturning(@Nullable Object var1, Method var2, Object[] var3, @Nullable Object var4) throws Throwable;
3 }

4. introduction Advice

  Spring treats introduction advice as a special kind of interception advice。

  Introduction requires an IntroductionAdvisor and an IntroductionInterceptor that implement the following interface:  

1   public interface IntroductionInterceptor extends MethodInterceptor, DynamicIntroductionAdvice {
2   }
3   public interface DynamicIntroductionAdvice extends Advice {
4       boolean implementsInterface(Class<?> var1);
5   }

  The invoke() method inherited from the AOP Alliance MethodInterceptor interface must implement the introduction. That is, if the invoked method is on an introduced interface, the introduction interceptor is responsible for handling the method call — it cannot invoke proceed().

  Introduction advice cannot be used with any pointcut, as it applies only at the class, rather than the method, level. You can only use introduction advice with the IntroductionAdvisor, which has the following methods:

1 public interface IntroductionAdvisor extends Advisor, IntroductionInfo {
2     ClassFilter getClassFilter();
3 
4     void validateInterfaces() throws IllegalArgumentException;
5 }
6 
7 public interface IntroductionInfo {
8     Class<?>[] getInterfaces();
9 }

* ADVISOR  

  In Spring, an Advisor is an aspect that contains only a single advice object associated with a pointcut expression.

  Apart from the special case of introductions, any advisor can be used with any advice. org.springframework.aop.support.DefaultPointcutAdvisor is the most commonly used advisor class. It can be used with a MethodInterceptorBeforeAdvice, or ThrowsAdvice.

It is possible to mix advisor and advice types in Spring in the same AOP proxy. For example, you could use an interception around advice, throws advice, and before advice in one proxy configuration. Spring automatically creates the necessary interceptor chain.

*  Using the ProxyFactoryBean to Create AOP Proxies

   The basic way to create an AOP proxy in Spring is to use the org.springframework.aop.framework.ProxyFactoryBean. This gives complete control over the pointcuts, any advice that applies, and their ordering. 

* Creating AOP Proxies Programmatically with the ProxyFactory

   It is easy to create AOP proxies programmatically with Spring. This lets you use Spring AOP without dependency on Spring IoC.
1 ProxyFactory factory = new ProxyFactory(myBusinessInterfaceImpl);
2 factory.addAdvice(myMethodInterceptor);
3 factory.addAdvisor(myAdvisor);
4 MyBusinessInterface tb = (MyBusinessInterface) factory.getProxy();

* Manipulating Advised Objects

  you can manipulate them BY using the org.springframework.aop.framework.Advised interface. Any AOP proxy can be cast to this interface, no matter which other interfaces it implements. This interface includes the following methods:

  

ok https://docs.spring.io/spring/docs/5.2.4.RELEASE/spring-framework-reference/core.html#aop-targetsource

原文地址:https://www.cnblogs.com/YsirSun/p/12500854.html