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 ...
随机推荐
- double to long
obj to double obj to long instance of Bigdecimal public static void main(String[] args) throws Parse ...
- fwrite()
fwrite(),最好写strlen()个字节,否则可能有乱码
- CI/CD----jenkins安装配置
1.下载jenkins rpm包. https://pkg.jenkins.io/redhat/ 2.安装 rpm -ivh jenkins-2.182-1.1.noarch systemctl st ...
- 遗传算法解决寻路问题——Python描述
概要 我的上一篇写遗传算法解决排序问题,当中思想借鉴了遗传算法解决TSP问题,本质上可以认为这是一类问题,就是这样认为:寻找到一个序列X,使F(X)最大. 详解介绍 排序问题:寻找一个序列,使得这个序 ...
- Lambda学习总结(三)--方法引用
一.方法引用 1.1 方法引用含义 在学习了 Lambda 表达式之后,我们通常会使用 Lambda 表达式来创建匿名方法.但有的时候我们仅仅是需要调用一个已存在的方法.如下示例: @Function ...
- 树形结构根据最后一位的id匹配整个路径
function recursionTreeId(_arr, _id) { _arr.forEach(item => { if (item.id === _id) { optionArr.uns ...
- IPC 进程间通信方式——共享内存
共享内存 共享内存区域是被多个进程共享的一部分物理内存. 多个进程都可以把共享内存映射到自己的虚拟空间.所有用户空间的进程要操作共享内存,都要将其映射到自己的虚拟空间,通过映射的虚拟内存空间地址去操作 ...
- 初学者的springmvc笔记02
springmvc笔记 springmvc拦截器,spring类型转换,spring实现文件上传/下载 1.SpringMVC标准配置 导入jar包:core contaner 在web.xml文件中 ...
- 论文阅读:Fast, Scalable, and Programmable Packet Scheduler in Hardware
摘要: 随着链接速度的提高和CPU扩展速度的放缓,软件中的数据包调度会导致较低的精度和较高的CPU利用率. 通过将数据包调度卸载到诸如NIC之类的硬件,可以潜在地克服这些缺点.然而为了保持软件分组调度 ...
- week5 作业
week5 作业 1.描述GPT是什么,应该怎么使用? 描述GPT之前要简单了解MBR分区,MBR(Main Boot Record)叫做主引导记录,其位于磁盘的最前端,由一段代码组成,共占用512个 ...