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 ...
随机推荐
- python的isocalender()
isocalender()返回指定日期的年,第几周,周几这三个值. 例子: import date date_time = datetime.date(2019, 5, 9) ret = date_t ...
- 设计数据结构之LRU缓存
leetcode 146. LRU Cache class LRUCache { private: struct Node { int key; int val; Node* prev; Node* ...
- HTTP 请求出现 405 not allowed 的一种解决办法
问题:http post请求网页会出现405 原因: Apache.IIS.Nginx等绝大多数web服务器,都不允许静态文件响应POST请求 解决:将post请求改为get请求
- HTML中的超链接(Hyperlink)
超链接 ★超链接简单介绍 超链接可以说是网页中最常见的元素.超链接的英文名是hyperlink.每个网站都是由众多的网页组成,网页之间通常都是通过链接的方式相互关联的. 超链接能够让浏览者在各个独立的 ...
- C# 2.0
序言 泛型 为什么需要泛型? 分部类型 分部类和方法 partial 匿名方法 使用Delegate的时候很多时候没必要使用一个普通的方法,因为这个方法只有这个Delegate会用,并且只用一次,这时 ...
- springboot运行jar包时候加载指定目录的其他jar支持包
最近发生一个小故障,调试好的项目,发布成jar包后无法找到oracle的驱动,研究了一下解决了.记录一下.写了一个run.sh脚本 #!/bin/bash cd ~ cd app nohup java ...
- 16位masm汇编实现记忆化递归搜索斐波那契数列第50项
.model small ;递归fib,使用压缩BCD码,小端派 .data y1 byte 6 dup(0) y2 byte 6 dup(0) vis byte 1,1,1,61 dup(0) ;便 ...
- 论文阅读:Camdoop: Exploiting In-network Aggregation for Big Data Applications
摘要: 大公司与中小型企业每天都在批处理作业和实时应用程序中处理大量数据,这会产生大量的网络流量,而使用传统的的网络基础架构则很难支持.为了解决这个问题已经提出了几种新颖的网络拓扑,旨在增加企业集群中 ...
- Unity3D_(API)场景切换SceneManager
Unity场景切换SceneManager 官方文档:传送门 静态方法 创建场景 CreateScene Create an empty new Scene at runtime with the g ...
- java 多线程相关概念总结
前言 本篇文章介绍一些多线程的相关的深入概念.理解后对于线程的安全性会有更深的理解. 先说一个格言,摘自Java核心技术:如果向一个变量写入值,而这个变量接下来可能会被另一个线程读取:或者一个变量读值 ...