Spring AOP introduction and implementation based interface

Warmly recommended: Super Multi IT resources, all in 798 Resource Network

Disclaimer: reprint article, do so in order to prevent the loss of this backup.
This article from the public numbers: Heart Procedure
Original Address: https://mp.weixin.qq.com/s/vo94gVyTss0LYwEcRx4iiw

Aspect Oriented Programming, abbreviated as AOP, program development is mainly used to solve some of the problems on the system level, such as logging, transaction, and other privileges. Ali system, AOP
is widely used in Tissot log, the local cache, doom and so enhance the scene.

The basic concepts of AOP

Why Oriented Programming? Object-oriented programming package to solve the problem, but also brought new problems, how to enhance an object's method? For example, an interface A may have multiple implementation classes A1, A2, ******, An, how do log or print rights management method more for the same implementation class? Common idea of ​​object-oriented tools is generally a package, and then added to all methods where, once more needs to be done to enhance the code will be very cumbersome, difficult to maintain. Therefore, only the aspect-oriented programming.

The following diagram shows the idea of aspect-oriented programming, object-oriented programming by encapsulation, inheritance, and polymorphism to achieve business logic, while the business process Oriented Programming Hengdao processing section cut in a particular transaction. A cross-pricked, the call is cut out of this.
Here Insert Picture Description
Can be seen from FIG Oriented Programming is not part of the business logic, but independent of the specific business-specific services, such as logs, permissions. Aspect Oriented Programming has the following concept to understand:

  • (1) Aspect (section): usually a class, which can define pointcuts and;
  • (2) JointPoint (connection points): during program execution point clear, the general method is invoked;
  • (3) Advice (notification): AOP executing enhancement processing on a specific entry points may be pre-processing, post-processing, exception handling and the like;
  • (4) Pointcut (entry point): with a notification that the point of attachment, is mainly reflected in the program entry point written expression;
  • (5) AOP proxy: Object created by the AOP framework, the agent is to strengthen the target object.
  • (6) woven into: the section applied to the target object to the process of creating a new proxy object.

Weaving as generally occurs in several time:

  • (1) compilation: when a file is compiled class for weaving, which requires special compiler can do that, for example, the weaving AspectJ compiler;
  • (2) when the class loader: the use of special ClassLoader target class is loaded into byte code class enhanced before the program;
  • (3) Operating: section is woven into at some point run.

Spring AOP

Spring provides a special package to rely on support AOP. In Spring AOP proxy will be a JDK dynamic proxy, the proxy may be CGLIB. Proxy generation, management and its dependencies are the responsibility of the IOC container, Spring is using JDK dynamic proxy default proxy class in need rather than when the agent interface, Spring will automatically switch to using CGLIB agent.

Spring provides several ways to achieve AOP:

  • Based on section interface: interface implemented method, define pointcuts and;
  • Annotation-based section: Based @ Aspect, @ Pointcut, @ Before, @ AfterReturning and other annotations AOP way;
  • XML-based section: aop using the xml namespace, such as aop: Aspect;

Presented here use more simple way based interfaces, several other types do introduced. Spring uses Advisor interface represents the general section, including Advice, but no entry point.

public interface Advisor {
//获取 Advice
Advice getAdvice();
//暂不用
boolean isPerInstance();
}

PointcutAdvisor interface extends Advisor, represents a starting point of the cut, which comprises two Pointcut Advice and classes. PointcutAdvisor interface can be defined based on the cut, the flexibility to define cut points connected by a class method names, comments and other information, provide more applicability section.

public interface PointcutAdvisor extends Advisor {
//获取 Pointcut
Pointcut getPointcut();
}

Spring provides six PointcutAdvisor interface implementation class, section defines six types, as follows:

  • DefaultPointcutAdvisor: The most common type of cut, it can be a section defined by any Pointcut and Advice;
  • NameMatchMethodPointcutAdvisor: define cut points may be defined by section by the class name of the method:
  • RegexpMethodPointcutAdvisor: Press section for a regular expression for matching method name defined by the tangent point, can be operated by extending the implementation class;
  • StaticMethodMatcherPointcutAdvisor: static methods defined aspects matcher cut point;
  • AspecJExpressionPointcutAdvisor: cutting point of the expression defined for Aspecj tangent point section;
  • AspecJPointcutAdvisor: AspecJ grammar section for the definition of cut-off point.

Spring AOP provides a variety of built-in interface to achieve Pointcut, we can based on these implementation-defined entry point. Common implementations include:

  • StaticMethodMatcherPointcut: static matching method, a method to achieve the level of cut, cut into the non-operation, by default matches all classes;
  • DynamicMethodMatcherPointcut: dynamic matching method, a method to achieve the level of cut, cut runs, matching all classes by default;
  • AnnotationMatchingPointcut: annotation-based matching;
  • ExpressionPointcut: support for AspectJ pointcut expression syntax;
  • ControlFlowPointcut: control flow cut-off point, the program execution information according to the target stack Check whether the method of initiating a call directly or indirectly, a method to judge whether a connection point matching;
  • ComposablePointcut: Compound tangent point, easy to operate to create a plurality of cut points based provided. It all methods return ComposablePointcut class, so that we can be manipulated using the link expression.

Spring AOP Advice provides an interface to multiple sub-interfaces support enhanced. As follows:

  • Interface MethodBeforeAdvice: Advice invoked before the target method invocation;
  • Interface AfterReturningAdvice: call after call and return to the target method Advice;
  • Interface MethodInterceptor: before and after the valid throughout the execution of the target method and the ability to control execution of the target method;
  • Interface ThrowsAdvice: Advice throws an exception when invoked target method;

Based on AOP use interface

PointcutAdvisor interface AOP only need to define the starting point Pointcut and notification Advice to others to Spring AOP framework to deal with based. Based in the interface, PointcutAdvisor implementation class defines a section.

Defined entry point getPointcut in. As in the following example, the starting point is to define all the methods have MyTestAnnotation annotations, all annotated methods are our starting point.

@Getter
public Pointcut pointcut = new StaticMethodMatcherPointcut() {
@Override
public boolean matches(Method method, Class<?> targetClass) {
return method.getAnnotation(MyTestAnnotation.class) != null;
}
};

It needs to be done to enhance the definition of () in getAdvice. The following example, to implement a method of enhancing the interface MethodInterceptor, as MethodInvocation into the reference object carries information section, the method being called, and the like into the reference. In the direct method can be called the original method, and pre-processing before calling the method, or for post-processing after the calling method.

@Getter
public Advice advice = (MethodInterceptor)methodInvocation -> {
// TODO 增强前置处理
// 调用原来的方法
Object result = methodInvocation.proceed();
// TODO 增强后置处理
return result;
};

AOP Notes

  • AOP process should not take too much time operation: AOP as a section should focus on the process simple transactions, such as print log. Consuming operations should not be placed too many AOP
    carried out, if we need to deal with, should do asynchronous processing.
  • AOP should not throw an exception: AOP does not affect the normal operation of the business logic, so be sure to add catch, ensure AOP itself anomaly does not affect the normal course of business.
  • AOP cycle can not appear: in particular, the print log section, be sure to avoid loops. When you want to print the error log AOP appear, try printing alone, to avoid the cycle.

to sum up

This article describes the basic concept of AOP, describes a method of aspect-oriented programming interface based Spring AOP.
Rational use of AOP can reduce the complexity of the system, non-invasive enhance the function of the system, it has been widely used in enterprise Java applications.

Guess you like

Origin blog.csdn.net/xianhenyuan/article/details/93406564