SpringAOP-Clarify the point of contact and notify the point of link

Point of cut - the point of cut defines "where", and the point of cut will match one or more connection points to be woven into the notification

Notification-Aspects also need to complete certain work. In AOP terminology, aspect work is called notification

Aspects-Aspects are a collection of notices and points of contact, which together define all the functions of the aspect, when and where to complete their functions

Link point - A link point is a point where an aspect can be inserted during the execution of an application, from the perspective of the application.

Why do you need to know these four points, because the next time you write code, you will write based on these four points.

Pointcut corresponds to the annotation @pointcut, and you can define which methods or such methods or types need to be intercepted/cut.

Notify, apply our @Before, @After@AfterThorwing@AfterReturn, and notify when the method is called.

Aspect, corresponding to @Aspect used on the class, where we can define our point of contact and notify

Link point, apply the joinpoint object to grab all the data, parameters, method name, etc. inside our cut method.

Use spring AOP's three axes:

The first step is to introduce the jar package,

         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>

The second step:

In the spring configuration file, spring.xml is placed under the resource, and package scan and aspectj are added to open the sop annotation

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan base-package="com.allen.trainning.spring.aop"/>
<aop:aspectj-autoproxy/>

</beans>

third step,

Create a new aspect class yourself and add annotations to the class:

@Aspect

@Component

package com.allen.trainning.spring.aop.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogAspect {

    @Before("execution(* com.allen.trainning.spring.aop.service.impl.*.*(..))")
    public void before(){
        System.out.println("someone get is find one xiaojieji!");
    }
}

Write your own class for testing, namely the pointcut class:

interface:

public interface GetXiaoJieJieDao {
    public  XiaoJieJie getByAge(int age);
    public  XiaoJieJie getByName(String name);
}

achieve: 

@Repository
public  class GetXiaoJieJieDaoImpl implements GetXiaoJieJieDao {

    public  XiaoJieJie getByAge(int age){
        return new XiaoJieJie("Lucy",age);
    }
    public  XiaoJieJie getByName(String name){
        return new XiaoJieJie(name,18);
    }
}

Entity class:

public class XiaoJieJie {
    public XiaoJieJie(String name, Integer age){
        this.name=name;
        this.age=age;
    }
    private String name;
    private Integer age;
    public void play(){
        System.out.println( "my Master, I am "+name+ " ,"+age+ " years old, will you play with me");
    }
}

Own test class:

public class TestAop {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext xml = new ClassPathXmlApplicationContext("spring.xml");
        GetXiaoJieJieDao service = xml.getBean(GetXiaoJieJieDao.class);
        XiaoJieJie girl = service.getByName("ziyi");
        girl.play();
    }
}

Results of the:

someone get is find one xiaojieji!
my Master, I am ziyi ,18 years old, will you play with me

Process finished with exit code 0

The above code is implemented, no matter who calls the GetXiaoJieJie class, it will be captured, and a sentence will be printed before the method of the class is executed.

Only the basic ideas of spring aop are introduced here. The next article will introduce more and more detailed knowledge points in spring aop with examples.

Guess you like

Origin blog.csdn.net/pengweismile/article/details/109788298