Spring AOP junit错误整理

1、【spring】[Xlint:invalidAbsoluteTypeName]error

首先说一下最基本的错误,使用AOP的pointcut的expression表达式必须是正确的,表达式规则相见另外一篇

表达式不正确回报

2、error at ::0 formal unbound in pointcut

如果你的AOP的method有参数,必须指明参数,例如:

a、注解配置指明参数:

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

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

3、error at ::0 can't find referenced pointcut XXX

如果出现这种错误记得检查依赖包是否与jdk匹配,最常见的是

jdk1.6

aspectjweaver.jar

aspectjrt.jar

jdk1.7aspectjweaver1.7.jaraspectjrt1.7.jar

 

 

 

4、使用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 }

 

5、最后说一个基本问题,AOP实现的基础是IOC,所以,切面必须是bean中可访问的方法,否则AOP无效。

 

转载于:https://www.cnblogs.com/scyitgz/p/9797006.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值