Spring AOP junit錯誤整理

一、【spring】[Xlint:invalidAbsoluteTypeName]errorhtml

首先說一下最基本的錯誤,使用AOP的pointcut的expression表達式必須是正確的,表達式規則相見另一篇spring

表達式不正確回報 express

二、error at ::0 formal unbound in pointcutide

若是你的AOP的method有參數,必須指明參數,例如:測試

a、註解配置指明參數:spa

@AfterReturning(pointcut = "pointCutMethod()", returning = "result")
    public void doAfterReturning(String result) {
        System.out.println("後置通知");
        System.out.println("---" + result + "---");
    }
b、XML配置指明參數code

<aop:after-returning method="doAfterReturning" result="result" />component

三、error at ::0 can't find referenced pointcut XXXorm

若是出現這種錯誤記得檢查依賴包是否與jdk匹配,最多見的是xml

jdk1.6

aspectjweaver.jar

aspectjrt.jar

jdk1.7 aspectjweaver1.7.jar aspectjrt1.7.jar

 

 

 

四、使用junit測試的時候我測試的代碼複製以下:

test-context.xml(也就是常說的ApplicationContext.xml)

 1 <?xml version="1.0" encoding="UTF-8"?>  2 <beans xmlns="http://www.springframework.org/schema/beans"  3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"  4 xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"  5 xmlns:context="http://www.springframework.org/schema/context"  6 xmlns:aop="http://www.springframework.org/schema/aop"  7 xsi:schemaLocation="  8 http://www.springframework.org/schema/beans  9 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 12 http://www.springframework.org/schema/jee 13 http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 14 http://www.springframework.org/schema/context 15 http://www.springframework.org/schema/context/spring-context-2.5.xsd 16 http://www.springframework.org/schema/aop 17 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" 18 default-lazy-init="false"> 19 20 <context:component-scan base-package="com.founder.test" /> 21 22 <!-- i18n Resourcebundles --> 23 24 25 <!-- <aop:aspectj-autoproxy/> --> 26 <aop:config> 27 <aop:aspect id="saveRptLog" ref="aopService"> 28 <aop:pointcut expression="execution(* com.founder.test.aop.AopGetStrService.getStr(..))" id="sendXML"/> 29 <aop:after-returning method="doAfterReturning" pointcut-ref="sendXML"></aop:after-returning> 30 <aop:after-throwing method="doAfterThrowing" pointcut-ref="sendXML" throwing="e"></aop:after-throwing> 31 </aop:aspect> 32 </aop:config> 33 </beans>

下面是業務代碼

 1 package com.founder.test.aop;  2  3 public interface AopService {  4  5 public String aopTest();  6  7 }  8  9 -------------------------------------------------------------------------------- 10 package com.founder.test.aop; 11 12 import org.springframework.beans.factory.annotation.Autowired; 13 import org.springframework.stereotype.Component; 14 15 @Component 16 public class AopServiceImpl implements AopService { 17 18  @Autowired 19  AopGetStrService aopGetStrService; 20  @Override 21 public String aopTest() { 22 String str = aopGetStrService.getStr(); 23 return str; 24  } 25 26 } 27 -------------------------------------------------------------------------------- 28 package com.founder.test.aop; 29 30 public interface AopGetStrService { 31 32 public String getStr(); 33 34 } 35 -------------------------------------------------------------------------------- 36 package com.founder.test.aop; 37 38 import org.springframework.stereotype.Component; 39 40 @Component 41 public class AopGetStrServiceImpl implements AopGetStrService { 42 43  @Override 44 public String getStr(){ 45 return "123"; 46  } 47 48 }

下面是AOP的實現

 1 package com.founder.test.aop;  2 
 3 import org.aspectj.lang.JoinPoint;  4 import org.aspectj.lang.ProceedingJoinPoint;  5 import org.springframework.stereotype.Component;  6  
 7 /**
 8  * Created by Dell on 2017/1/20.  9  */
10 @Component("aopService") 11 class TestAnnotationAspect { 12     private void pointCutMethod() { 13  } 14  
15     //聲明前置通知
16     public void doBefore() { 17         System.out.println("前置通知"); 18  } 19  
20     //聲明後置通知
21     public void doAfterReturning(JoinPoint point) { 22         Object[] args = point.getArgs(); 23         System.out.println("後置通知"); 24         //System.out.println(args[0]);
25  } 26  
27     //聲明例外通知
28     public void doAfterThrowing(Exception e) { 29         System.out.println("例外通知"); 30  System.out.println(e.getMessage()); 31  } 32  
33     //聲明最終通知
34     public void doAfter() { 35         System.out.println("最終通知"); 36  } 37  
38     //聲明環繞通知
39     public Object doAround(ProceedingJoinPoint pjp) throws Throwable { 40         System.out.println("進入方法---環繞通知"); 41         Object o = pjp.proceed(); 42         System.out.println("退出方法---環繞通知"); 43         return o; 44  } 45 }

 

最後是junit的測試

 1 package com.founder.test.aop;  2  3 import org.junit.Test;  4 import org.junit.runner.RunWith;  5 import org.springframework.beans.factory.annotation.Autowired;  6 import org.springframework.test.context.ContextConfiguration;  7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  8  9 @RunWith(SpringJUnit4ClassRunner.class) 10 @ContextConfiguration(locations="classpath:test-context.xml") 11 public class AopTest { 12 13  @Autowired 14  AopService aopService; 15 16  @Test 17 public void testAOP(){ 18  System.out.println(aopService.aopTest()); 19 20  } 21 }

 

五、最後說一個基本問題,AOP實現的基礎是IOC,因此,切面必須是bean中可訪問的方法,不然AOP無效。

相關文章
相關標籤/搜索