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.7 | aspectjweaver1.7.jar | aspectjrt1.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的实现
package com.founder.test.aop; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component; /**
* Created by Dell on 2017/1/20.
*/
@Component("aopService")
class TestAnnotationAspect {
private void pointCutMethod() {
} //声明前置通知
public void doBefore() {
System.out.println("前置通知");
} //声明后置通知
public void doAfterReturning(JoinPoint point) {
Object[] args = point.getArgs();
System.out.println("后置通知");
//System.out.println(args[0]);
} //声明例外通知
public void doAfterThrowing(Exception e) {
System.out.println("例外通知");
System.out.println(e.getMessage());
} //声明最终通知
public void doAfter() {
System.out.println("最终通知");
} //声明环绕通知
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入方法---环绕通知");
Object o = pjp.proceed();
System.out.println("退出方法---环绕通知");
return o;
}
}
最后是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无效。
Spring AOP junit错误整理的更多相关文章
- spring 单元测试方法及其错误整理
spring 单元测试及其错误整理 目录: NO1 spring单元测试方法 - NO1.1 pom.xml文件中确认有下面依赖 - NO1.2 在需要测试的类上,或者新建的测试类上添加注解 - NO ...
- AOP面试知识整理,^_^-包括spring Aop
讲到java企业级开发框架,就不可避免的讲到 IOC,AOP,MCV 今天面试时被问到AOP,讲的很乱,这里整理笔记,包括AOP,spring-AOP的部分知识,错误的地方请小伙伴指出来. 谈谈你对A ...
- Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)
Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误) 问题: 今天在用AfterReturningAdvice时,a ...
- 用心整理 | Spring AOP 干货文章,图文并茂,附带 AOP 示例 ~
Spring AOP 是 Java 面试的必考点,我们需要了解 AOP 的基本概念及原理.那么 Spring AOP 到底是啥,为什么面试官这么喜欢问它呢?本文先介绍 AOP 的基本概念,然后根据 A ...
- spring aop开发常见错误
1. Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreExcepti ...
- Spring AOP使用整理:各种通知类型的介绍
2.PersonImpl类的源码 public class PersonImpl implements Person { private String name; private int age; p ...
- Spring AOP整理
示例展示 AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充.AOP之所以能得到广泛认可,主要是因为它将应用系统拆分分了 ...
- Spring AOP 整理笔记
一.AOP概念 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 利用AOP可以对业务逻辑的各 ...
- Spring错误——Spring AOP——org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
背景:学习切面,测试前置通知功能,xml配置如下 <?xml version="1.0" encoding="UTF-8"?> <beans ...
随机推荐
- Cocos Creator 热更新文件MD5计算和需要注意的问题
Creator的热更新使用jsb.热更新基本按照 http://docs.cocos.com/creator/manual/zh/advanced-topics/hot-update.html?h=% ...
- glsl:error C1105: cannot call a non-function
今天写的shader编译过程中报了这个错误,而且错误行数是0.原因怎么找也找不到.后来发现原来是normalize方法写成了了normal正好和函数的形参名字一样. 特地记录一下.
- CI/CD----jenkins+gitlab+django(内网)
1.py第三方包获取 ./pip3 ./pip3 -i "http://pypi.douban.com/simple/" --trusted-host pypi.douban.co ...
- iptables内网访问外网 ε=ε=ε=(~ ̄▽ ̄)~
介绍 iptables概述: netfilter/iptables : IP信息包过滤系统,它实际上由两个组件netfilter 和 iptables 组成. netfilter/iptables 关 ...
- 关于jq中input的value值clone的问题
如果想将input进行克隆,然后在后面显示出来并修改input里面的文字,这时就会发现一个问题,就是你克隆出来的value值始终是你克隆时的value,检查页面元素你就会发现,这时需要对克隆之后的in ...
- python 安装第三方包
python环境是Anaconda3安装的,由于项目需要用到git的第三方包,但是在conda自带的环境中没有. 例如使用jieba分词库. 安装的三种方式: (1)全自动安装:`easy_insta ...
- Spark入Hbase的四种方式效率对比
一.方式介绍 本次测试一种采用了四种方式进行了对比,分别是:1.在RDD内部调用java API.2.调用saveAsNewAPIHadoopDataset()接口.3.saveAsHadoopDat ...
- Js文件函数中调用另一个Js文件函数的方法
在项目中Js文件需要完成某一功能,但这一功能的大部分代码在另外一个Js文件已经完成,只需要调用这个文件实现功能.那么如何调用:一个Js文件函数中调用另一个Js文件函数的方法? (直接代码说明) 示例d ...
- TTTTTTTTTTTTTTTTTT hdu 1800 字符串哈希 裸题
题意:意思是有若干个飞行员,需要在扫帚上练习飞行,每个飞行员具有不同的等级,且等级高的飞行员可以当等级低的飞行员的老师,且每个飞行员至多有且只有一个老师和学生.具有老师和学生关系的飞行员可以在同一把扫 ...
- Splay教程
目录 前言 引入 教程 Rotate Splay 一些其他操作: 区间翻转 结语 前言 Splay是名副其实的区间小能手.它会经常出现在一些有关区间的题上.而本蒟蒻只会Treap,感到分外难受,于是就 ...