最近在用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. shell基础入门(一)

    //获取输入内容 #!/bin/bash echo "What is your name?" read PERSON read -p "who are you name: ...

  2. pipenv 方便的python 开发工作流工具

    pipenv 将 composer.bundler.npm.yarn.cargo 等比较方便的包管理工具添加到了python 语言中,可以 帮助我们自动的管理virtualenv ,同时可以方便的从p ...

  3. MySQL中正则表达式

    正则表达式是用来匹配文本的特殊的串(字符集合),将一个模式(正则表达式)与一个文本串进行比较 从文本文件中提取电话号码 查找名字中间带有数字的文件 文本块中重复出现的单词 替换页面的URL为这些URL ...

  4. 22 初始模块 random time collections functools

    一 .初始模块 1.从⼩到⼤的顺序: ⼀条代码 < 语句块 < 代码块(函数, 类) < 模块 2.引入模块的方式 ①   import   模块 ②   from   模块  im ...

  5. JAVA_HOME 设置为JDK 7无效

    场景: Window 10中有Jdk 7和jdk8, 还有Oracle配置 环境变量的配置如下 然后JAVA_HOME 设置为java 7 但是java -version显示为java 8 解决方法 ...

  6. 获取 TUniConnection.SpecificOptions默认值和下拉框列表值

    TUniConnection的SpecificOptions参数决定了数据库连接配置参数,但可惜的是,SpecificOptions设计器界面,Devart公司只能让它在设计期配置! Specific ...

  7. 在本机将本机的ip和mac绑定

    cmd命令框中输入arp -s ip mac即可绑定 解除绑定:arp -d ip

  8. sql语句事务

    set xact_abort on begin tran ... commit tran

  9. linux找不到动态链接库 .so文件的解决方法

    linux找不到动态链接库 .so文件的解决方法 如果使用自己手动生成的动态链接库.so文件,但是这个.so文件,没有加入库文件搜索路劲中,程序运行时可能会出现找不到动态链接库的情形. 可以通过ldd ...

  10. Linux Bash on Win10 (WSL)在cmder下使用vim时方向键失灵问题解决

    更改方法 由于cmder和bash.exe不兼容,如果你直接输入bash ~,那么进入子系统后将无法使用方向键和Home/PageUp/PageDown等键都无法使用,网上常见的cmder配置过程如下 ...