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无效。