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 ...
随机推荐
- android app 闪屏
main activity package com.splash.screen; import android.app.Activity; import android.content.Intent; ...
- 标准C语言(5)
无法预知的数字叫随机数,rand标准函数可以用来获得随机数,为了使用这个标准函数需要包含stdlib.h头文件 srand标准函数用来设置随机数种子,这个函数把一个整数作为种子使用不同的种子可以得到不 ...
- js 中dindexof()用法
https://www.cnblogs.com/jiadi321/p/10174317.html http://www.w3school.com.cn/jsref/jsref_splice.asp
- linux下使用 TC 对服务器进行流量控制
tc 介绍 在linux中,tc 有二种控制方法 CBQ 和 HTB.HTB 是设计用来替换 CBQ 的.HTB比CBQ更加灵活,但是CPU 开销也更大,通常高速的链路会使用CBQ,一般而言HTB使用 ...
- fedora 29 安装ALSA声音驱动
centos系列解决 安装utils时遇到的问题 configure: error: this packages requires a curses library yum install ncurs ...
- webpack打包,同时将ES6转为ES5,初探
webpack打包,同时将ES6转为ES5,第一次尝试搞了一下午才弄好,所有的问题均来自ES6转es5上面,可能天分不够把,但愿各大浏览器快点支持ES6吧!忽略nodejs安装. 第一,新建一个项目文 ...
- 用CSS创建分页的实例
总结介绍如何通过使用 CSS 来创建分页的实例. ㈠简单分页 如果你的网站有很多个页面,你就需要使用分页来为每个页面做导航. 以下实例演示了如何使用 HTML 和 CSS 来创建分页: <!DO ...
- 15、Qt 样式表
Qt的样式表类似HTML的层叠式样式表CSS,可以为一个独立的子部件.整个窗口.整个应用程序指定一种外表样式. 样式表功能:1.优化外观.2.实现某些动作,如鼠标在图片上,图片切换. 格式:QWidg ...
- (转)window.open和window.showModalDialog的区别
window.open和window.showModalDialog区别: 1.都是在IE上打开新窗口,只不过前者是非阻塞式,也可以说非模态窗口.而后者是阻塞式模态窗口.阻塞或者模态窗口,只有你把当前 ...
- CGI环境变量
所有的CGI程序都接收以下的环境变量,这些变量在CGI程序中发挥了重要的作用: 变量名 描述 CONTENT_TYPE 这个环境变量的值指示所传递来的信息的MIME类型.目前,环境变量CONTENT_ ...