切入点Pointcut
时间: 2023-08-27 19:07:21 浏览: 58
切入点(Pointcut)是在AOP中用于匹配连接点(JoinPoint)的表达式。它描述了哪些方法或者哪些位置是需要被通知(Advice)的。在Spring AOP中,切入点可以描述一个具体的方法,也可以匹配多个方法。例如,可以使用切入点来描述com.demo.dao包下的BookDao接口中的无参无返回值的save方法,或者匹配所有的save方法、所有以get开头的方法、所有以Dao结尾的接口中的任意方法,以及所有带有一个参数的方法。[1][2]
相关问题
springboot aop 切入点签名
切入点签名是指在使用Spring AOP时,定义一个方法来指定需要被拦截的方法的名称、参数类型和个数等信息,用于确定切入点。
在Spring Boot中,使用@Pointcut注解来定义切入点签名。例如:
```java
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(* com.example.demo.service.*.*(..))")
public void myPointcut() {}
// ...
}
```
上述代码定义了一个切入点签名myPointcut(),用于拦截com.example.demo.service包中的所有方法。其中,execution(* com.example.demo.service.*.*(..))就是切入点表达式,表示拦截该包下的所有方法。
切入点签名可以在@Around、@Before、@After等注解中使用,用于确定需要被拦截的方法。例如:
```java
@Around("myPointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// ...
}
```
上述代码中,@Around注解中的myPointcut()就是切入点签名,用于拦截需要被环绕通知的方法。
aop通知如何指定切入点
aop通知可以通过注解或配置文件的方式指定切入点。
1. 通过注解指定切入点
在使用注解的方式时,可以使用@Pointcut注解来定义切入点,然后在通知方法上使用@Pointcut注解来指定要切入的方法。例如:
```java
@Pointcut("execution(* com.example.demo.service.*.*(..))")
public void servicePointcut() {}
@Before("servicePointcut()")
public void beforeAdvice() {
// ...
}
```
上面的代码中,@Pointcut注解指定了切入点为com.example.demo.service包下的所有方法,然后@Before注解指定了要在servicePointcut切入点之前执行的通知方法。
2. 通过配置文件指定切入点
在使用配置文件的方式时,可以使用<aop:pointcut>标签来定义切入点,然后在<aop:advisor>标签中使用pointcut-ref属性来引用切入点。例如:
```xml
<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(* com.example.demo.service.*.*(..))"/>
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
<bean id="beforeAdvice" class="com.example.demo.advice.BeforeAdvice"/>
```
上面的代码中,<aop:pointcut>标签定义了一个id为servicePointcut的切入点,然后<aop:advisor>标签中使用pointcut-ref属性引用了servicePointcut切入点,beforeAdvice为通知方法的实现类。