- 為了在Spring中啟動@AspectJ支持,需要在類加載路徑下新增兩個AspectJ庫:aspectjweaver.jar和aspectjrt.jar。除此之外,Spring AOP還需要依賴一個aopalliance.jar包
- 定義一個類似ServiceAspect.java這樣的切面bean:
-
1 package com.hyq.aop; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.aspectj.lang.JoinPoint; 6 import org.aspectj.lang.ProceedingJoinPoint; 7 import org.aspectj.lang.annotation.After; 8 import org.aspectj.lang.annotation.AfterReturning; 9 import org.aspectj.lang.annotation.AfterThrowing; 10 import org.aspectj.lang.annotation.Around; 11 import org.aspectj.lang.annotation.Aspect; 12 import org.aspectj.lang.annotation.Before; 13 import org.aspectj.lang.annotation.Pointcut; 14 import org.springframework.stereotype.Component; 15 16 /** 17 * 系統服務組件Aspect切面Bean 18 * @author Shenghany 19 */ 20 //聲明這是一個組件 21 @Component 22 //聲明這是一個切面Bean 23 @Aspect 24 public class ServiceAspect { 25 26 private final static Log log = LogFactory.getLog(ServiceAspect.class); 27 28 //配置切入點,該方法無方法體,主要為方便同類中其他方法使用此處配置的切入點 29 @Pointcut("execution(* com.hyq.aop..*(..))") 30 public void aspect(){ } 31 32 /* 33 * 配置前置通知,使用在方法aspect()上注冊的切入點 34 * 同時接受JoinPoint切入點對象,可以沒有該參數 35 */ 36 @Before("aspect()") 37 public void before(JoinPoint joinPoint){ 38 System.out.println("執行before....."); 39 } 40 41 //配置后置通知,使用在方法aspect()上注冊的切入點 42 @After("aspect()") 43 public void after(JoinPoint joinPoint){ 44 System.out.println("執行after....."); 45 } 46 47 //配置環繞通知,使用在方法aspect()上注冊的切入點 48 @Around("aspect()") 49 public void around(JoinPoint joinPoint){ 50 long start = System.currentTimeMillis(); 51 try { 52 ((ProceedingJoinPoint) joinPoint).proceed(); 53 long end = System.currentTimeMillis(); 54 if(log.isInfoEnabled()){ 55 log.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms!"); 56 } 57 } catch (Throwable e) { 58 long end = System.currentTimeMillis(); 59 if(log.isInfoEnabled()){ 60 log.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : " + e.getMessage()); 61 } 62 } 63 } 64 65 //配置后置返回通知,使用在方法aspect()上注冊的切入點 66 @AfterReturning("aspect()") 67 public void afterReturn(JoinPoint joinPoint){ 68 if(log.isInfoEnabled()){ 69 log.info("afterReturn " + joinPoint); 70 } 71 } 72 73 //配置拋出異常后通知,使用在方法aspect()上注冊的切入點 74 @AfterThrowing(pointcut="aspect()", throwing="ex") 75 public void afterThrow(JoinPoint joinPoint, Exception ex){ 76 if(log.isInfoEnabled()){ 77 log.info("afterThrow " + joinPoint + "\t" + ex.getMessage()); 78 } 79 } 80 81 }
3.定義一個業務組件,如:
-
1 package com.hyq.aop; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.springframework.stereotype.Component; 6 @Component() 7 public class UserService { 8 9 private final static Log log = LogFactory.getLog(UserService.class); 10 11 public User get(long id){ 12 if(log.isInfoEnabled()){ 13 log.info("getUser method . . ."); 14 } 15 return new User(); 16 } 17 18 public void save(User user){ 19 if(log.isInfoEnabled()){ 20 log.info("saveUser method . . ."); 21 } 22 } 23 24 public boolean delete(long id) throws Exception{ 25 if(log.isInfoEnabled()){ 26 log.info("delete method . . ."); 27 throw new Exception("spring aop ThrowAdvice演示"); 28 } 29 return false; 30 } 31 32 }
業務組件要用@Component()注解修飾
4.在bean.xml中加入下面配置:
1 <!-- 激活組件掃描功能,在包com.hyq.aop及其子包下面自動掃描通過注解配置的組件 --> 2 <context:component-scan base-package="com.hyq.aop"/> 3 <!-- 激活自動代理功能 --> 4 <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> --> 5 <aop:aspectj-autoproxy/>