最近在用springboot搭建项目框架时,遇到了如下错误,查询了一番,原来是没有引入spring框架的aop包导致:

问题:

 ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@2e3252] to prepare test instance [com.zsm.sb.service.impl.StudentServiceImpTest@d978ab9]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.zsm.sb.service.impl.StudentServiceImpTest': Unsatisfied dependency expressed through field 'studentServiceImp'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'studentServiceImpl' is expected to be of type 'com.zsm.sb.service.impl.StudentServiceImpl' but was actually of type 'com.sun.proxy.$Proxy101'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$.runReflectiveCall(SpringJUnit4ClassRunner.java:)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:)
at org.junit.runners.ParentRunner$.run(ParentRunner.java:)
at org.junit.runners.ParentRunner$.schedule(ParentRunner.java:)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:)
at org.junit.runners.ParentRunner.access$(ParentRunner.java:)
at org.junit.runners.ParentRunner$.evaluate(ParentRunner.java:)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:)
at org.junit.runners.ParentRunner.run(ParentRunner.java:)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:)
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'studentServiceImpl' is expected to be of type 'com.zsm.sb.service.impl.StudentServiceImpl' but was actually of type 'com.sun.proxy.$Proxy101'
at org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType(DefaultListableBeanFactory.java:)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:)
... common frames omitted

解决方案:

1.添加AOP依赖包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.配置参数:

 spring.aop.proxy-target-class=true 
<aop:aspectj-autoproxy  proxy-target-class="true"/>

  spring对AOP有两种实现方式,一种是动态代理,它是通过接口方式实现的,要求所代理的类一定是实现了某一个接口,对一般的类就无法代理,spring默认是这种;一种是通过设置proxy-target-class="true",则是使用CGLIB实现AOP,CGLIB直接生成二进制码,使得普通类也可以实现AOP。

but was actually of type 'com.sun.proxy.$Proxy101' 注入问题的更多相关文章

  1. 使用openoffice将word文件转换为pdf格式遇到问题:The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from required

    The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from requi ...

  2. Spring错误之org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookService' is expected to be of type 'pw.fengya.tx.BookService' but was actually of type 'com.sun.proxy.$Proxy1

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cas ...

  3. Bean named '...' is expected to be of type [...] but was actually of type [com.sun.proxy.$Proxy7解决方法

    报错 三月 07, 2017 8:09:52 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepare ...

  4. Springboot2.x 启动报错:Bean named 'xxxService'... but was actually of type 'com.sun.proxy.$Proxy82'

    Springboot 2.0.5 搭建一个新项目启动后报错:Bean named 'xxxService'... but was actually of type 'com.sun.proxy.$Pr ...

  5. Spring 动态代理 之 but was actually of type 'com.sun.proxy.$Proxy14 Exception

    今天在写Spring的引介代理的时候,报了一个错: Exception in thread "main" org.springframework.beans.factory.Bea ...

  6. jmap错误:unknown CollectedHeap type : class sun.jvm.hotspot.gc_interface.CollectedHeap

    原文:https://www.jianshu.com/p/0e5d8db8ed5e 错误场景 今天使用jmap -heap命令查看堆的详细信息报错. [root@instance-2gak1pfv d ...

  7. JAVA: 解决is expected to be of type but was actually of type com.sun.proxy.$Proxy的问题

    修改spring-config文件中的transactionManager部分为 <tx:annotation-driven proxy-target-class="true" ...

  8. Bean named 'XXX' is expected to be of type [XXX] but was actually of type [com.sun.proxy.$Proxy7

    AOP原理 <aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面. <aop:aspectj-aut ...

  9. JDK下sun.net.www.protocol.http.HttpURLConnection类-----Http客户端实现类的实现分析

    HttpClient类是进行TCP连接的实现类, package sun.net.www.http; import java.io.*; import java.net.*; import java. ...

随机推荐

  1. MySQL--数据超时相关参数

    =============================================== connect_timeout connect_timeout用在client和server之间建立连接 ...

  2. prime 又一个开源的基于graphql 的cms

    prime 是一个开源的基于graphql 的cms,类似的已经又好多了,strapi 就是一个(graphql 是通过插件扩展的) graphcms 是一款不错的,但是是收费的,prime 是基于t ...

  3. jquery禁用form表单中的文本框

    //禁用form表单中所有的input[文本框.复选框.单选框],select[下拉选],多行文本框[textarea] function disableForm(formId, isDisabled ...

  4. 论 数据库 B Tree 索引 在 固态硬盘 上 的 离散存储

    传统的做法 , 数据库 的 B Tree 索引 在 磁盘上是 顺序存储 的 , 这是考虑到 磁盘 机械读写 的 特性 . 实际上 , B Tree 是一个 树形结构 , 可以采用 链式 存储 , 就是 ...

  5. [转]JBoss7中domain、standalone模式介绍

    JBoss AS7 可实现为云做好准备的架构,并可使启动时间缩短十倍,提供更快的部署速度并降低内在的占用.JBoss Enterprise Application Platform 6 的核心是JBo ...

  6. mobx 知识点

    antd+mobx 项目例子:https://github.com/cag2050/antd_mobx_demo 在 create-react-app 创建的项目中,使用 mobx:https://s ...

  7. $("#form1"). serialize()提交表单

    一.jQuery ajax()使用serialize()提交form数据 jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直 ...

  8. 本地开发spark代码上传spark集群服务并运行

    打包 :右击.export.Java .jar File 把TestSpark.jar包上传到spark集群服务器的 spark_home下的myApp下: 提交spark任务: cd /usr/lo ...

  9. SpringMVC和Struts是线程安全的吗?为什么?

    线程不安全的.(其实我觉得回答为:存在线程安全问题 这样比较好点) 原因如下: 第一点,先理解为何线程不安全 1 struts1的action是单例的,所以存在线程安全问题(struts2是多例的,不 ...

  10. SD-SDI播出系统---使用GTX TX产生恢复时钟

    SD-SDI播出系统---使用GTX TX产生恢复时钟 1. 2.SD-SDI恢复时钟的生成