org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'accounts' must be of t

你的名字 2023-06-06 10:41 71阅读 0赞

描述:

  1. 在使用Spring AOP 时,启动服务抛出异常:
  2. org.springframework.beans.factory.BeanNotOfRequiredTypeException

解决方案:

  1. 该问题往往因代码中默认使用了JDK的动态代理注入而引起的,由于JDK的动态代理不支持类注入,只支持接口注入。
  2. 解决方法有3种:
  3. 1 修改代码,统一使用接口注入,不使用类注入;
  4. 2 使用CGLib代理;
  5. 3 SpringAOP配置中添加如下配置,使Spring代理目标类:
  6. <aop:config proxy-target-class="true">
  7. <?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10. xmlns:tx="http://www.springframework.org/schema/tx"
  11. xmlns:aop="http://www.springframework.org/schema/aop"
  12. xsi:schemaLocation="http://www.springframework.org/schema/beans
  13. http://www.springframework.org/schema/beans/spring-beans.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd
  16. http://www.springframework.org/schema/aop
  17. http://www.springframework.org/schema/aop/spring-aop.xsd">
  18. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  19. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  20. <property name="url" value="jdbc:mysql://localhost:3306/db_bank"></property>
  21. <property name="username" value="root"></property>
  22. <property name="password" value="root"></property>
  23. </bean>
  24. <!-- 获得工厂-->
  25. <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <property name="dataSource" ref="dataSource"></property>
  27. <property name="typeAliasesPackage" value="com.bjsxt.pojo"></property>
  28. </bean>
  29. <!-- 扫描mapper映射文件-->
  30. <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  31. <property name="sqlSessionFactoryBeanName" value="factory"></property>
  32. <property name="basePackage" value="com.bjsxt.mapper"></property>
  33. </bean>
  34. <bean id="accounts" class="com.bjsxt.service.imple.AccountServiceImple">
  35. <property name="accountMapper" ref="accountMapper"></property>
  36. </bean>
  37. <!-- -->
  38. <aop:config proxy-target-class="true"></aop:config>
  39. <!-- 创建事务对象-->
  40. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  41. <property name="dataSource" ref="dataSource"></property>
  42. </bean>
  43. <!-- 创建事务通知-->
  44. <tx:advice id="ad" transaction-manager="transactionManager">
  45. <tx:attributes>
  46. <tx:method name="transfer"/>
  47. </tx:attributes>
  48. </tx:advice>
  49. <!-- 配置事务切面-->
  50. <aop:config>
  51. <aop:pointcut id="txt" expression="execution(* com.bjsxt.service.imple.*.*(..))"/>
  52. <aop:advisor advice-ref="ad" pointcut-ref="txt"></aop:advisor>
  53. </aop:config>
  54. </beans>

发表评论

表情:
评论列表 (有 0 条评论,71人围观)

还没有评论,来说两句吧...

相关阅读