@Around("matchApi()")
时间: 2024-05-18 18:16:16 浏览: 14
`@Around`是Spring AOP中的一种通知(Advice),表示环绕通知,它可以在目标方法之前和之后执行一些代码,还可以在目标方法抛出异常时执行一些代码。`@Around`通知需要一个参数,即切入点表达式,它指定了需要被代理的方法。在这个例子中,`@Around("matchApi()")`表示应用到所有匹配`matchApi()`切入点表达式的方法上。
在方法体内,使用`ProceedingJoinPoint`类型的参数`joinPoint`来执行目标方法,它可以让通知方法控制目标方法的执行。`joinPoint.proceed()`方法执行目标方法,可以带上参数,返回目标方法的返回值。在目标方法执行前后,可以在`joinPoint.proceed()`方法前后添加一些额外的代码,实现一些切面的功能,例如日志记录、性能监控等。在方法返回目标方法的返回值之前,可以对目标方法的返回值进行一些处理。
在这个例子中,`around`方法通过`joinPoint.getSignature()`方法获取目标方法的签名,然后通过签名获取目标方法的名称和参数列表。在方法执行前,记录了方法的类名和方法名,以及方法的参数列表。在方法执行后,记录了方法的返回值。这样就实现了一个简单的接口调用日志记录功能。
相关问题
@Around 参数用法
@Around是Spring AOP中的一个注解,用于实现环绕增强(Around Advice)。它可以在目标方法执行前后进行拦截,并且可以控制目标方法的执行。在使用@Around注解时,需要注意以下几点:
1. @Around注解的参数应该是一个方法签名,而不是runTime。这是因为@Around注解需要指定一个切入点表达式,用于确定哪些方法需要被拦截。方法签名可以包含方法名、参数类型和返回类型等信息,以便准确地匹配目标方法。
2. @Around注解可以携带参数,这取决于自定义的注解中是否携带参数。如果自定义的注解中携带了参数,那么在增强处理类中也可以使用这些参数。通过在@Around注解中使用参数,可以在拦截方法执行前后对参数进行处理或传递额外的参数给目标方法。
下面是一个示例代码,演示了@Around注解的使用方法:
```java
@Aspect
@Component
public class LoggingAspect {
@Around("execution(* com.example.MyService.*(..))")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Before method execution");
Object result = joinPoint.proceed();
System.out.println("After method execution");
return result;
}
}
```
在上面的代码中,@Around注解被用于定义一个环绕增强的方法aroundAdvice。该方法会在目标方法执行前输出"Before method execution",在目标方法执行后输出"After method execution"。通过调用joinPoint.proceed()方法,可以继续执行目标方法。
@Around注解详解
@Around注解是Spring AOP中的一种通知类型,它可以在目标方法执行前后进行增强操作,并且可以控制目标方法的执行。下面是一个关于@Around注解的详细介绍:
1. @Around注解的使用方式:
```java
@Around("execution(* com.example.demo.service.*.*(..))")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
// 在目标方法执行前的操作
System.out.println("Before method execution");
// 执行目标方法
Object result = joinPoint.proceed();
// 在目标方法执行后的操作
System.out.println("After method execution");
return result;
}
```
在上面的例子中,@Around注解被应用在一个方法上,该方法的参数类型为ProceedingJoinPoint,它包含了目标方法的信息。在方法体内,我们可以在目标方法执行前后进行一些操作,并且通过调用`joinPoint.proceed()`来执行目标方法。
2. @Around注解的作用:
@Around注解可以用于实现一些横切关注点,例如日志记录、性能监控、事务管理等。通过在目标方法执行前后进行操作,我们可以在不修改目标方法的情况下,对其进行增强。
3. @Around注解的注意事项:
- 在@Around注解的方法内部,必须调用`joinPoint.proceed()`来执行目标方法,否则目标方法将不会被执行。
- 如果需要修改目标方法的返回值,可以在方法内部通过修改`result`变量的值来实现。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)