aop@Around不生效
时间: 2023-09-08 14:13:30 浏览: 79
如果在使用AOP的时候,@Around不生效,可能有以下几个原因:
1. 没有添加@EnableAspectJAutoProxy注解:在使用AOP时,需要在配置类上添加@EnableAspectJAutoProxy注解,才能开启AOP的功能。
2. 没有将切面类注册到Spring容器中:在使用AOP时,需要将切面类注册到Spring容器中,才能让Spring容器知道这个切面类的存在。
3. 切面类的优先级不正确:如果有多个切面类,需要按照优先级的顺序进行执行。可以通过实现Ordered接口或使用@Order注解来设置切面类的优先级。
4. 目标方法没有被代理:如果目标方法没有被代理,并且直接在同一个类中被调用,那么@Around注解是不会生效的。
5. 切入点表达式不正确:如果切入点表达式不正确,也会导致@Around注解不生效。需要检查切入点表达式是否正确,是否能够匹配到目标方法。
以上是一些常见的@Around注解不生效的原因,可以根据具体情况进行排查和解决。
相关问题
@AfterReturning 注解在哪里生效
@AfterReturning注解在方法正常返回后生效。具体来说,它会在目标方法返回结果后执行,但在返回结果之前执行org.springframework.web.method.support.HandlerMethodReturnValueHandler。\[2\]在你提供的代码示例中,@AfterReturning注解被用于一个切面方法,该方法在指定的切入点方法执行后被调用。\[1\]所以,当切入点方法成功执行并返回结果时,@AfterReturning注解的方法会被调用。
#### 引用[.reference_title]
- *1* *2* [spring aop之 @AfterReturning 不生效](https://blog.csdn.net/ql_7256/article/details/128930767)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [@Before @After @Around @AfterReturning @AfterThrowing 执行顺序](https://blog.csdn.net/u011498478/article/details/117822120)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
springboot 自定义aop
Spring Boot允许我们使用自定义AOP来定制业务逻辑。AOP(面向切面编程)是一种编程范式,可以在不改变原有代码的情况下,对现有代码进行增强。
下面是自定义AOP的步骤:
1. 创建一个切面类,使用@Aspect注解标注该类。该类中需要定义一个或多个切点和通知方法。
```java
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(* com.example.demo.service.*.*(..))")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
// 前置通知
System.out.println("Before method execute...");
// 执行目标方法
Object result = pjp.proceed();
// 后置通知
System.out.println("After method execute...");
return result;
}
}
```
上述代码中,@Pointcut注解定义了一个切点,切点表达式指定了需要增强的方法,这里指定为com.example.demo.service包下的所有方法。@Around注解定义了一个环绕通知方法,该方法在目标方法执行前后分别执行前置通知和后置通知。
2. 在应用程序入口类上添加@EnableAspectJAutoProxy注解,启用Spring的AOP功能。
```java
@SpringBootApplication
@EnableAspectJAutoProxy
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
3. 在需要增强的方法上添加自定义注解,并在切面类中定义一个针对该注解的切点。
```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
}
```
```java
@Aspect
@Component
public class MyAspect {
@Pointcut("@annotation(com.example.demo.annotation.MyAnnotation)")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
// 前置通知
System.out.println("Before method execute...");
// 执行目标方法
Object result = pjp.proceed();
// 后置通知
System.out.println("After method execute...");
return result;
}
}
```
上述代码中,@MyAnnotation注解用于标注需要增强的方法,@Pointcut注解定义一个针对@MyAnnotation注解的切点,@Around注解定义一个环绕通知方法,该方法在目标方法执行前后分别执行前置通知和后置通知。
4. 在需要增强的方法上添加@MyAnnotation注解。
```java
@Service
public class UserServiceImpl implements UserService {
@Override
@MyAnnotation
public void addUser(User user) {
System.out.println("Add user: " + user.getName());
}
}
```
上述代码中,addUser方法上添加了@MyAnnotation注解,表示该方法需要增强。
5. 运行应用程序,观察控制台输出。
```java
Before method execute...
Add user: Alice
After method execute...
```
上述输出结果表明,自定义AOP已经生效,增强逻辑被成功执行。
总结:Spring Boot允许我们使用自定义AOP来定制业务逻辑,可以通过定义切面类、切点、通知方法等方式来实现增强逻辑。在实际应用中,我们可以根据具体需求来制定相应的AOP策略,从而提高应用程序的可扩展性和可维护性。