Briefly talk about AOP

Thinking about a technology is basically based on the order in which a new knowledge is explained in the textbook in school.

First: What is AOP?

Second: Why introduce AOP? Or what problem does AOP solve?

Third: How to implement AOP simply?

Fourth: How does AOP help us think about and help us write code?

Now, let's start to explain AOP,

1 What is AOP?

1.1 Concept introduction

Spring's mission is to simplify Java code development, and aop, as a submodule of Spring, is no exception.

AOP is the abbreviation of Aspect Oriented Programming. It is a programming idea like OOP (Object Oriented Programming) and is a supplement to OOP.

How to understand what AOP is? Taking logs as an example, many management systems, such as order systems, push systems, etc., need to record logs. If log-related code is written in every business logic, there will be too much duplication.

Simply encapsulate the relevant logic code of the log in a unified manner. Then embed it where needed. AOP mainly does embedding. Look at the picture below.

84f74238-de3b-4dd9-86c3-2cd4e38f2395.png

Add order Edit order Cancel order Push order log transaction

AOP aims to separate crosscutting concerns from the business main logic and achieve separation of concerns to improve the modularity of the program (and the business module only needs to focus on business logic and does not need to pay attention to logs, security, transactions, etc. general logic)

Some terms are mentioned above, such as aspects, concerns, crosscutting, etc. These terms are explained below.

1.2 Definition of terms

  1. AOP has its own set of terminology, and we must understand these jargons to better understand AOP. In order to facilitate everyone's understanding, the following will use the class representative to collect homework as an example.

    1.Advice

    定义了在收作业前后需要做的事。常见的通知类型有:before、after、after-returning、around 等。
    

    2.JoinPoint

    连接点指程序运行时允许插入切面的一个点,可以是一个函数、一个包路径、一个类、或者抛出的异常。有点类似于可以收作业的时间点。
    

    3. PointCut

    切点用于定义切面的位置,也就是捕获哪些连接点的调用然后执行"通知"的操作(什么地点)。
    

    4.Aspect

    切面是切点和通知的聚合,定义了在哪一个切点做什么通知。
    

    5. Target

    指被切面织入的对象。
    

    6. Introduction

    引入允许我们向现有的类添加新方法或属性。
    

    7. Weaving

    织入是把切面应用到切点对应的连接点的过程。切面在指定连接点被织入到目标对象中。
    

The specific relationship represented graphically is:
2.png

2 Why introduce AOP?

The above is the definition. The reason for introducing AOP is definitely to solve some pain points in current development:

(1) In the current development, each other is modular development, and the use of AOP can effectively realize the modular idea.

(2) Separate the auxiliary logic (logging, security, monitoring, etc.) from the business main logic and develop it simultaneously.

AOP is an idea that separates some business logic and then combines it according to the main business logic to finally achieve the desired functional logic.

3 How to implement AOP simply?

3.1 AOP programming ideas

This picture is a simple idea.

3.png

Clarify the requirements, create the aspect class, define the pointcut, and define the notification. Thinking: What should be done in which method and when (before the method? After the method? Or around?), that is, which connection point is selected for execution. The notification is near the pointcut (before? after? ) What to do

The following defines a simple case of log to implement AOP. The original annotation method is used:

3.2 Case implementation of AOP

3.2.1 Clarify requirements

Adding @FddLog to a method will automatically output corresponding information before and after executing this method. The following is an example of putting an elephant in the refrigerator:

3.2.2 Basic interface and implementation
public interface ElephentToRe{
  public void toRe();
}
The implementation class is as follows:
public class ElephentToReImpl implements ElephentToRe{
  public void toRe() {
    System.out.println("把大象放冰箱");
  }
}
3.2.3 Defining aspects and advice
    public class ElephentToReHelper {
        public void beforeElephentToRe() {
            System.out.println("把冰箱门打开");
        }

        public void afterElephentToRe() {
            System.out.println("把冰箱门关上");
        }
    }
Just configure it
<?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:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
  <!-- 定义通知内容,也就是切入点执行前后需要做的事情 -->
  <bean id="elephentToReHelper" class="com.fdd.bean.ElephentToReHelper"></bean>
  <!-- 定义被代理者 -->
  <bean id="elephentToReImpl" class="com.fdd.bean.ElephentToReImpl"></bean>
  <aop:config>
    <aop:aspect ref="elephentToReHelper">
      <aop:before method="beforeElephentToRe" pointcut="execution(* *.toRe(..))" />
      <aop:after method="afterElephentToRe" pointcut="execution(* *.toRe(..))" />
    </aop:aspect>
  </aop:config>
</beans>
3.2.4 Test to see the effect
    public class Test {
        public static void main(String[] args) {
            @SuppressWarnings("resource") ApplicationContext appCtx = new FileSystemXmlApplicationContext("application.xml");
            ElephentToRe elephentToReImpl = (ElephentToRe) appCtx.getBean("elephentToReImpl");
            elephentToReImpl.toRe();
        }
    }

The above method is accomplished through pure POJO aspects . The implementation is also relatively simple.

4 My views on AOP thinking

The emergence of any new technology is to solve certain pain points in current development. For aop, it mainly abstractly encapsulates some functional codes and separates them from the main business logic code. Just weave in where needed.

My opinion is

(1) When developing code, you can encapsulate some common and commonly used functional codes and try to achieve dynamic configuration. Different functional modules only need to be woven in.

(2) Define the business logic template. For example, if you want to solve a certain business function and the pages are similar, you can combine them according to the basic framework, and then use the configuration platform for controllable configuration.

Guess you like

Origin blog.csdn.net/YOUYOU0710/article/details/125419866