AOP - aspect oriented programming

1. What is AOP?

AOP is aspect oriented programming, which extends the method without modifying the source code!!! It solves the coupling of business
When we usually write code, we will implement various methods in the control layer, but too much code writing will lead to code redundancy. How to solve this problem?
Then you need to use the AOP mechanism in the Spring framework for aspect oriented programming
In the control layer, we only need to write a small amount of code that must be written, and the rest are placed in the aspect class, which can solve the problem of code redundancy

1.1 principle of section programming

Principle: use dynamic proxy to create objects
Dynamic agent: JDK dynamic agent Cglib dynamic agent

package com.jt.demo1.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/*The current class is the tool API, which is used to get the proxy object*/
public class JDKProxy {
     * Parameter Description:
     * ClassLoader loader,  Class loader: load the class into the java running mechanism
     * Class<?>[] interfaces, The interface array of the agent can be implemented in java
     * InvocationHandler h  Writes the contents of the proxy object extension to the processor
     * @param target
     * @return
    public static Object getProxy(Object target){
        //1. Get the class loader of the target object
        ClassLoader loader = target.getClass().getClassLoader();
        //2. Get interface array
        Class<?>[] interfaces = target.getClass().getInterfaces();
        //Get processor object
        InvocationHandler h = invocationHandler(target);
        // Create proxy object
       return Proxy.newProxyInstance(loader,interfaces,h);
    //Get InvocationHandler object
    public static InvocationHandler invocationHandler(Object target){
        //When the proxy object performs business operations, the business is extended through InvocationHandler
        return new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("Transaction start");
                //Call, let the target method execute target: target object!!! Others are fixed writing!!!
                Object result = method.invoke(target, args);
                System.out.println("Transaction commit");
                return result;


1.2 step analysis:

  1. Gets the classloader of the target object
  2. Get interface array
  3. Post processor object
  4. Create proxy object
    The proxy object is created by using the structure of internal class, then write the code to be executed, and then call the target object

1.3 simplify the source code with Spring

package com.demo1.aop;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component//Leave this class to container management
@Aspect//Section class
public class TxAspect {
    //Pointcut expression
    //@Afterthrowing ("execution (return value type package name. Class name. Method name (parameter list))")
    @Pointcut("execution(* com.demo1.service..*.*(..))")
    public void pointCut(){

    @AfterThrowing(value = "pointCut()",throwing = "exception")
    public void afterThrowing(Exception exception){
        String message = exception.getMessage();
        System.out.println("AOP The exception message is:"+message);


1.4 code analysis

Directly create a facet class in the Spring container and hand it over to the Spring container for management,
Identify that this class is a facet class, and then write the pointcut expression

@Pointcut("execution(* com.demo1.service..*.*(..))")

The above code represents the list of all parameters of all classes and methods in all files of com package demo1 package service package
In addition, there are five notifications in the aspect class

  1. Pre notification: @ Before: execute Before code execution
  2. Post notification: @ AfterReturning: execute after code execution and before final notification
  3. Exception notification: @ AfterThrowing: it will not be executed until an exception occurs
  4. Final notice: @ After: execute After code execution
  5. Surround notification: @ Around: execute before and after all code (the most powerful)
    The implementation effect is as follows

Keywords: Java JavaEE

Added by edcaru on Mon, 28 Feb 2022 13:45:45 +0200