[Error] Spring - @Aspect 不生效或直接报错

🚫 @Aspect 不生效或直接报错

  • 错误重现

image-20230722160035945

  • 错误原因:maven导包是作用域标签问题
    • 在这里插入图片描述- 解决办法:去掉作用域即可(不要忘记重新加载maven
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
@Aspect is an annotation in Spring that is used to define an aspect, which is a modularization of a concern that cuts across multiple classes. Aspects are used to encapsulate cross-cutting functionalities, such as logging, security, transaction management, and error handling, and apply them to multiple classes in a consistent and reusable manner. To use @Aspect, you need to define an aspect class that contains advice and pointcut expressions. The advice defines the action to be taken when a particular pointcut expression is matched, while the pointcut expression defines the set of join points where the advice should be applied. Here is an example of an aspect class that logs the execution of a method: ``` @Aspect public class LoggingAspect { @Before("execution(* com.example.MyService.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Method " + joinPoint.getSignature().getName() + " is about to be executed..."); } @AfterReturning("execution(* com.example.MyService.*(..))") public void logAfterReturning(JoinPoint joinPoint) { System.out.println("Method " + joinPoint.getSignature().getName() + " has been executed successfully."); } @AfterThrowing("execution(* com.example.MyService.*(..))") public void logAfterThrowing(JoinPoint joinPoint) { System.out.println("Method " + joinPoint.getSignature().getName() + " has thrown an exception."); } } ``` In this example, the aspect class logs the execution of any method in the com.example.MyService class. The @Before advice is executed before the method is executed, the @AfterReturning advice is executed after the method has returned successfully, and the @AfterThrowing advice is executed after the method has thrown an exception. To enable the aspect in your Spring application, you need to add the @EnableAspectJAutoProxy annotation to your configuration class. This annotation enables Spring's aspect-oriented programming (AOP) support and allows the framework to create proxies for the classes that match the pointcut expressions defined in the aspect. ``` @Configuration @EnableAspectJAutoProxy public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } @Bean public LoggingAspect loggingAspect() { return new LoggingAspect(); } } ``` In this example, the AppConfig class defines a bean for the MyService interface and its implementation, as well as a bean for the LoggingAspect class. The @EnableAspectJAutoProxy annotation enables AOP support in the application context and allows Spring to intercept calls to the MyService bean and apply the logging aspect to it.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bayyys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值