基于@Aspect的AOP配置

本文详细介绍了如何使用Spring的注解方式配置AOP,包括@Aspect的使用及各种通知类型(前置、后置、异常、最终和环绕通知)的具体实现。此外,还展示了如何定义公共切入点来简化重复代码。

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

1、Spring除了支持Schema方式配置AOP,还支持注解方式:使用@Aspect来配置

2、Spring默认不支持@Aspect风格的切面声明,通过如下配置开启@Aspect支持:

<aop:aspectj-autoproxy/>  
3、通过以上配置,Spring就能发现用@Aspect注解的切面内并把它应用到目标对象上。

4、定义一个切面:
@Aspect  
public class AspectStyle {  
     @Before("execution(* com.sxit..*.*(..))")  
     public void before(){  
         System.out.println("方法执行前执行.....");  
     }  
 } 
 5、后置返回通知:

  @AfterReturning("execution(* com.sxit..*.*(..))")  
    public void afterReturning(){  
            System.out.println("方法执行完执行.....");  
    }  
 6、后置异常通知:
   
@AfterThrowing("execution(* com.sxit..*.*(..))")  
    public void throwss(){  
            System.out.println("方法异常时执行.....");  
    }  
 7、后置最终通知:

 @After("execution(* com.sxit..*.*(..))")  
    public void after(){  
            System.out.println("方法最后执行.....");  
    }  
 8、环绕通知:
   
 @Around("execution(* com.sxit..*.*(..))")  
    public Object around(ProceedingJoinPoint pjp){  
            System.out.println("方法环绕start.....");  
            try {  
                pjp.proceed();  
            } catch (Throwable e) {  
                e.printStackTrace();  
            }  
            System.out.println("方法环绕end.....");  
    }  
 9、按上面的每一个通知都要写一个定义,其实这部分可以抽出来,定义个一个公共的切入点。

 package com.sxit;  
      
    import org.aspectj.lang.ProceedingJoinPoint;  
    import org.aspectj.lang.annotation.After;  
    import org.aspectj.lang.annotation.AfterReturning;  
    import org.aspectj.lang.annotation.AfterThrowing;  
    import org.aspectj.lang.annotation.Around;  
    import org.aspectj.lang.annotation.Aspect;  
    import org.aspectj.lang.annotation.Before;  
    import org.aspectj.lang.annotation.Pointcut;  
      
    @Aspect  
    public class AspectStyle {  
          
        @Pointcut("execution(* com.sxit..*.*(..))")  
        public void init(){  
              
        }  
      
        @Before(value="init()")  
        public void before(){  
            System.out.println("方法执行前执行.....");  
        }  
          
        @AfterReturning(value="init()")  
        public void afterReturning(){  
            System.out.println("方法执行完执行.....");  
        }  
          
        @AfterThrowing(value="init()")  
        public void throwss(){  
            System.out.println("方法异常时执行.....");  
        }  
          
        @After(value="init()")  
        public void after(){  
            System.out.println("方法最后执行.....");  
        }  
          
        @Around(value="init()")  
        public Object around(ProceedingJoinPoint pjp){  
            System.out.println("方法环绕start.....");  
            Object o = null;  
            try {  
                o = pjp.proceed();  
            } catch (Throwable e) {  
                e.printStackTrace();  
            }  
            System.out.println("方法环绕end.....");  
            return o;  
        }  
    }  
 10、打印信息:

 方法before前执行.....  
    方法环绕start.....  
    我看.....................  
    方法after执行.....  
    方法环绕end.....  
    方法afterReurning执行.....  

参考自:
http://jinnianshilongnian.iteye.com/blog/1418598http://ch-space.iteye.com/blog/493956
 http://jinnianshilongnian.iteye.com/blog/1418598http://ch-space.iteye.com/blog/493956



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值