spring 动态代理Bean named 'userService' is expected to be of type 'cn.msg.service.impl.UserServiceImpl'

这个异常错了一下午。。。。。

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 
'userService' is expected to be of type 'cn.msg.service.impl.UserServiceImpl' 
but was actually of type 'com.sun.proxy.$Proxy7'

最后终于在我的知识盲区中找到了解决方案,然后又了解了一下
这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体是由于spring采用代理的机制导致的,看使用的代码:
在这里插入图片描述
看代码也没什么问题但是关于动态代理就有问题了:
默认动态代理是jdk动态代理,而jdk动态代理不支持类注入也就是依赖注入的对象不能是类,只能是接口
解决方法:proxy-target-class
proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。首先说明下proxy-target-class="true"和proxy-target-class="false"的区别,为true则是基于类的代理将起作用(需要cglib库),为false或者省略这个属性,则标准的JDK 基于接口的代理将起作用。
所以遇到这种问题可以在以下标签中根据需求设置代理:

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<aop:config proxy-target-class="true">
<cache:annotation-driven proxy-target-class="true"/>
  • 11
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
文件夹 PATH 列表 卷序列号为 4E8D-6931 C:. │ .txt │ assess.zip │ └─assess │ assess.sql │ pom.xml │ ├─.idea │ │ assess.eml │ │ assess.iml │ │ compiler.xml │ │ encodings.xml │ │ misc.xml │ │ modules.xml │ │ uiDesigner.xml │ │ webContexts.xml │ │ workspace.xml │ │ │ ├─artifacts │ │ assess_war.xml │ │ assess_war_exploded.xml │ │ │ └─libraries │ Maven__javax_servlet_javax_servlet_api_4_0_1.xml │ Maven__javax_servlet_jstl_1_2.xml │ Maven__mysql_mysql_connector_java_5_1_38.xml │ ├─src │ └─main │ ├─java │ │ └─com │ │ └─demo │ │ ├─bean │ │ │ UserBean.java │ │ │ │ │ ├─dao │ │ │ JdbcUtils.java │ │ │ │ │ ├─service │ │ │ UserService.java │ │ │ │ │ └─web │ │ └─controller │ │ LoginController.java │ │ UserController.java │ │ │ ├─resources │ └─webapp │ │ add.jsp │ │ index.jsp │ │ login.jsp │ │ modify.jsp │ │ │ ├─assets │ │ └─bootstrap-3.3.7 │ │ ├─css │ │ │ bootstrap-theme.css │ │ │ bootstrap-theme.css.map │ │ │ bootstrap-theme.min.css │ │ │ bootstrap-theme.min.css.map │ │ │ bootstrap.css │ │ │ bootstrap.css.map │ │ │ bootstrap.min.css │ │ │ bootstrap.min.css.map │ │ │ │ │ ├─fonts │ │ │ glyphicons-halflings-regular.eot │ │ │ glyphicons-halflings-regular.svg │ │ │ glyphicons-halflings-regular.ttf │ │ │ glyphicons-halflings-regular.woff │ │ │ glyphicons-halflings-regular.woff2 │ │ │ │ │ └─js │ │ bootstrap.js │ │ bootstrap.min.js │ │ npm.js │ │ │ ├─images │ │ background.png │ │ │ └─WEB-INF │ web.xml │ └─target ├─assess-1.0.0-SNAPSHOT │ │ add.jsp │ │ index.jsp │ │ login.jsp │ │ modify.jsp │ │ │ ├─assets │ │ └─bootstrap-3.3.7 │ │ ├─css │ │ │ bootstrap-theme.css │ │ │ bootstrap-theme.css.map │ │ │ bootstrap-theme.min.css │ │ │ bootstrap-theme.min.css.map │ │ │ bootstrap.css │ │ │ bootstrap.css.map │ │ │ bootstrap.min.css │ │ │ bootstrap.min.css.map │ │ │ │ │ ├─fonts │ │ │ glyphicons-halflings-regular.eot │ │ │ glyphicons-halflings-regular.svg │ │ │ glyphicons-halflings-regular.ttf │ │ │ glyphicons-halflings-regular.woff │ │ │ glyphicons-halflings-regular.woff2 │ │ │ │ │ └─js │ │ bootstrap.js │ │ bootstrap.min.js │ │ npm.js │ │ │ ├─images │ │ background.png │ │ │ ├─META-INF │ │ MANIFEST.MF │ │ │ └─WEB-INF │ │ web.xml │ │ │ ├─classes │ │ └─com │ │ └─demo │ │ ├─bean │ │ │ UserBean.class │ │ │ │ │ ├─dao │ │ │ JdbcUtils.class │ │ │ │ │ ├─service │ │ │ UserService.class │ │ │ │ │ └─web │ │ └─controller │ │ LoginController.class │ │ UserController.class │ │ │ └─lib │ jstl-1.2.jar │ mysql-connector-java-5.1.38.jar │ ├─classes │ └─com │ └─demo │ ├─bean │ │ UserBean.class │ │ │ ├─dao │ │ JdbcUtils.class │ │ │ ├─service │ │ UserService.class │ │ │ └─web │ └─controller │ LoginController.class │ UserController.class │ └─generated-sources └─annotations

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值