spring切点函数有哪些
-
Spring切点函数是用来定义哪些方法应该在哪些连接点进行拦截和执行切面逻辑的函数。Spring框架中提供了多种切点函数供我们选择和使用。下面是一些常用的Spring切点函数:
-
execution(): 这是最常用的切点函数,用于匹配方法执行的连接点。可以使用通配符来匹配类名、方法名和参数类型。例如,execution(* com.example.service.UserService.*(..))将匹配UserService类中的所有方法。
-
within(): 匹配指定类型内的所有方法执行的连接点。例如,within(com.example.service.*)将匹配com.example.service包下的所有方法。
-
this(): 用于匹配当前执行对象类型为指定类型的连接点。例如,this(com.example.service.UserService)将匹配所有UserService类型的对象。
-
target(): 用于匹配目标对象类型为指定类型的连接点。例如,target(com.example.service.UserService)将匹配被代理的目标对象为UserService类型的连接点。
-
args(): 用于匹配方法参数类型匹配指定类型的连接点。例如,args(java.lang.String)将匹配所有具有一个String类型参数的方法。
-
@annotation(): 用于匹配标注有指定注解的方法执行的连接点。例如,@annotation(com.example.annotation.Loggable)将匹配标注有@Loggable注解的方法。
-
@within(): 用于匹配标注有指定注解的类型内的所有方法执行的连接点。例如,@within(com.example.annotation.Transactional)将匹配所有标注有@Transactional注解的类型内的方法。
-
@target(): 用于匹配目标对象类型标注有指定注解的连接点。例如,@target(com.example.annotation.Transactional)将匹配所有被@Transactional注解标注的目标对象。
-
@args(): 用于匹配方法参数类型标注有指定注解的连接点。例如,@args(com.example.annotation.Loggable)将匹配所有具有一个参数标注有@Loggable注解的方法。
以上是一些常用的Spring切点函数,可以根据实际需求选择适合的切点函数来定义切面的拦截逻辑。
1年前 -
-
Spring切点函数在基于面向切面编程(AOP)的开发中起着重要作用。它用于确定在哪些 Join Point(连接点)上会触发切面的逻辑处理。下面是几种常见的Spring切点函数:
-
execution:根据方法的执行规则匹配Join Point,例如"execution(* com.example.service..(..))"表示匹配com.example.service包下的所有方法。
-
within:匹配特定包或类下的所有Join Point,例如"within(com.example.service*)"表示匹配com.example.service包及其子包下的所有Join Point。
-
this:基于代理对象引用的类型进行匹配,例如"this(com.example.service.MyService)"表示匹配目标对象为MyService类型的方法。
-
target:基于目标类(被代理对象)的类型进行匹配,例如"target(com.example.service.MyService)"表示匹配目标类为MyService类型的方法。
-
args:基于方法参数类型进行匹配,例如"args(String, int)"表示匹配方法有两个参数,类型分别为String和int。
-
@target:基于目标类拥有特定注解进行匹配,例如"@target(org.springframework.stereotype.Service)"表示匹配具有@Service注解的目标类的方法。
-
@args:基于方法参数拥有特定注解进行匹配,例如"@args(com.example.annotation.Loggable)"表示匹配方法参数中标记了@Loggable注解的方法。
-
@within:基于目标类(被代理对象)拥有特定注解进行匹配,例如"@within(com.example.annotation.Transactional)"表示匹配目标类标记了@Transactional注解的方法。
-
@annotation:基于方法上拥有特定注解进行匹配,例如"@annotation(com.example.annotation.Loggable)"表示匹配被@Loggable注解标记的方法。
以上是常见的Spring切点函数,开发人员可以根据需求选择合适的切点函数来提供切面逻辑处理。
1年前 -
-
在Spring框架中,切点函数(Pointcuts)是指用于定义切点的一系列函数或表达式。切点函数用于指定哪些方法或类的执行应该被切入(拦截)以应用特定的通知(Advices)。下面是一些常见的Spring切点函数:
- execution:通过方法的执行来定义切点,用于匹配方法的执行。
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)其中,modifiers-pattern:表示方法的修饰符,如public、protected等;ret-type-pattern:表示方法的返回类型;declaring-type-pattern:表示方法所在的类;name-pattern:表示方法的名称;param-pattern:表示方法的参数类型;throws-pattern:表示方法所可能抛出的异常。
示例:
- execution(public * com.example.service.Service.(..)):匹配com.example.service包下的所有Service类中的所有方法。
- within:通过类或接口来定义切点,用于匹配指定类或接口下的所有方法。
within(type-pattern)其中,type-pattern:表示类或接口的全限定名,支持通配符(*)。
示例:
- within(com.example.service.*):匹配com.example.service包及其子包下的所有类的所有方法。
- this:通过目标对象的类型来定义切点,用于匹配指定类型的所有方法。
this(type)其中,type:表示目标对象的类型。
示例:
- this(com.example.service.UserService):匹配所有实现了UserService接口的类的所有方法。
- target:通过被代理的目标对象的类型来定义切点,用于匹配指定类型的所有方法。
target(type)其中,type:表示被代理的目标对象的类型。
示例:
- target(com.example.service.UserService):匹配所有实现了UserService接口的类的所有方法。
- args:通过方法的参数类型来定义切点,用于匹配带有特定参数的方法。
args(type-pattern)其中,type-pattern:表示参数类型的全限定名,支持通配符(*)。
示例:
- args(java.lang.String):匹配方法的参数类型为String的方法。
- @annotation:通过方法上的注解来定义切点,用于匹配带有特定注解的方法。
@annotation(annotation-type)其中,annotation-type:表示注解的类型。
示例:
- @annotation(org.springframework.transaction.annotation.Transactional):匹配带有@Transactional注解的方法。
以上是常见的Spring切点函数,可以根据实际需要选择合适的切点函数来定义切点。切点函数的灵活使用可以实现精确的方法拦截和增强。
1年前