Comprehensive analysis of Spring framework AOP section oriented programming principle

  • 2021-11-14 05:32:50
  • OfStack

Directory 1. What are the facet-oriented advantages of AOPAOP the dependencies that AOP needs to add 2. Briefly describe how AOP works. Summary of dynamic creation: 3. Use Spring to create AOP test classes Spring. xml

1. What is AOP

AOP: Aspect Oriented Programming program to cut.

Advantages of AOP face-to-face

Reduce the coupling between modules. Make the system easier to expand. Better code replication. Business codes are more centralized and not scattered, which is convenient for unified management. Business code is more concise and does not involve the influence of other codes.

AOP is a supplement to programming to objects. During operation, the code is dynamically cut to the specified method of classes. The programming idea at the specified position is to program to cut. The same position of different methods is abstracted into a cut object, and the programming of this cut object is AOP.

Dependencies that AOP needs to add


<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.3.9</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.3.9</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.9</version>
    </dependency>

2. Briefly describe the working principle of AOP


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class MyInvocationHandler implements InvocationHandler {
    private Object object=null;
    public Object bind(Object object){
        this.object=object;
        return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(method.getName()+" Parameters of the method "+ Arrays.toString(args));
        Object result=method.invoke(this.object,args);
        System.out.println(method.getName()+" The result is that "+result);
        return result;
    }
}

The above is the method of dynamically creating AOP, which is first returned through bind

Proxy. newProxyInstance (object. getClass (). getClassLoader (), object. getClass. getInterfaces (), this) Returns the proxy object

Then, the method of reflection invoke is used to realize section-oriented programming

Get the method name with method. getName ()

Getting properties with arg

Dynamically created summary:

The above is the process of realizing AOP through dynamic proxy, which is complicated and difficult to understand. The Spring framework encapsulates AOP, so that the Spring framework can realize AOP with the idea of object

3. Create an AOP using Spring


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Aspect
@Component
public class LoggerAspect {
    //@before The specific position and timing of the implementation of the adaptive method are indicated 
    @Before("execution(public int Util.impl.Calimpl.*(..))")
    public void before(JoinPoint joinPoint){
        // Get the method name 
        String name=joinPoint.getSignature().getName();
        // Get parameters 
        String args= Arrays.toString(joinPoint.getArgs());
        System.out.println(name+" The parameters of the method are "+args);
    }
    @After("execution(public int Util.impl.Calimpl.*(..))")
    public  void after(JoinPoint joinPoint){
        String name=joinPoint.getSignature().getName();
        System.out.println(name+" Complete execution ");
    }
    @AfterReturning(value = "execution(public int Util.impl.Calimpl.*(..))",returning = "result")
    public void returning(JoinPoint joinPoint,Object result){
        System.out.println(" The result is :"+result);
    }
}

Annotation @ Aspect refers to aspect-oriented programming

@ Component refers to the Ioc container management

Get the method name through Joinpoint. getSignature (). getname ()

Then implement the log information needed to run before calling this method

Test class


import Util.Cal;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test2 {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-aop.xml");
        Cal proxy = (Cal) applicationContext.getBean("calimpl");
        proxy.add(1,1);
    }
}

Or read Spring through Application. xml, then set the default lowercase name of getBean, and then call the method

Spring.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
    <!-- ⾃ Dynamic scanning  -->
    <context:component-scan base-package="Util">
    </context:component-scan>
    <!--  Yes Aspect Annotation ⽣ Effect, for ⽬ Standard class ⾃ Move ⽣ Proxy object  -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

The above is the Spring framework AOP section-oriented programming comprehensive analysis of the details, more about the Spring framework AOP section-oriented information please pay attention to other related articles on this site!


Related articles: