使用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技术,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装 ...
随机推荐
- java多线程基础
多线程基础 读书练习照猫画虎 package Threadtest; import java.util.Date; import java.util.concurrent.ArrayBlockingQ ...
- C++ exception
从没用过C++STL中的exception(异常类),在使用rapidxml,操作XML文件时,发现在一个抛出异常的错误.关注了下,就模范着做. 我也专门写了个函数来分配内存,如果发现分配不成功,就抛 ...
- 关于C语言的一些trick
很多东西已经记不起来了,想到一点写一点,碰到一点写一点,慢慢累积. 关于# #在宏定义中用于替换传入变量的字符,例如: #define whole_operation(n) do { printf( ...
- Pads怎么设置某一网络的线宽
在利用pads工具进行layout时,由于某一条网络可能会有很多条走线,而走线的宽度也相同,如果一条条设置,会很麻烦,所以pads中可以直接设置某一网络的线宽,避免繁琐的工作. 如下所示同一网络的走线 ...
- 浅谈pads的铜(灌铜)
在pads中,先按照<pads实战攻略与高速PCB设计>中所说分类,大面积的灌铜有三个重要的概念: (1)copper(铜箔,静态铜): (2)copper pour(覆铜,动态铜): ( ...
- Nginx中文域名配置
Nginx虚拟主机上绑定一个带中文域名,比如linuxeye.中国,浏览器不能跳转. why? 因为操作系统的核心都是英文组成,DNS服务器的解析也是由英文代码交换,所以DNS服务器上并不支持直接的中 ...
- 浅入浅出“服务器推送”之一:Comet简介
最近有个项目,其中有项需求要从服务器端主动向客户端推送数据,本以为很简单,但在实际做的过程中发现很棘手,并没有想象中的简单.从网上搜索学习,发现主流讲的还是Ajax的长轮询技术或者流技术,websoc ...
- UICollectionView的使用
感谢下面文章作者的无私奉献 基础用法: http://www.cnblogs.com/ios8/p/iOS-UICollectionView.html http://my.oschina.net/jo ...
- VR外包团队:VR和AR技术已经红得发紫
近6个月以来,VR和AR技术已经红得发紫. 不管是创业公司还是互联网巨头,如果不在VR领域有所涉猎,都不好意思跟外界打招呼.最近,阿里巴巴公布VR战略并推出了一条VR购物的视频,更是给业界打了满满一碗 ...
- mysql-bin引起mysql不能启动
日志提示无法找到mysql-bin.000005这个文件 原因是我把其删除了,而在mysql-bin.index文件中却还有mysql-bin.000005的记录,因此启动失败了 解决: cd /va ...