动态代理是基于接口的,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通知时,代理出错的更多相关文章

  1. Spring通知类型及使用ProxyFactoryBean创建AOP代理

    Spring 通知类型 通过前面的学习可以知道,通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Ad ...

  2. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring通知类型及使用ProxyFactoryBean创建AOP代理

    通知(Advice)其实就是对目标切入点进行增强的内容,Spring AOP 为通知(Advice)提供了 org.aopalliance.aop.Advice 接口. Spring 通知按照在目标类 ...

  3. 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  4. 使用BeanNameAutoProxyCreator实现spring的自动代理

    提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置 ...

  5. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  6. iOS开发——UI进阶篇(五)通知、代理、kvo的应用和对比,购物车

    一.通知 1.通知中心(NSNotificationCenter)每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信任何一个对象都可以向 ...

  7. spring设计模式_代理模式

    代理模式应该是Spring核心设计模式之一了 先说下代理模式特性: 1.有代理人和被代理人 2.对于被代理的人来说,这件事情是一定要做的,但是我又不想做,所有就找代理人来做. 3.需要获取到被代理人的 ...

  8. Spring中的代理模式

    代理模式 所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 代理模式是一种 ...

  9. spring AOP 动态代理和静态代理以及事务

    AOP(Aspect Oriented Programming),即面向切面编程 AOP技术,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装 ...

随机推荐

  1. REGEX.C GNU 提取过滤数据

    今天被@SVCHAO  勾起兴趣来了.. 有把正则表达式兴趣捡起来了,试了下notepad++基本上语法倒是没有忘记,不过如果是用在嵌入式的方案的话,似乎还是有点费劲的. 先mark一个基础语法. 单 ...

  2. 修改 C:\Users\[account name] 目录名称

    起因: 修改了用户名(第二个用户,标准用户,从 控制面板——用户账户 修改),后来发现 C:\Users\ 下的文件夹名称未变. 修改了 3 处: 1. 计算机——管理——本地用户和组——用户 2. ...

  3. mysql if case条件更新

    在mysql中,如果你要根据某个字段的值不一样,来更新另一个字段的值,可以用如下sql语句: 如果仅仅是两个分支,if语句就可以了 update tm set page_name=if(q_aswer ...

  4. PHP 数组转码

    /** * 数组转码 * @param array $arr 要转码的数组 * @param string $in_charset 输入的字符集 * @param string $out_charse ...

  5. iPhone4下window各个部分的高度

  6. 有wsdl地址生成客户端调用

    新建Java Project 1.项目名称:HelloWorldClient 2.建好的项目 3.生成客户端 选择HelloWorldClient 填写wsdl地址(参考:myeclipse构建web ...

  7. rake :You have already activated rake 10.1.0

    rake aborted! You have already activated rake 10.1.0, but your Gemfile requires rake 10.0.3. Using b ...

  8. Maven项目java.lang.NoClassDefFoundError: Lorg/apache/log4j/Logger报错

    本文转载自:http://www.javaweb1024.com/info/894.jspx maven管理的项目,里面已经引入了log4j的包 maven引入如下: <dependency&g ...

  9. 将 Shiro 作为应用的权限基础

    Shiro 是 Java 世界中新近出现的权限框架,较之 JAAS 和 Spring Security,Shiro 在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势.本文介绍了 Shiro 的 ...

  10. C# WinForm 禁止最大化、最小化、双击标题栏、双击图标等操作(转载)

    protected override void WndProc(ref Message m) { if (m.Msg==0x112) { switch ((int) m.WParam) { //禁止双 ...