一、源码:
1、目标类接口:
package localhost.anno;
public interface TargetInterface {
public void save();
}
2、目标类实现
package localhost.anno;
import org.springframework.stereotype.Component;
@Component("annoTarget")
public class Target implements TargetInterface {
@Override
public void save() {
System.out.println("save running ……");
}
}
3、切面类
package localhost.anno;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component("myAspect")
@Aspect
public class MyAspect {
@Before("execution(* localhost.anno.*.*(..))")
public void before() {
System.out.println("前置增强……");
}
@AfterReturning("MyAspect.pointcut()")
public void afterReturning() {
System.out.println("后置增强方法…………");
}
@Pointcut("execution(* localhost.anno.*.*(..))")
public void pointcut() {
}
}
4、ApplicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:component-scan base-package="localhost.anno"/>
<aop:aspectj-autoproxy proxy-target-class="false"/>
</beans>
5、测试类
package test.annoconf;
import localhost.anno.Target;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class TestAnnoConf {
@Resource(name = "annoTarget")
private Target targetInterface;
@Test
public void aTest() {
if (targetInterface != null) {
targetInterface.save();
}
}
}
6、测试结果
Error creating bean with name 'test.annoconf.TestAnnoConf': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'annoTarget' is expected to be of type 'localhost.anno.Target' but was actually of type 'com.sun.proxy.$Proxy23'
7、原因:
由于在TestAnnoConf测试类中成员变量targetInterface声明的类型为Target目标对象类型,
且未在配置文件中声明前置使用CgLib动态代理,导致注入的数据类型错误
因为使用jdk的动态代理,就需要注入一个接口类型的变量
8、解决办法:
- 1、将测试类中成员变量targetInterface的数据类型更新为该目标类实现的接口类型
private TargetInterface targetInterface;
<aop:aspectj-autoproxy proxy-target-class="true"/>