In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the annotation method of SpringAOP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the annotation method of SpringAOP"?
1. An overview of SpringAOP is not a technology but a design idea, called aspect-oriented programming, which uses crosscutting technology to analyze the interior of an object, extracts and encapsulates the logic of common calls between businesses into a reusable module, which is named Aspect, which reduces repetitive code in the system, reduces the coupling between modules, and can be used for logging, authority authentication, transaction management, etc. 2.
AOP is implemented based on proxies, and Java defaults to JDK dynamic proxies, but JDK dynamic proxies can only proxy interfaces, not proxy classes. As a result, springAOP dynamically switches between JDK dynamic agents and CGLIB agents.
2.1 there are two kinds of agents: dynamic agent and static agent.
Dynamic proxy: temporary generation of AOP proxy classes in memory at runtime with the help of JDk dynamic agents, CGLIB, etc., also known as runtime enhancements
Static proxy: refers to compiling using the commands of the AOP framework so that AOP proxy classes can be generated during the compilation phase and is also known as a compilation enhancement
2.2 Choice of agency mode
The proxy target object implements the interface. JDK dynamic proxy is used by default, or CGLIB can be forced to be used.
The proxy target object does not implement the interface and must use CGLIB
Spring automatically converts JDK dynamic agents and CGLIB agents directly.
3. Noun interpretation
Section (Aspect): the section is the combination of notification and pointcut, which together define the whole content of the section.
Notification (Advice): defines when the section is used. Notifications are divided into the following types
Pre-notification (Before): calls the notification function before the target method is called
Post notification (After): the notification is called after the execution of the target method is completed, and the output of the method is not concerned at this time
Return notification (After-returning): call notification after the target method executes successfully
Exception notification (After-throwing): call notification after the target method throws an exception
Surround notification (Around): performs custom behavior before and after the notified method call
PointCut: defines where to apply the join point, usually explicitly used on class and method names, or using matching or annotated methods
Join point (JoinPoin): a join point is a point that can be inserted into an aspect during the operation of an application, and the specific insertion method varies according to the notification.
Target object (Target): an object that needs to be handled, defined by a tangent point
Weaving: the process of applying a section to a target object and creating a new proxy object
Compile-time weaving
Class loading time weaving into
Run-time weaving
4. Concrete realization
The blogger takes log collection as an example to demonstrate the implementation of annotation.
4.1 create a new springboot project and map it into AOP relying on org.springframework.boot spring-boot-starter-aop4.2 to write annotation class import java.lang.annotation.*;/** * log annotations * * @ author chilx * / @ Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) @ Documentedpublic @ interface LogAnnotation {/ * * method name * / String title () default "" / * * Operation type {@ link LogTypeConsts} * / String type () default ""; / * Operation description * / String remark () default "";} 4.3 write aspect class import com.chilx.aop.annotation.LogAnnotation;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component / * * @ author chilx * @ date 2021-3-31 * * / @ Aspect@Componentpublic class LogAnnotationAspect {/ * pointcut * / @ Pointcut ("@ annotation (com.chilx.aop.annotation.LogAnnotation)") public void logPointCut () {} / * pre-notification: call before method execution * Note: you can use @ Annotation (parameter name) to configure value: Use our custom annotation * remember to have the same parameter names before and after * / @ Before (value = "logPointCut () & & @ annotation (logInfo)" ArgNames = "point,logInfo") public void before (JoinPoint point, LogAnnotation logInfo) {/ / Parameter Object [] args = point.getArgs () System.out.println (); System.out.println ("pre-notice" + "-->" + logInfo.title () + "- -" + logInfo.remark ()) } / * * Post Notification: it is called after the method is executed. If an exception occurs in the method, * / @ After (value = "logPointCut () & & @ annotation (logInfo)", argNames = "point,logInfo") public void after (JoinPoint point, LogAnnotation logInfo) {System.out.println ("Post Notification" +-> "+ logInfo.title () +--" + logInfo.remark ()) is not executed. } / * returned: it is called after the method is executed. If an exception occurs in the method, the * / @ AfterReturning (value = "logPointCut () & & @ annotation (logInfo)", argNames = "point,logInfo") public void afterReturning (JoinPoint point, LogAnnotation logInfo) {System.out.println ("post notification" +-> "+ logInfo.title () +--" + logInfo.remark ()) is not executed. } / * surround notification: * / @ Around (value = "logPointCut () & & @ annotation (logInfo)", argNames = "point,logInfo") public void around (ProceedingJoinPoint point, LogAnnotation logInfo) throws Throwable {System.out.println ("before method execution"); Object proceed = point.proceed (point.getArgs ()); System.out.println ("after method execution") System.out.println ("surround notification" + "-->" + logInfo.title () + "- -" + logInfo.remark ()) } / * * exception notification: execute * / @ AfterThrowing when the method throws an exception (value = "logPointCut () & & @ annotation (logInfo)", argNames = "point,logInfo,e", throwing = "e") public void afterThrowing (JoinPoint point, LogAnnotation logInfo, Exception e) {System.out.println ("exception notification"-- > "+ logInfo.title () +--" + logInfo.remark ()); System.out.println (e.getMessage ()) }} 4.4 considerations
1. For surround notification, the object is ProceedingJoinPoint.
2. When using methods similar to the following, remember that the names of the bold parts should be exactly the same.
@ Before (value = "logPointCut () & & @ annotation (logInfo)", argNames = "point,logInfo")
Public void before (JoinPoint point, LogAnnotation logInfo) {}
At this point, I believe that you have a deeper understanding of "what is the annotation method of SpringAOP?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.