使用spring通知时,代理出错
动态代理是基于接口的,spring配置是基于类的!!!!!!!!!!
注意:JDK的动态代理,只能对实现接口的类实现代理,生成代理对象,如果这个类没有实现接口,是生成不了代理对象的。如本例UserManagerImpl实现了UserManager,如果去掉了UserManager接口,会出现异常(找不到cglib库)。
要解决这个问题,要添加cglib库,即:/spring_home/cglib/cglib-nodep-2.1_3.jar
如果目标对象实现了接口,默认情况下会使用JDK的动态代理实现AOP;如果有些目标对象没有实现接口,而有些又实现了,那么框架会在jdk动态代理和cglib直接灵活切换,实现了接口的使用动态代理,没有实现接口的采用cglib,因此较为保险的做法是总是引入cglib库。
如下面的代码, Spring注入的是接口,关联的是实现类。 这里注入了实现类,所以报异常了。 (出现在事务处理过程中)
<bean id="aa" class="com.jdbcdemo.util.HibernateUtil"></bean>
 <bean id="ld" class="com.jdbcdemo.dao.impl.LogDaoImpl">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <bean id="userDao" class="com.jdbcdemo.dao.impl.UserDaoImpl">
  <property name="sessionFactory" ref="sessionFactory"></property>
  <property name="logDaoImpl" ref="ld"></property>
 </bean>
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [E:\JavaProj\Hibernate_Spring_Study\WebRoot\WEB-INF\classes\applicationContext_beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0 implementing com.jdbcdemo.dao.LogDao,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.jdbcdemo.dao.impl.LogDaoImpl] for property 'logDaoImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0 implementing com.jdbcdemo.dao.LogDao,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.jdbcdemo.dao.impl.LogDaoImpl] for property 'logDaoImpl': no matching editors or conversion strategy found 
______________________________________________________________________________________________
解决办法1, <aop:config proxy-target-class="true"> ,表示可以用类注入:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 <tx:annotation-driven transaction-manager="transactionManager" />
 <tx:advice id="txAdvice"  >
  <tx:attributes>
   <tx:method name="add"/>
  </tx:attributes>
 </tx:advice>
<aop:config proxy-target-class="true"> 
  <aop:pointcut expression="execution(* com.jdbcdemo.dao.impl.*.*(..))" id="allManagerMethod"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
 </aop:config>
</beans>
解决办法2,在tx:annotation-driven中表示可以用类注入:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"proxy-target-class="true"/>
<tx:advice id="txAdvice" >
<tx:attributes>
<tx:method name="add"/>
</tx:attributes>
</tx:advice>
<aop:config >
<aop:pointcut expression="execution(* com.jdbcdemo.dao.impl.*.*(..))"id="allManagerMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>
使用spring通知时,代理出错的更多相关文章
- Spring通知类型及使用ProxyFactoryBean创建AOP代理
		
Spring 通知类型 通过前面的学习可以知道,通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Ad ...
 - 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理
		
通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...
 - 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器
		
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
 - 使用BeanNameAutoProxyCreator实现spring的自动代理
		
提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置 ...
 - 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象
		
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
 - iOS开发——UI进阶篇(五)通知、代理、kvo的应用和对比,购物车
		
一.通知 1.通知中心(NSNotificationCenter)每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信任何一个对象都可以向 ...
 - spring设计模式_代理模式
		
代理模式应该是Spring核心设计模式之一了 先说下代理模式特性: 1.有代理人和被代理人 2.对于被代理的人来说,这件事情是一定要做的,但是我又不想做,所有就找代理人来做. 3.需要获取到被代理人的 ...
 - Spring中的代理模式
		
代理模式 所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 代理模式是一种 ...
 - spring AOP 动态代理和静态代理以及事务
		
AOP(Aspect Oriented Programming),即面向切面编程 AOP技术,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装 ...
 
随机推荐
- Python使用re模块正则式的预编译及pickle方案
			
项目上线要求当中有言论和昵称的过滤需求, 客户端使用的是python脚本, python脚本中直接利用re模块来进行正则匹配, 一开始的做法是开启游戏后, 每帧编译2条正则式, 无奈运营需求里面100 ...
 - Tomcat部署Solr4.10.4
			
前段时间学习solr,兴致勃勃的从官网下载到solr5.3.0最新版本,然后在后期部署时出现了很多问题.首先,4.0到5.0是个大版本更新,下载 的压缩包的文件结构有了很多变化,导致网上很多关于sol ...
 - sublime text2在windows中以命令行启动
			
sublime text2在windows中以命令行启动 把执行文件添加到PATH中即可,如图: 如果你和我一样习惯了mac下的简写subl,那么需要在程序目录中新建一个批处理文件subl.bat ...
 - 【学】React的学习之旅2 - React Component的生命周期
			
分成三个状态: Mounted Update Unmounted Mounted:当我们看到组件在浏览器中从无到有的效果的时候,mounted已经结束了,这个组件已经被mounted了 有这个阶段有2 ...
 - Web性能测试工具JMeter
			
做Web方面的黑盒测试,也就是功能测试,基本不需要什么测试工具,都是直接打开浏览器访问,点一点界面就行. 现在流行的移动互联网应用,客户端和服务端的开发是分离的,两者开发进度肯定不一样,可能存在服务端 ...
 - mfc release 版本 内存不足 的解决方法
 - jQuery选择器大全
			
1. id选择器(指定id元素) 将id="one"的元素背景色设置为黑色.(id选择器返单个元素) $(document).ready(function () { $('#one ...
 - MFC ADO连接Sql Server数据库报无效指针的问题
			
相关症状: Win7sp1上编译的ADO程序无法在低版本系统上运行,创建ADO时提示错误:0x80004002 解决办法如下: 1.下载: http://download.microsoft.c ...
 - 工具04_SQL Trace/DBMS_SYSTEM
			
2014-06-25 Created By BaoXinjian
 - 用openvswitch配置跨节点的docker网络环境
			
在一篇随笔中,我们已经尝试了在不依赖工具的情况下设置docker的ip,连我都想吐槽,MD单机都这么麻烦,在多机的环境中岂不是要了我的小命! 本文就是为了多机环境中各个节点的容器通信而做的,网络拓朴如 ...