@Around的表达式中“*”的表示
时间: 2024-05-26 22:18:10 浏览: 10
在Spring AOP中,@Around注解用于定义一个环绕通知,它可以拦截目标方法的执行,可以在目标方法执行之前和之后执行一些自定义的操作,比如记录日志、处理异常等。
@Around注解中的表达式表示被拦截的方法。其中,* 表示匹配任意的方法名,.. 表示匹配任意的参数列表,例如:
- execution(* com.example.service.*.*(..)):匹配com.example.service包中的所有方法
- execution(* com.example.service.UserService.*(..)):匹配com.example.service.UserService中的所有方法
- execution(* com.example.service.*Service.*(..)):匹配com.example.service包中以Service结尾的所有类的所有方法
- execution(* com.example.service..*.*(..)):匹配com.example.service包及其子包中所有类的所有方法
相关问题
@Around使用示例
@Around是Spring AOP中的一个注解,用于实现面向切面编程。它可以在目标方法之前和之后织入增强动作,并且可以决定目标方法的执行时机、执行方式,甚至可以完全阻止目标方法的执行。此外,@Around还可以改变目标方法的参数值和返回值。
下面是一个@Around使用示例:
```java
@Aspect
@Component
public class LoggingAspect {
@Around("execution(* com.example.MyService.*(..))")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
return result;
}
}
```
在上面的示例中,我们定义了一个切面类LoggingAspect,并在其中定义了一个@Around增强方法logExecutionTime。该方法使用了@Around注解,并指定了切入点表达式execution(* com.example.MyService.*(..)),表示对com.example.MyService包下的所有方法进行增强。
在logExecutionTime方法中,我们首先记录了目标方法的开始时间startTime,然后调用joinPoint.proceed()执行目标方法,并将返回值保存在result变量中。接着,我们计算了目标方法的执行时间executionTime,并打印了执行时间信息。最后,我们返回了目标方法的返回值result。
通过使用@Around注解,我们可以在目标方法执行前后进行一些额外的操作,比如记录方法的执行时间、打印日志等。
@Around("call(* org.apache.log4j.Category.debug(..))") call不存在
`@Around("call(* org.apache.log4j.Category.debug(..))")` 表示切入点表达式是 `call(* org.apache.log4j.Category.debug(..))`,但是这个表达式是错误的。
`call` 表达式是 AspectJ 中的一种切入点表达式,它用于匹配方法调用连接点。但是在 Spring AOP 中,只支持 `execution` 表达式,用于匹配方法执行连接点。
因此,如果想要在 Spring AOP 中匹配 `org.apache.log4j.Category.debug` 方法的执行连接点,可以使用以下切入点表达式:
```
@Around("execution(* org.apache.log4j.Category.debug(..))")
```
其中,`execution` 表达式可以匹配 `org.apache.log4j.Category.debug` 方法的任意执行连接点。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)