preface

This article will focus on the basic concepts of AOP in Spring and related interfaces, but also a review and summary of my previous learning of Spring.

What is the AOP

AOP full name is Aspect Oriented Programming, Aspect Oriented Programming, is object-oriented Programming (OOP: Object Oriented Programming) complement and perfect. In general, OOP uses inheritance, encapsulation and polymorphism to simulate business modules and logic. AOP encapsulates non-business functions that are frequently invoked by business modules, such as logging, authorization, and transactions. Using AOP can effectively reduce the duplication of code in the system, reduce the coupling between modules, and facilitate maintenance.

AOP related terms

Before we start working with AOP, let’s familiarize ourselves with AOP concepts and terminology. These terms are not specific to Spring, but are AOP related.



1) Joinpoint

A specific point in the execution of a program: before class initialization, after class initialization, before a class method is called, called
After the method throws an exception. A class or a piece of program code has certain points that have boundary properties
Specific points are called “join points”. Spring only supports join points for methods, that is, only in
Before method call,
The method call
after,
Method throws an exceptionAs well as
Before and after method callsThese program execution points are woven enhanced.

2) Pointcut

Each program class has multiple join points, such as a class with two methods, both of which are join points, i.e
Join points are things that exist objectively in program classes. AOP locates specific join points through “pointcuts.”

3) Advice

An enhancement is a piece of program code woven into the target class join point. In Spring, an enhancement is used to describe a program
In addition to the sequence code, there is another piece of information related to the join point, which is the orientation of the execution point. Combine the execution point azimuth information
And pointcut information, we can find specific join points.

The following types of notifications are defined in Spring:

  • Before advice – Perform advice Before the target is easily invoked
  • After Advice – Perform advice After the target method completes
  • After Returning Advice – Calls advice After successful execution of the target method
  • After Throwing Advice – After the target method throws an exception, the advice is executed
  • Around Advice – Custom logic can be executed both before and after the target method invocation

4) Target

Enhanced logical weaving into target classes. Without AOP, the target business class needs to implement all the logic itself while in AOP
The target business class implements only program logic that is not crosscutting logic, but performance monitoring and transaction management
Cutting logic can be dynamically woven to specific join points using AOP.

5) Introduction

Introductions are a special enhancement that adds attributes and methods to a class. This way, even if a business class does not have one
Implement an interface. With AOP’s introduction, we can dynamically add the implementation logic of the interface to that business class.
Let the business class be the implementation class for this interface.

6) Weaving

Weaving is the process of adding enhancements to a specific join point to a target class. AOP is like a loom, adding target classes
Strong or referrals are woven together seamlessly through the loom of AOP. There are three kinds of AOP, depending on the implementation technology
Way of weaving:
A. Compile-time weaving, which requires the use of a special Java compiler.
B. Class load-time weaving, which requires the use of a special class loader.
C, dynamic proxy weaving, adding enhanced subclassing methods for target classes at run time.
Spring uses dynamic proxy weaving, while AspectJ uses compile-time weaving and class-load weaving.

7) Proxy

When a class is woven into AOP for enhancement, it produces a result class, which is a proxy that blends the original class with the enhanced logic
Class. Depending on the proxy, a proxy class can be a class with the same interface as the original class or a child of the original class
Class, so we can call the proxy class in the same way we call the original class.

8) Aspect

A section consists of pointcuts and enhancements (introduction), which include both the definition of crosscutting logic and the definition of join points,
Spring AOP is the framework responsible for implementing the aspect, weaving the crosscutting logic defined by the aspect into that specified by the aspect
Join point.

Core interface class

Spring AOP as a whole can be viewed in two steps:
  1. Generate proxy objects from configuration files or annotations
  2. Executes the proxy object, which includes enhanced methods and proxied objects
Here’s a quick look at the core interface classes in Spring AOP

InvocationHandler

This interface is a parent interface that is implemented by most dynamic proxy interfaces, such as JdkDynamicAopProxy. Only one method is defined

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
Copy the code

AopProxy

Interface, also a parent interface, defines the proxy. There are only three implementation classes CglibAopProxy, JdkDynamicAopProxy, and ObjenesisCglibAopProxy, and only two methods are defined inside the interface

Object getProxy(a);
Object getProxy(ClassLoader classLoader);Copy the code

AopProxyFactory

Interface to generate a factory for the agent. The interface has only one method, which defines the method to create the proxy, implemented as follows:

public interface AopProxyFactory {
   AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException;
}Copy the code



In Spring AOP,
This interface has only one implementation class, DefaultAopProxyFactory. DefaultAopProxyFactory implements the createAopProxy method in the interface and defines when to use JdkDynamicAopProxy and when to use ObjenesisCglibAopProxy proxies. The source code is as follows:

public class DefaultAopProxyFactory implements AopProxyFactory.Serializable {
   @Override
   public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
      if(config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { Class<? > targetClass = config.getTargetClass();if (targetClass == null) {
            throw new AopConfigException("TargetSource cannot determine target class: " +
                  "Either an interface or a target is required for proxy creation.");
         }
         if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
            return new JdkDynamicAopProxy(config);
         }
         return new ObjenesisCglibAopProxy(config);
      }
      else {
         return newJdkDynamicAopProxy(config); }}private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) { Class<? >[] ifcs = config.getProxiedInterfaces();return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0))); }}Copy the code

By default, Spring AOP uses JDK dynamic proxies to implement AOP functions.
Use the JDK dynamic proxy when the proxy object is an interface.
Otherwise, use ObjenesisCglibAopProxy.