In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of @ Pointcut annotation expression in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
1 expression type
The expression types of the standard Aspectj Aop's pointcut are rich, but Spring Aop supports only 9 of them, plus a total of 10 types of expressions extended by Spring Aop itself, as follows.
Execution: generally used to specify the execution of methods, the most used.
Within: specifies all method execution of certain types, or it can be used to specify a package.
This:Spring Aop is based on proxy, and the generated bean is also a proxy object, and this is the proxy object. When this object can be converted to a specified type, the corresponding pointcut is it, and Spring Aop will take effect.
Target: when the proxied object can be converted to the specified type, the corresponding pointcut is it, and Spring Aop will take effect.
Args: takes effect when the parameter of the executed method is of the specified type.
Target: takes effect when there is a specified annotation on the target object of the agent.
Args: takes effect when there is a specified annotation on the type of method parameter that is executed.
@ within: similar to @ target, the official document and online saying that @ within only needs a specified annotation on the class or parent class of the target object, @ within will take effect, and @ target means that there must be a specified annotation on the class of the target object. According to the author's test, both are as long as there are specified comments on the target class or parent class.
Annotation: takes effect when there is a specified annotation on the executing method.
Bean: takes effect when the method called is the specified bean method.
2 using sample 2.1 execution
Execution is the most frequently used Pointcut expression that represents the execution of a method, and its standard syntax is as follows.
Execution (modifiers-pattern? Ret-type-pattern declaring-type-pattern? Name-pattern (param-pattern) throws-pattern?)
Modifiers-pattern indicates the access type of the method, public, etc.; ret-type-pattern indicates the return type of the method, such as String indicates that the return type is String, and "*" represents all return types; declaring-type-pattern represents the declaration class of the method, such as "com.elim..*" represents all types under the com.elim package and its subpackages; name-pattern represents the name of the method, such as "add*" represents all method names that begin with add Param-pattern represents the type of method parameter, and name-pattern (param-pattern) is actually the parameter type corresponding to the method set represented together. For example, "add ()" represents the add method without parameters, "add (*)" represents the add method with one parameter of any type, "add (*, String)" represents the add method with two parameters, and the second parameter is the add method of type String; throws-pattern represents the exception type. The part that ends with a question mark can be omitted.
1. "execution (* add ())" matches all add () methods without parameters.
2. "execution (public * com.elim..*.add* (..))" Matches all public methods that start with add for all classes under all com.elim packages and their subpackages.
3. "execution (* * (..) throws Exception)" matches all methods that throw Exception.
2.2 within
Within is used to specify a type, and all methods in the specified type will be intercepted.
1. "within (com.elim.spring.aop.service.UserServiceImpl)" matches all external calls to methods of the corresponding object of the UserServiceImpl class, and the object can only be of type UserServiceImpl, not its subtype.
2. "within (com.elim..*)" matches all external calls to methods of all classes under the com.elimi package and its subpackages.
2.3 this
Spring Aop is proxy-based, and this represents the proxy object. The syntax of an Pointcut expression of type this is this (type), which indicates a match when the generated proxy object can be converted to the type specified by type. Proxy objects generated by JDK interface-based proxies and CGLIB-based proxies are different.
1. The proxy object generated by the "this (com.elim.spring.aop.service.IUserService)" match is an external call to all methods of type IUserService.
2.4 target
Spring Aop is proxy-based, and target represents the target object of the delegate. A match is indicated when the target object being proxied can be converted to the specified type.
1. "target (com.elim.spring.aop.service.IUserService)" matches all external calls to methods that can be converted to IUserService types by all proxied target objects.
2.5 args
Args is used to match method parameters.
1. "args ()" matches any method without parameters.
2. "args (java.lang.String)" matches anything with only one parameter, and the type of this parameter is String's method.
3. "args (..)" A method with arbitrary parameters.
4. "args (java.lang.String,..)" The match takes any parameter, but the type of the first parameter is String's method.
5. "args (.., java.lang.String)" matches with any parameter, but the type of the last parameter is String's method.
2. 6 @ target
@ target matches when there is a specified annotation on the type corresponding to the proxied target object and its parent type.
1. When "@ target (com.elim.spring.support.MyAnnotation)" matches the type corresponding to the proxied target object with MyAnnotation annotations.
2.7 @ args
@ args matches situations where the called method contains parameters and the corresponding parameter type has the specified annotation.
1. "@ args (com.elim.spring.support.MyAnnotation)" matches the method call with the MyAnnotation annotation on the method parameter type. If we have a method add (MyParam param) that takes a parameter of type MyParam, and the class MyParam has an annotated MyAnnotation, it can be matched by the Pointcut expression "@ args (com.elim.spring.support.MyAnnotation)".
2. 8 @ within
@ within is used to match situations where the type corresponding to the proxied target object or its parent type has the specified annotation, but only if a method on the class that has the specified annotation is called.
1. "@ within (com.elim.spring.support.MyAnnotation)" matches the case where there are MyAnnotation annotations on the class declared by the called method. For example, if an annotated MyAnnotation annotation is used on a ClassA and a method a () is defined, the Pointcut will be matched when the ClassA.a () method is called If there is no MyAnnotation annotation on a ClassB, but it inherits from ClassA and has a method b () defined on it, the Pointcut will not be matched when the ClassB () .b () method is called, but the method call will be matched when the ClassB () .a () is called, because a () is defined on the parent type ClassA and the MyAnnotation annotation is used on the ClassA. But if the subclass ClassB overrides the a () method of the parent class ClassA, the call to the ClassB.a () method does not match the Pointcut.
2.9 @ annotation
@ annotation is used to match situations where there are specified annotations on the method.
1. "@ annotation (com.elim.spring.support.MyAnnotation)" matches all external calls to methods that have MyAnnotation annotations on them.
2.10 bean
Bean is used to match when a method of a bean of the specified Spring is called.
1. "bean (abc)" matches the method call in the Spring Bean container where id or bean whose name is abc.
2. "bean (user*)" matches all method calls whose id or name is a bean that begins with user.
3 expression combination
The combination of expressions is actually the logical operation of the corresponding expressions, and, OR, not. You can use them to combine multiple expressions.
1. "bean (userService) & & args ()" matches all the no-parameter methods of bean where id or name is userService.
2. "bean (userService) | | @ annotation (MyAnnotation)" matches a method call with id or bean whose name is userService, or a method call annotated with MyAnnotation on the method.
3. "bean (userService) & &! args ()" matches all parametric method calls to bean where id or name is userService.
4 Application of Pointcut expression based on Aspectj annotations
When using Spring Aop based on Aspectj annotations, we can define the Pointcut through the @ Pointcut annotation, specify its expression, and then specify Pointcut directly when we need to use the Pointcut expression.
@ Component@Aspectpublic class MyAspect {@ Pointcut ("execution (* add (..)") Private void beforeAdd () {} @ Before ("beforeAdd ()") public void before () {System.out.println ("- before-");}}
In the above code, we directly specify the expression of Pointcut corresponding to the beforeAdd () method of the current class definition in @ Before (). If we need to specify a Pointcut definition that is not in the current class, we need to add the class name, for example, in the following example, we refer to the expression of Pointcut defined on the add () method in MyService.
@ Before ("com.elim.spring.aop.service.MyService.add ()") public void before2 () {System.out.println ("- before2-");}
Of course, in addition to referencing its corresponding Pointcut expression indirectly by referencing the Pointcut definition, we can also use Pointcut expression directly, as in the following example, Pointcut expression is directly used in @ Before.
/ * * external execution of all add methods * / @ Before ("execution (* add ())") public void beforeExecution () {System.out.println ("- before execution-") } this is the end of the article on "sample Analysis of @ Pointcut Annotation expressions in Java". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please 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.