This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge
A Pointcut refers to those methods that need to be “AOP” and is described by a “Pointcut Expression”. Pointcut can have the following ways to define or through && | | and! In the same way.
Expression Common methods
Method parameter matching
args()
@args()
Method description matching
execution()
Current AOP proxy object type matches
this()
Target class matching
target()
@target()
within()
@within()
Method matches marked with this annotation
@annotation()
Execution is the most common, in this format:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?) returning type pattern,name pattern, Ret-type-pattern: can be * to indicate any return value, the class name of the full path, etc. Name-pattern: specifies the method name, and the parameters pattern is required. * represents all methods that start with set. * represents all methods that start with set. (*) represents all parameters,(*) represents one parameter (*,String) means the first parameter is any value, and the second parameter is String.Copy the code
For example:
Execution of any public method:
execution(public * *(..) )Copy the code
Execution of any method that starts with “set” :
execution(* set*(..) ) AccountService interface method of arbitrary execution: execution (* com. Xyz. Service. AccountService. * (..) )Copy the code
Execution of any method defined in the service package:
execution(* com.xyz.service.*.*(..) )Copy the code
Execution of any method of any class defined in the Service package and all subpackages:
execution(* com.xyz.service.. *. * (..) )Copy the code
Execution of any method of class JoinPointObjP2 defined in the PointCutexp package and all subpackages:
execution(* com.test.spring.aop.pointcutexp.. JoinPointObjP2.*(..) )") ***> nearest (..) Is the method name, close to.*(..) JoinPointObjP2.*(..) )Copy the code
Any class in the PointCutexp package.
Within (com) test) spring. Aop. Pointcutexp. *) pointcutexp packages and all child any class of the bag. The within (. Com. Test. The spring aop) pointcutexp). *)Copy the code
MyInterface implements all classes of the MyInterface interface. If MyInterface is not an interface, qualify MyInterface as a single class.
This (com) test) spring. Aop. Pointcutexp. MyInterface) * * * > when a class implements an interface is aop, with getBean method must be cast as the interface type, not for the type of the class.Copy the code
Any method of any class with the @myTypeAnnotation annotation.
@within(com.elong.annotation.MyTypeAnnotation)
@target(com.elong.annotation.MyTypeAnnotation)
Any method with an @myTypeAnnotation annotation.
@annotation(com.elong.annotation.MyTypeAnnotation)
***> @Within and @Target annotations for classes, and @annotation is annotation for methods
Method with the @mymethodannotation parameter.
@args(com.elong.annotation.MyMethodAnnotation)
Methods that take a String (run is determined).
args(String)
Configure the Pointcut XML format
<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop "Default - the autowire =" byName "http://www.springframework.org/schema/aop/spring-aop-3.0.xsd > < bean id="ecoServiceInterceptor" class="com.elong.eco.EcoServiceInterceptor"/> <aop:config> <aop:pointcut id="ecoPoint" expression="(execution(public * *.. *. * (..) )) && (@within(com.elong.eco.annotation.EcoExtension))"/> <aop:advisor pointcut-ref="ecoPoint" advice-ref="ecoServiceInterceptor"/> </aop:config> </beans>Copy the code
Java annotations
<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "default - the autowire =" byName "> <! <context:component-scan base-package=" com.elon.eco "/> <! </beans> </beans>Copy the code
package com.elong.eco;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Compoment @Aspect public class EcoServiceAspect { @Pointcut("(execution(public * *.. *. * (..) )) && (@within(com.elong.eco.annotation.EcoExtension))") public void ecoAnotationPoint() { } @Around("ecoAnotationPoint()") public Object around(ProceedingJoinPoint point) throws Throwable { // TODO something return point.proceed(); // Do not execute the target method without calling point.proceed()}}Copy the code