spring的aop编程


更多请看 http://blog.csdn.net/wangpeng047/article/details/8560694


在上篇博文中,我向大家介绍了Aop重要概念和教程,这回给出代码示例。

一、XML方式

1. TestAspect:切面类

[java]  view plain copy
  1. package com.spring.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5.   
  6. public class TestAspect {  
  7.   
  8.     public void doAfter(JoinPoint jp) {  
  9.         System.out.println("log Ending method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());  
  10.     }  
  11.   
  12.     public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
  13.         long time = System.currentTimeMillis();  
  14.         Object retVal = pjp.proceed();  
  15.         time = System.currentTimeMillis() - time;  
  16.         System.out.println("process time: " + time + " ms");  
  17.         return retVal;  
  18.     }  
  19.   
  20.     public void doBefore(JoinPoint jp) {  
  21.         System.out.println("log Begining method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());  
  22.     }  
  23.   
  24.     public void doThrowing(JoinPoint jp, Throwable ex) {  
  25.         System.out.println("method " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + " throw exception");  
  26.         System.out.println(ex.getMessage());  
  27.     }  
  28. }  

2. AServiceImpl:目标对象

[java]  view plain copy
  1. package com.spring.service;  
  2.   
  3. // 使用jdk动态代理  
  4. public class AServiceImpl implements AService {  
  5.   
  6.     public void barA() {  
  7.         System.out.println("AServiceImpl.barA()");  
  8.     }  
  9.   
  10.     public void fooA(String _msg) {  
  11.         System.out.println("AServiceImpl.fooA(msg:" + _msg + ")");  
  12.     }  
  13. }  

3. BServiceImpl:目标对象

[java]  view plain copy
  1. package com.spring.service;  
  2.   
  3. // 使用cglib  
  4. public class BServiceImpl {  
  5.   
  6.     public void barB(String _msg, int _type) {  
  7.         System.out.println("BServiceImpl.barB(msg:" + _msg + " type:" + _type + ")");  
  8.         if (_type == 1)  
  9.             throw new IllegalArgumentException("测试异常");  
  10.     }  
  11.   
  12.     public void fooB() {  
  13.         System.out.println("BServiceImpl.fooB()");  
  14.     }  



  15.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值