aop前置增强、后置、环绕增强

后置

   @AfterReturning(value="@annotation(com.sdyy.biz.act.maint.aop.annotation.ResIfNull)",returning="result")
     public void AfterReturning(JoinPoint joinPoint , Object result) {
   
         logger.info("AfterReturning------------start");

         ResIfNull myAnno=  ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(ResIfNull.class);
         if ("obj".equals(myAnno.type())) {
   
             if (result == null) {
   
                 throw new SysException("初始化流程失败,请定义服务流程");
             }

         } else if ("list".equals(myAnno.type())) {
   
             if (result instanceof List) {
   
                 List res = (List) result;
                 if (res.size() == 0) {
   
                     t
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
首先,需要在Spring配置文件中开启AOP的支持: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- AOP 配置 --> <aop:aspectj-autoproxy /> <!-- 其他 Bean 的配置 --> <!-- ... --> </beans> ``` 然后,分别使用XML和注解的方式实现各种增强: ## 前置增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:before pointcut="execution(* com.example.service.*.*(..))" method="beforeAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice() { // 前置增强的逻辑 } } ``` ## 后置增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:after-returning pointcut="execution(* com.example.service.*.*(..))" method="afterAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @AfterReturning("execution(* com.example.service.*.*(..))") public void afterAdvice() { // 后置增强的逻辑 } } ``` ## 环绕增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:around pointcut="execution(* com.example.service.*.*(..))" method="aroundAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @Around("execution(* com.example.service.*.*(..))") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { // 环绕增强前置逻辑 Object result = joinPoint.proceed(); // 环绕增强后置逻辑 return result; } } ``` ## 抛出异常增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:after-throwing pointcut="execution(* com.example.service.*.*(..))" method="afterThrowingAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @AfterThrowing("execution(* com.example.service.*.*(..))") public void afterThrowingAdvice() { // 抛出异常增强的逻辑 } } ``` ## 最终增强 使用XML配置: ```xml <aop:config> <aop:aspect id="myAspect" ref="myAspectBean"> <aop:after pointcut="execution(* com.example.service.*.*(..))" method="finallyAdvice" /> </aop:aspect> </aop:config> ``` 使用注解: ```java @Aspect @Component public class MyAspect { @After("execution(* com.example.service.*.*(..))") public void finallyAdvice() { // 最终增强的逻辑 } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值