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(); } }
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 { }
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"; } }
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"); } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In 2023, AI technology has become a hot topic and has a huge impact on various industries, especially in the programming field. People are increasingly aware of the importance of AI technology, and the Spring community is no exception. With the continuous advancement of GenAI (General Artificial Intelligence) technology, it has become crucial and urgent to simplify the creation of applications with AI functions. Against this background, "SpringAI" emerged, aiming to simplify the process of developing AI functional applications, making it simple and intuitive and avoiding unnecessary complexity. Through "SpringAI", developers can more easily build applications with AI functions, making them easier to use and operate.

As an industry leader, Spring+AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into the application examples of Spring+AI in various fields. Each case will show how Spring+AI meets specific needs, achieves goals, and extends these LESSONSLEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring+AI more deeply. The Spring framework has a history of more than 20 years in the field of software development, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring

How to implement spring programmatic transactions: 1. Use TransactionTemplate; 2. Use TransactionCallback and TransactionCallbackWithoutResult; 3. Use Transactional annotations; 4. Use TransactionTemplate in combination with @Transactional; 5. Customize the transaction manager.

Annotations in the JUnit framework are used to declare and configure test methods. The main annotations include: @Test (declaration of test methods), @Before (method run before the test method is executed), @After (method run after the test method is executed), @ BeforeClass (method that runs before all test methods are executed), @AfterClass (method that runs after all test methods are executed), these annotations help organize and simplify the test code, and improve the reliability of the test code by providing clear intentions and configurations. Readability and maintainability.

Introduction: PHPDoc is a comment standard for PHP code that produces easy-to-understand and informative documentation. By using specific comment tags, PHPDoc allows developers to provide important details about functions, classes, methods, and other code elements. This advanced guide takes an in-depth look at PHPDoc, demonstrating its capabilities and providing effective documentation strategies. Syntax and tags: PHPDoc comments start with double slashes (//) or multi-line comments (/**/). Here are some common annotation tags: @param: Defines the parameters of a function or method. @return: Specifies the return value of the function or method. @throws: Describes exceptions that may be thrown by a function or method. @var: defines the attributes or instances of the class

How to set the transaction isolation level in Spring: 1. Use the @Transactional annotation; 2. Set it in the Spring configuration file; 3. Use PlatformTransactionManager; 4. Set it in the Java configuration class. Detailed introduction: 1. Use the @Transactional annotation, add the @Transactional annotation to the class or method that requires transaction management, and set the isolation level in the attribute; 2. In the Spring configuration file, etc.

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

Detailed introduction to the usage of MyBatis annotation dynamic SQL MyBatis is a persistence layer framework that provides us with convenient persistence operations. In actual development, it is usually necessary to dynamically generate SQL statements based on business needs to achieve flexible data operations. MyBatis annotation dynamic SQL is designed to meet this demand.
