In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how the facet programming AOP in Springboot is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Springboot section programming AOP
AspectJ is an aspect-oriented framework that extends the Java language. AspectJ defines the AOP syntax. It and the OOP we usually come into contact with are different ideas of programming, OOP, that is, "object-oriented programming". It advocates modularization and objectification of functions, while the idea of AOP is not quite the same. It advocates a unified treatment of the same kind of problems. Of course, in the actual programming process, it is impossible to program simply according to the ideas of AOP or OOP. In many cases, it may be mixed with multiple programming ideas. There is no need for us to worry about which idea to use. It is the right way to take the best of a hundred families.
AspectJ is actually a practice of AOP programming ideas, of course, in addition to AspectJ, there are many other AOP implementations, such as ASMDex, but the best and most convenient is still AspectJ.
SpringAOP includes AspectJ because SpringAOP uses the syntax of AspectJ, but the underlying technology uses Spring's own.
Spring Aop uses dynamic weaving, while Aspectj uses static weaving.
Static weaving: refers to weaving at compile time, that is, the compiled class file, the bytecode has been woven into.
Dynamic weaving is divided into static and dynamic, static means that the weaving process is executed only on the first call; dynamic refers to how to operate according to the intermediate state of the dynamic running of the code, which is executed every time Target is called. If you have some unclear students, you can fill in the basic agency knowledge by yourself.
I. terms related to AOP
Pointcut pointcut
Usually a pointcut will select some execution points in the program that we are interested in, or a collection of execution points in the program. Is an expression based on a regular expression.
JoinPoint connection point
Through a specific execution point in the set selected by pointcut, we call it JoinPoint.
Target target class
An object that is crosscut by aspectj. What we call joinPoint is a line of Target, such as the place where the method starts to execute, the code that the method class calls some other method.
Advice Notification
The operation and logic to be performed on the selected JoinPoint. Typical Advice types are before, after, and around, which represent code executed before, after, and completely replacing the execution of the target method, respectively. @ Before pre-notification target method pre-execution @ After post notification target method execution, regardless of whether there is an exception @ AfterReturning post notification target method normal execution ends, you can know the return value of the target method @ AfterThrowing post notification target method execution exception, you can access the exception object Print. You can also specify that @ Around surround notification is executed only when an exception occurs, including the above four types of notification, example 2.1, introduction of jar package org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-aop
After you have finished introducing the AOP dependency package, you do not need to do any other configuration. In the default configuration attribute of AOP, the spring.aop.auto attribute is enabled by default, that is, @ EnableAspectJAutoProxy has been added by default as long as the AOP dependency is introduced, and there is no need to add @ EnableAspectJAutoProxy to the main class of the program to enable it.
2.2.Business module @ RestController@RequestMapping ("/ aopController") public class AopController {@ GetMapping (value = "/ Curry") public void Curry () {System.out.println ("Curry plays!") ;} @ GetMapping (value = "/ Harden") public void Harden () {System.out.println ("Harden is playing!") ;} @ GetMapping (value = "/ Antetokounmpo") public void Antetokounmpo () {System.out.println ("Brother Alphabet is playing!") ;} @ GetMapping (value = "/ Jokic") public void Jokic () {System.out.println ("Yokichi is playing!") ;} @ GetMapping (value = "/ Durant/ {point}") public void Durant (@ PathVariable ("point") int point) {System.out.println ("Durant plays!") ;}} 2.3, define aspect class
The @ Aspect annotation makes it a facet class
The @ Component annotation adds the aspect class to the IOC container
@ Aspect@Componentpublic class CustomAspect {/ / define pointcut @ Pointcut ("execution (public * com.lee.springaop.controller.AopController.* (..))") Public void CustomAspect () {} @ Pointcut ("execution (public * com.lee.springaop.controller.AopController.Durant (..)") Public void CustomAroundAspect () {} @ Pointcut ("execution (public * com.lee.springaop.controller.AopController.Durant (int)) & & args (int)") public void CustomAroundAspect2 () {} / / pre-notice @ Before ("CustomAspect ()") public void doBeforeGame () {System.out.println ("before: the agent is dealing with pre-match affairs!") ;} / / Post notice @ After ("CustomAspect ()") public void doAfterGame () {System.out.println ("after: the agent applauds madly for the star's performance!") ;} / / Post notice-normally returns @ AfterReturning ("CustomAspect ()") public void doAfterReturningGame () {System.out.println ("after returning: return notice: agents applaud the star's performance crazily!") ;} / / Post notification-an exception occurs @ AfterThrowing ("CustomAspect ()") public void doAfterThrowingGame () {System.out.println ("after throwing: exception notification: fans ask for a refund!") ;} / / surround notice @ Around ("CustomAroundAspect ()") public void doAroundGame (ProceedingJoinPoint joinPoint) {try {System.out.println ("Around before 1: the agent is handling pre-match affairs"); / / Parameter Object [] args = joinPoint.getArgs () For (Object o: args) {System.out.println ("params 1:" + o.toString ());} / / target Object target = joinPoint.getTarget (); System.out.println ("target 1:" + target.toString ()); / / make the next advice or target method call joinPoint.proceed () System.out.println ("Around after 1: return notice: agents applaud the star's performance crazily");} catch (Throwable throwable) {System.out.println ("Aroung throwing 1: abnormal notice: fans ask for a refund") }} / / surround Notification 2 @ Around ("CustomAroundAspect2 () & & args (point)") public void doAroundGame2 (ProceedingJoinPoint joinPoint,int point) {try {System.out.println ("Around before 2: the agent is handling the star's pre-match affairs"); / / proceed to the next advice or target method call joinPoint.proceed () System.out.println ("stars get 2:" + point + "points"); System.out.println ("Around after 2: return notice: agents applaud the star's performance crazily");} catch (Throwable throwable) {System.out.println ("Aroung throwing 2: abnormal notice: fans ask for a refund") 2.4.Test browser access: http://127.0.0.1:8080/aopController/Harden
Browser access: http://127.0.0.1:8080/aopController/Durant/80
Third, related grammar 3.1, wildcard * matches any number of characters.. Match the repetition of any number of characters, such as: type pattern: match any number of subpackages; method parameter pattern: match any quantity parameter + match the subtype of the specified type Note: can only be placed after the type pattern as a suffix
Eg:
Java.lang.String matches the String type. Java.*.String matches the String type under any "first-level subpackage" under the java package. Matches java.lang.String, but does not match java.lang.ss.Stringjava..* matches any type under java packages and any subpackages. For example, match java.lang.String and java.lang.annotation.Annotation java.lang.*ing match the type ending in ing under any java.lang package. Java.lang.Number+ matches the self-type of any Number under the java.lang package. If it matches java.lang.Integer, it also matches java.math.BigInteger "()" which means that the method does not have any parameters "(..)" Indicates that the matching accepts any parameter method "(.., java.lang.String)" means that the match accepts the end of a parameter of type java.lang.String, and the front of it can accept a method with any parameter; "(java.lang.String,..)" Indicates that the matching starts with a parameter of type java.lang.String, followed by a method that can accept any parameter; "(*, java.lang.String)" means that the matching accepts a parameter of type java.lang.String, and the method preceded by it accepts a method of any type; 3.2.The expression & & and | | or! Non-3.3, pointcut indicator 3.3.1, The indicator execution matches the link point executed by the method within to match methods within the specified type to execute this to match the current AOP proxy object type execution method target to match the current target object type execution method args to match the currently executed method passed in the specified type execution method @ within Used to match, so hold the method @ target within the specified annotation type to match the execution method of the current target object type Where the target object holds the specified annotation @ args to match the currently executed method passed in the parameter holding the specified annotation @ annotation to match the current execution method holding the specified annotation method bean to match the execution method reference pointcut representation of the Bean object with a specific name Reference other naming pointcuts 3.3.2, Example: 3.3.2.1, execution schema description public * * (..) Execution of any public method * cn.javass..IPointcutService.* () cn.javass package and any no-parameter method * cn.javass..*. in the IPointcutService interface under all subpackages * (..) cn.javass package and any method of any class under all subpackages * cn.javass..IPointcutService.* (*) cn.javass package and any method of IPointcutService interface under all subpackages with only one parameter * (! cn.javass..IPointcutService+). * (..) Any method other than "IPointcutService interface and subtype under cn.javass package and all subpackages" * cn.javass..IPointcutService+.* () cn.javass package and any no-parameter method of IPointcutService interface and subtype under all subpackage * cn.javass..IPointcut*.test* (java.util.Date) cn.javass package and method of IPointcut prefix type under all subpackages that begins with test and has only one parameter type java.util.Date * cn .javass.. IPointcut * .test * (..) Throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptioncn.javass packages and any method of IPointcut prefix type under all subpackages, and throws IllegalArgumentException and ArrayIndexOutOfBoundsException exceptions * (cn.javass..IPointcutService+&& java.io.Serializable+). (..) Any method @ java.lang.Deprecated * * (..) that implements the type of IPointcutService interface and java.io.Serializable interface under the cn.javass package and all subpackages. Any method that holds the @ java.lang.Deprecated annotation @ java.lang.Deprecated @ cn.javass..Secure * * (..) Any method that holds @ java.lang.Deprecated and @ cn.javass..Secure annotations @ (java.lang.Deprecated | | cn.javass..Secure) * * (..) Any method that holds @ java.lang.Deprecated or @ cn.javass..Secure annotations (@ cn.javass..Secure *) * (..) Any method that returns a value type holding @ cn.javass..Secure * (@ cn.javass..Secure). (..) Any type that defines a method holds the @ cn.javass..Secure method * (@ cn.javass..Secure (), @ cn.javass..Secure (*)) any method that has a signature with two parameters marked with @ Secure, such as public void test (@ Secure String str1, @ Secure String str1). * * ((@ cn.javass..Secure)) or * (@ cn.javass..Secure *) any method that takes a parameter and the parameter type holds @ cn.javass..Secure For example, public void test (Model model); and the Model class holds the @ Secure annotation * * (@ cn.javass..Secure (@ cn.javass..Secure *), @ cn.javass..Secure (@ cn.javass..Secure *)) any method with two parameters marked with @ cn.javass..Secure; and the type of both parameters holds @ cn.javass..Secure;* * (java.util.Map,.) Any method that takes a java.util.Map parameter that is typed as a
< cn.javass..Model, cn.javass..Model >Is a generic parameter; note that only the first parameter is java.util.Map, not including subtypes; for example, public void test (HashMap map, String str); will not match, you must use "* * (java.util.HashMap,..)" Match; and public void test (Map map, int I); will not match because the generic parameter does not match * * (java.util.Collection) any method with a parameter (type java.util.Collection), and the parameter type has a generic parameter that holds the @ cn.javass..Secure annotation on the generic parameter type, such as public void test (Collection collection); the generic parameter holds @ cn.javass..Secure3.3.2.2, within on the generic type
Within (type expression) matches method execution within a specified class
Pattern description within (cn.javass..*) cn.javass package and any method under the subpackage executes any method of IPointcutService type and subtype under the within (cn.javass..IPointcutService+) cn.javass package or all subpackages within (@ cn.javass..Secure *) any method of any type that holds the cn.javass..Secure annotation must declare this annotation on the target object, and what is declared on the interface has no effect on it 3.3.2.3, this
The execution method of this (type fully qualified name) matching the current AOP proxy object type
Schema description this (cn.javass.spring.chapter6.service.IPointcutService) current AOP object implements any method of the IPointcutService interface this (cn.javass.spring.chapter6.service.IIntroductionService) the current AOP object implements any method of the IIntroductionService interface may also introduce interface 3.3.2.4, target
Target (type fully qualified name) matches the execution method of the current target object type
Schema description target (cn.javass.spring.chapter6.service.IPointcutService) current target object (non-AOP object) implements any method of IPointcutService interface target (cn.javass.spring.chapter6.service.IIntroductionService) current target object (non-AOP object) implements any method of IIntroductionService interface can not be introduced into interface 3.3.2.5, args
Args (type parameter list) matches the currently executed method. The parameter passed in is the execution method of the specified type.
Schema description args (java.io.Serializable,..) Any one starts with accepting "incoming parameter type as java.io.Serializable" and can then be executed with any method of any type of parameter. The parameter type specified by args is 3.3.2.6, @ within, which is dynamically matched at run time.
Use "@ within" to match so hold methods within the specified annotation type; the annotation type must also be a fully qualified type name
Schema description @ within cn.javass.spring.chapter6.Secure) any type corresponding to the target object holds the class method of the Secure annotation; this annotation must be declared on the target object, and the declaration on the interface has no effect on it 3.3.2.7, @ target
Use "@ target" to match the execution method of the current target object type, where the target object holds the specified annotation; the annotation type must also be a fully qualified type name
Schema description @ target (cn.javass.spring.chapter6.Secure) any class method that holds a Secure annotation on the target object; this annotation must be declared on the target object, and 3.3.2.8, @ args declared on the interface has no effect on it
Use "@ args (annotation list)" to match the parameters passed by the currently executed method to hold the execution of the specified annotation; the annotation type must also be a fully qualified type name
The pattern description @ args (cn.javass.spring.chapter6.Secure) any method that accepts only one parameter, and the parameters passed in by the method runtime hold the annotated cn.javass.spring.chapter6.Secure; dynamic pointcut, similar to the arg indicator; 3.3.2.9, @ annotation
Use "@ annotation" to match the method in which the current execution method holds the specified annotation; the annotation type must also be a fully qualified type name
The pattern description @ annotation (cn.javass.spring.chapter6.Secure) holds comments on the current execution method cn.javass.spring.chapter6.Secure will be matched to 3.3.2.10, bean
Use "bean (Bean id or name wildcard)" to match the execution method of a Bean object with a specific name; extended by Spring ASP, there is no corresponding concept in AspectJ
The schema description bean (* Service) matches all Bean3.3.2.11, reference pointcut that end with the Service name (id or name)
Indicates that other naming pointcuts are referenced. Only @ ApectJ style supports it, but not Schema style.
Package cn.javass.spring.chapter6.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @ Aspect public class ReferencePointcutAspect {@ Pointcut (value= "execution (* * ())") public void pointcut () {}}
Reference:
@ Before (value = "cn.javass.spring.chapter6.aop.ReferencePointcutAspect.pointcut ()") public void referencePointcutTest2 (JoinPoint jp) {} so much about how to program AOP in Springboot. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
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.