Bean named 'target' is expected to be of type 'com.ming.aop.Target' but was actually of type 'com.sun.proxy.$Proxy16'报错处理

今天在做xml配置AOP遇到的报错 文件如下
1.pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>SpringAop</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>

</project>
2.Target.java 目标对象
package com.ming.aop;

public class Target implements TargetInterface{
public void save() {
System.out.printf("save running...");
}
}
3.TargetInterface.java
package com.ming.aop;

public interface TargetInterface {
public void save();
}
4.MyAspect.java 切面对象
package com.ming.aop;

public class MyAspect {
public void before(){
System.out.println("前置增强...");
}

}
5.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">
<!--目标对象-->
<bean id="target" class="com.ming.aop.Target"/>
<!--切面对象-->
<bean id="myAspect" class="com.ming.aop.MyAspect"/>
<!--告诉Spring框架哪些方法(切点)需要哪些增强(前置,后置...)-->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect" >
<aop:before method="before" pointcut="execution(public void com.ming.aop.Target.save())"></aop:before>
</aop:aspect>
</aop:config>

</beans>
6.SpringAopTest.java
package com.ming.Test;

import com.ming.aop.Target;
import com.ming.aop.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringAopTest {

/*今天在这里犯错了 导致我报错Bean named 'target' is expected to be of type 'com.ming.aop.Target' but was actually of type 'com.sun.proxy.$Proxy16'
我注入的是Target这个接口 我真的该死 还没记住
测谁就要写谁 要记住这里是注入接口而不是实现类!!!!!!!!!!!!
写代码 要细心 要明白每一步的作用 不要写得不明不白的*/
@Autowired
private TargetInterface target;

@Test
public void test1(){
target.save();
}


}


posted @ 2020-11-19 22:23  JAVA&up  阅读(337)  评论(0编辑  收藏  举报