ASPECTJ is a tangent-oriented framework that extends the Java language. ASPECTJ defines the AOP syntax, so it has a dedicated compiler to generate class files that conform to the Java Byte encoding specification.
There are two main ways to use AOP in Springboot: annotation interception and Method rule interception, as shown in the following article.
First, create a simple springboot 2.03 project, add AOP dependencies
< Dependency > < groupId >org.springframework.boot</groupId> < Artifactid>spring-boot-starter-aop</artifactid> </ Dependency >
This dependency already contains ASPECTJ dependent packages.
Ii. annotations for the preparation of interception rules
Package com.cenobitor.aop.annotation; import java.lang.annotation.*; @Target (Elementtype.method) @Retention (retentionpolicy.runtime) @ Documented public @Interface Action { String name ();}
Note: meta-annotations refer to annotations, including @retention @Target @Document @Inherited four types.
[Email protected]: Define retention policies for annotations
- @Retention (Retentionpolicy.source)//annotations only exist in the source code and are not included in the class bytecode file
- @Retention (Retentionpolicy.class)//default retention policy, annotations exist in the CLASS bytecode file, but are not available at run time.
- @Retention (retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at run time
The first thing to be clear is the life cycle length SOURCE < CLASS < RUNTIME, so the former can function where the latter must also be able to function. In general, if you need to dynamically obtain the annotation information at runtime, it can only be annotated with runtime, if you want to do some preprocessing at compile time, such as generating some auxiliary code (such as Butterknife), use class annotation; If you just do some checking, like @ Override and @SuppressWarnings, you can select the SOURCE annotation.
[Email protected]: Define the purpose of the annotation
The source code is:
@Documented @Retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @Interface Target { elementtype[] value (); }
- @Target (Elementtype.type)//interfaces, classes, enumerations, annotations
- @Target (Elementtype.field)//fields, constants for enumerations
- @Target (Elementtype.method)//method
- @Target (Elementtype.parameter)//Method parameters
- @Target (Elementtype.constructor)//constructor
- @Target (elementtype.local_variable)//local variable
- @Target (Elementtype.annotation_type)//annotations
- @Target (Elementtype.package)///Package
[Email protected]: Note that the note will be included in the Javadoc
[Email protected]: The description subclass can inherit the annotation from the parent class
Third, write the interception class using annotations
Package Com.cenobitor.aop.service; Import com.cenobitor.aop.annotation.Action; Import Org.springframework.stereotype.Service; @Service Public class Demoannotationservice { = "Annotated block add Operation") publicvoid Add () {}}
Iv. writing a method of using rules to intercept classes
Package Com.cenobitor.aop.service; Import Org.springframework.stereotype.Service; @Service Public class Demomethodservice { publicvoid Add () {}}
V. Preparation of Facets
PackageCom.cenobitor.aop.aspect;Importcom.cenobitor.aop.annotation.Action;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.annotation.After;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;ImportOrg.aspectj.lang.annotation.Pointcut;Importorg.aspectj.lang.reflect.MethodSignature;Importorg.springframework.stereotype.Component;ImportJava.lang.reflect.Method, @Aspect @component Public classlogaspect {@Pointcut ("@annotation (com.cenobitor.aop.annotation.Action)") Public voidannotationpoincut () {} @After ("Annotationpoincut ()") Public voidAfter (Joinpoint joinpoint) {methodsignature signature=(methodsignature) joinpoint.getsignature (); Method Method=Signature.getmethod (); Action Action= Method.getannotation (Action.class); System.out.println ("Annotated interception" +action.name ()); } @Before ("Execution (* com.cenobitor.aop.service.demomethodservice.* (..))") Public voidbefore (Joinpoint joinpoint) {methodsignature signature=(methodsignature) joinpoint.getsignature (); Method Method=Signature.getmethod (); System.out.println ("Method rule-blocking," +method.getname ()); }}
Description of the AOP annotations:
- @Aspect define facets: facets are composed of tangency points and enhancements (which can contain multiple pointcuts and multiple enhancements), including both the definition of crosscutting logic and the definition of a connection point, SPRINGAOP is the framework responsible for implementing facets, It weaves the crosscutting logic defined by the slice into the link point specified by the slice.
- @Pointcut Defining a pointcut: A tangent is a collection of connection points. AOP locates specific connection points through the tangent point. The concept of database queries to understand the relationship between pointcuts and connection points is more appropriate: connection points are equivalent to records in a database, and tangency is equivalent to a query condition.
- @Before : For enhanced processing before the target method is called, @Before only need to specify the pointcut expression.
- @AfterReturning: After the target method has completed normal completion, @AfterReturning, in addition to specifying a pointcut expression, you can also specify a return value parameter name returning, which represents the return value of the target method.
- @Afterthrowing: Used primarily to handle unhandled exceptions in the program, @AfterThrowing in addition to specifying pointcut expressions, you can also specify a throwing return parameter name to access the exception object thrown in the target method through the parameter name.
- @After: After the target method is completed, the enhancement is done, regardless of the target method. @After can specify a pointcut expression.
- @Around: surround notification, before and after the target method is done to enhance processing, surround notification is the most important type of notification, such as transactions, logs, etc. are surrounded by notifications, note that the core of programming is a proceedingjoinpoint.
Vi.. Operation
public class main { public static void main (string[] args) {annotationconfigapplicationcontext context = new annotationconfigapplicationcontext (aopapplication. = Context.getbean (Demoannotationservice. Class = Context.getbean (Demomethodservice. Class
Aopapplication.class is the startup class for this project.
The results of the operation are as follows:
Annotated block add operation for annotation blocking
Method rule-blocking, add
Note: Excerpt from the "Java EE Development Subversion springboot actual combat", according to Springboot 2.0.3 make some changes, omit some configuration items.
SPRINGBOOT--AOP annotation Interception and method rule interception