Home > Java > JavaBase > Detailed explanation of Spring AOP annotations

Detailed explanation of Spring AOP annotations

Guanhui
Release: 2020-06-28 18:00:16
forward
3350 people have browsed it

Detailed explanation of Spring AOP annotations

Aspect: Aspect

Aspect = pointcut notification. In old spring versions, XML configuration is usually used, but now a class is usually annotated with @Aspect. Aspects are responsible for weaving cross-cutting logic (advice) into designated join points.

Target object: Target

The object to be enhanced.

Join Point: JoinPoint

The program execution point that can be intercepted is the method in the class in spring.

Entry point: PointCut

The method that needs to be intercepted is the method that specifically implements the cross-cutting logic. Pointcut rules are described in spring through the AspectJ pointcut expression language.

The difference between entry points and connection points: connection points are all points that can be "cut"; entry points are the points that really want to be cut.

Advice: Advice

Cross-cutting logic for entry points, including different types of advice such as "around", "before" and "after".

The action point of the advice is as named:

  • before: executed before the entry point

  • after: after Execute after the entry point

  • around: intercept the method at the entry point, before and after customization, more flexible

There are also some exception handling notifications, Here are no examples one by one

Weaving

The process of connecting aspects and target objects to create proxy objects. Dynamic proxy is used in spring. If the target object has an interface, use jdk dynamic proxy; otherwise, use cglib dynamic proxy.

Having said so many concepts, looking at the code implementation may help readers understand it more deeply. Here is a simple AOP-Demo that enhances the method through annotations.
First is the aspect class:

package com.example.demo.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author Fcb
 * @date 2020/6/20
 * @description 切面类=切入点+通知
 */
@Aspect
@Component
public class LogAspect {

  //这个方法定义了切入点
  @Pointcut("@annotation(com.example.demo.aop.anno.MyLog)")
  public void pointCut() {}

  //这个方法定义了具体的通知
  @After("pointCut()")
  public void recordRequestParam(JoinPoint joinPoint) {
    for (Object s : joinPoint.getArgs()) {
      //打印所有参数,实际中就是记录日志了
      System.out.println("after advice : " + s);
    }
  }

  //这个方法定义了具体的通知
  @Before("pointCut()")
  public void startRecord(JoinPoint joinPoint) {
    for (Object s : joinPoint.getArgs()) {
      //打印所有参数
      System.out.println("before advice : " + s);
    }
  }

  //这个方法定义了具体的通知
  @Around("pointCut()")
  public Object aroundRecord(ProceedingJoinPoint pjp) throws Throwable {
    for (Object s : pjp.getArgs()) {
      //打印所有参数
      System.out.println("around advice : " + s);
    }
    return pjp.proceed();
  }
}
Copy after login

Annotation:

package com.example.demo.aop.anno;
import java.lang.annotation.*;
/**
 * @author Fcb
 * @date 2020/6/20
 * @description
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyLog {
}
Copy after login

Target class:

package com.example.demo.aop.target;

import com.example.demo.aop.anno.MyLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Fcb
 * @date 2020/6/20
 * @description
 */
@RestController
public class MockController {

  @RequestMapping("/hello")
  @MyLog
  public String helloAop(@RequestParam String key) {
    System.out.println("do something...");
    return "hello world";
  }

}
Copy after login

Finally is the test class:

package com.example.demo.aop.target;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
 * @author Fcb
 * @date 2020/6/20
 * @description
 */
@SpringBootTest
class MockControllerTest {
  @Autowired
  MockController mockController;

  @Test
  void helloAop() {
    mockController.helloAop("aop");
  }
}
Copy after login

Console result:

around advice : aop
before advice : aop
do something...
after advice : aop

Recommended tutorial: "JavaTutorial

The above is the detailed content of Detailed explanation of Spring AOP annotations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template