使用SpringAOP实现自定义注解之切入点表达式

博客介绍了使用Spring AOP实现自定义注解,重点在于切入点PointCut表达式的书写,通过表达式扫描指定注解。还给出两种可扫描指定包下注解的写法,并提及Spring.xml配置及参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Spring AOP实现自定义注解时,关键在于切入点PointCut表达式的书写,即通过表达式扫描指定的注解。

以下给出两种写法,这两种写法都可以扫描指定包下的注解。

1、

/**
 * 作用:捕获方法异常,并修改异常返回结果
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExceptionMessageHandler {

}
@Aspect
@Component
public class ExceptionAnnotationAspect {
 
 private static final Logger LOG =Logger.getLogger(ExceptionAnnotationAspect.class);

 @Around("execution(@com.fish.webcore.annotation.ExceptionMessageHandler * com.fish.webcore..*.*(..))")
 public Object exceptionHandler(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  try {
   return proceedingJoinPoint.proceed();
  } catch (Exception e) {
   LOG.error("服务器内部错误!", e);
   return JacksonUtil.getExceptionResult(e, "服务器内部错误!");
  }
 }

}

2、

@Around("within(com.fish..*) && @annotation(lrt)")
public void authority(ProceedingJoinPoint proceedingJoinPoint, LogRunTime lrt) throws Throwable

Spring.xml配置:

    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="com.fish"/>

参考资料:

https://blog.youkuaiyun.com/qq_36951116/article/details/79172485

### Spring AOP 使用 CGLIB 和 注解 定义 切入点 在Spring框架中,当使用CGLIB代理时,通常是因为目标类没有实现接口或者是希望对final方法进行增强。为了启用CGLIB代理,除了基本的依赖外还需要额外导入`spring-boot-starter-aop`以及CGLIB库。 对于基于注解的方式定义切入点和切面来说,可以通过如下方式完成: #### 添加Maven依赖 确保项目中的pom.xml文件包含了必要的依赖项以便支持AOP特性及CGLIB动态代理机制。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- 如果不是Spring Boot项目,则需单独引入 --> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>3.3.0</version> </dependency> ``` #### 创建服务类 这里创建了一个简单的Service层组件作为被拦截的目标对象,并且该类并没有任何接口声明,这正是触发CGLIB子类化模式的原因之一。 ```java @Service public class MyServiceImpl { public void performTask() { System.out.println("Executing task..."); } } ``` #### 编写切面类并定义切入点表达式 下面展示了如何利用`@Aspect`标注一个POJO成为切面;同时运用`@Pointcut`来精确定位哪些方法应该受到监控。 ```java @Aspect @Component public class LoggingAspect { @Pointcut("execution(* com.example.service.MyServiceImpl.*(..))") // 匹配MyServiceImpl下的所有成员函数调用 private void myServiceMethods() {} @Before("myServiceMethods()") public void logBefore(JoinPoint joinPoint){ System.out.println("[BEFORE] Method Name: " + joinPoint.getSignature().getName()); } @AfterReturning(pointcut = "myServiceMethods()", returning="result") public void afterReturnLogging(JoinPoint joinPoint, Object result) { System.out.println("[RETURNING] Method Name: "+joinPoint.getSignature().getName()+" Result :"+result); } @Around("myServiceMethods()") public Object aroundAdvice(ProceedingJoinPoint pjp)throws Throwable{ long start=System.currentTimeMillis(); try { System.out.println("[AROUND-BEFORE]"); return pjp.proceed(); // 执行实际的方法体 } finally { System.out.println("[AROUND-AFTER], Time taken:"+(System.currentTimeMillis()-start)); } } @AfterThrowing(pointcut = "myServiceMethods()", throwing = "error") public void handleException(Exception error){ System.err.println("[EXCEPTION HANDLING]: An exception has been thrown! Details -> " + error.getMessage()); } } ``` 上述代码片段实现了四个不同类型的建议(advice),即前置(`@Before`)、后置返回(`@AfterReturning`)、环绕(`@Around`)还有异常捕获(`@AfterThrowing`)的通知行为[^2][^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值