Spring_Spring与AOP_AspectJ基于XML的实现

一、前置通知

 1 import org.aspectj.lang.JoinPoint;
 2 import org.aspectj.lang.ProceedingJoinPoint;
 3 import org.aspectj.lang.annotation.After;
 4 import org.aspectj.lang.annotation.AfterReturning;
 5 import org.aspectj.lang.annotation.AfterThrowing;
 6 import org.aspectj.lang.annotation.Around;
 7 import org.aspectj.lang.annotation.Aspect;
 8 import org.aspectj.lang.annotation.Before;
 9 import org.aspectj.lang.annotation.Pointcut;
10 
11 @Aspect  //表示当前类为切面
12 public class MyAspect {
13    @Before("execution(* *..ISomeService.doFirst(..))")
14    public void  myBefore(){
15        System.out.println("执行前置通知方法");
16    }
17    
18    @Before("execution(* *..ISomeService.doFirst(..))")
19    public void   myBefore(JoinPoint jp){
20        System.out.println("执行前置通知方法 jp="+jp);
21    }
22    
23    @AfterReturning("execution(* *..ISomeService.doSecond(..))")
24    public void myAfterReturning(){
25        System.out.println("执行后置通知方法");
26        
27    }
28    
29    @AfterReturning(value="execution(* *..ISomeService.doSecond(..))",returning="result")
30    public void myAfterReturning(Object result){
31        System.out.println("执行后置通知方法 result="+result);
32        
33    }
34    
35    @Around("execution(* *..ISomeService.doSecond(..))")
36    public Object myAround(ProceedingJoinPoint pjp) throws Throwable{
37        System.out.println("执行环绕通知方法,目标方法执行之前");
38        //执行目标方法
39        Object result = pjp.proceed();
40        System.out.println("执行环绕通知方法,目标方法执行之后");
41        if(result !=null){
42             result=((String)result).toUpperCase();
43        }
44        return result;  
45    }
46    
47    @AfterThrowing(value="execution(* *..ISomeService.doThird(..))",throwing="ex")
48    public void myAfterThrowing(Exception ex){
49       System.out.println("执行异常通知方法ex="+ex.getMessage());  
50    }
51    
52    @After("doThirdPointcut()")
53    public void myAfter(){
54       System.out.println("执行最终通知方法");  
55    }
56    //定义了一个切入点,叫doThirdPointcut()
57    @Pointcut("execution(* *..ISomeService.doThird(..))")
58    public void doThirdPointcut(){}
59 }
MyAspect
<!-- 注册切面 -->
    <bean id="myAspect" class="com.bjpowernode.xml.MyAspect"></bean>
     <!-- 注册目标对象 -->
     <bean id="someService" class="com.bjpowernode.xml.SomeServiceImpl"></bean> 
     <!-- AOP配置 -->
     <aop:config>
        <aop:pointcut expression="execution(* *..ISomeService.doFirst(..))" id="doFirstPointcut"/>
        <aop:pointcut expression="execution(* *..ISomeService.doSecond(..))" id="doSecond1Pointcut"/>
        <aop:pointcut expression="execution(* *..ISomeService.doThird(..))" id="doThirdPointcut"/>
        <aop:aspect ref="myAspect">
          <aop:before method="myBefore" pointcut-ref="doFirstPointcut"/>
          <aop:before method="myBefore(org.aspectj.lang.JoinPoint)" pointcut-ref="doFirstPointcut"/>
        </aop:aspect>
     </aop:config>

二、后置通知

1           <aop:after-returning method="myAfterReturning" pointcut-ref="doSecondPointcut"/>
2           <aop:after-returning method="myAfterReturning(java.lang.Object)" pointcut-ref="doSecondPointcut" returning="result" />

三、环绕通知

 <aop:around method="myAround" pointcut-ref="doSecondPointcut"/>

四、异常通知

 <aop:after-throwing method="myAfterThrowing(java.lang.Exception)" pointcut-ref="doThirdPointcut" throwing="ex"/>

五、最终通知

<aop:after method="myAfter" pointcut-ref="doThirdPointcut"/>

 

转载于:https://www.cnblogs.com/hoje/p/8478004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值