java custom aspect enhancement
1. Customize an annotation
@Target(value = { ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @interface CacheNullify { boolean value = true; }
2. Implement enhanced logic
@Slf4j @Aspect @Component public class CacheNullifyHandler { @AfterReturning("@annotation(com.xxx.CacheNullify)") public void afterReturning() { log.info("走你"); CacheServiceUtil.delUserInfoCache(); } }
@annotation
: What follows is the annotation you want to enhance (the annotated method), you can directly write the annotation name, But in order to prevent duplicate names, write the full path as much as possible##@AfterReturning
: Triggered after the method is executed and returns successfully
@After
: Triggered after the method is completed (including execution completion, or exception thrown)
3. Use
Add the @CacheNullify annotation to the method that needs to be enhancedUse of aspects and custom annotationsFirst understand the basic concepts and usage of aspects and custom annotations Introduction to AOPAOP is the abbreviation of Aspect Oriented Programming, which means: Aspect-oriented programming, a technology that achieves unified maintenance of program functions through pre-compilation and dynamic agents during runtime. AOP is the continuation of OOP, a hot spot in software development, an important content in the Spring framework, and a derivative paradigm of functional programming. AOP can be used to isolate various parts of the business logic, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and improving the efficiency of development. AOP definitionAspect (aspect)
Joint point
Pointcut (pointcut)
Advice (enhancement/notification)
Introduction
Target (target object)
Weaving
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation {
String value() default"";
}
Analysis of the above annotation composition
- ##public: modifier
- @interface: Keyword
- MyAnnotaion: Annotation name
- #value: Annotation type element
Meta-annotation
But careful analysis also includes four annotations starting with @@Target, @Retention, @Inherited, @Documented ) These are the four underlying meta-annotations, annotations that annotate other annotations
@Target: used to describe the scope of use of the annotation, where the annotation can be usedTarget type Description
- ##ElementType.TYPE
- Applies to classes, interfaces (including annotation types), and enumerations
- Applies to properties (including constants in enumerations)
- Applies to methods
- Applied to the formal parameters of the method
- Applied to the constructor
- Applies to local variables
##ElementType.ANNOTATION_TYPE Applies to annotation types -
ElementType.PACKAGE Applies to package
Note: For example, @Target(ElementType.METHOD), the annotation of the flag is used on the method, but if we use this annotation flag on the class, an error will be reported
@Retention: Indicates the life cycle of the annotation
Life cycle type Description
RetentionPolicy.SOURCE
Discarded during compilation, not included In the class fileRetentionPolicy.CLASS
is discarded when the JVM is loaded and included in the class file. The default value isRetentionPolicy.RUNTIME
Loaded by the JVM, included in the class file, and can be obtained at runtime
@Inherited: It is a mark annotation, @Inherited elaborates An annotated type is inherited. If an annotation type modified with @Inherited is used for a class, this annotation will be used for subclasses of the class.
@Documented: Indicates that the element marked by this annotation can be documented by Javadoc or similar tools
The above is the detailed content of What is the method for java custom aspect enhancement?. For more information, please follow other related articles on the PHP Chinese website!

在使用Netty开发WebSocket服务器时,可能会遇到浏览器在尝试连接时未能正确处理服务器返回的401状态码的情况。 �...

Java compilation failed: Running window javac command cannot generate class file Many Java beginners will encounter this problem during the learning process: running window...

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

Packages and Directories in Java: The logic behind compiler errors In Java development, you often encounter problems with packages and directories. This article will explore Java in depth...

Leetcode ...

JWT and Session Choice: Tradeoffs under Dynamic Permission Changes Many Beginners on JWT and Session...

How to correctly configure apple-app-site-association file in Baota nginx? Recently, the company's iOS department sent an apple-app-site-association file and...

How to understand the classification and implementation methods of two consistency consensus algorithms? At the protocol level, there has been no new members in the selection of consistency algorithms for many years. ...


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.