使用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>

时间: 2024-08-28 00:50:37

使用spring通知时,代理出错的相关文章

使用BeanNameAutoProxyCreator实现spring的自动代理

提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置文件的编写带来繁重的工作 Spring为我们提供了,根据beanName匹配后进行自动代理的解决方法 业务接口 package AutoProxyOne;public interface Shopping {  public String buySomething(String type);  pu

spring通知的注解

1.代理类接口Person.java 1 package com.xiaostudy; 2 3 /** 4 * @desc 被代理类接口 5 * 6 * @author xiaostudy 7 * 8 */ 9 public interface Person { 10 11 public void add(); 12 public void update(); 13 public void delete(); 14 } 2.代理类PersonImple.java 1 package com.xi

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

一.通知 1.通知中心(NSNotificationCenter)每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么.其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知 2.通知(NSNotification)一个完整的通知一般包含3个属性:- (NSString *)name; // 通知

通知和代理

一个完整的通知一般包含3个属性: - (NSString *)name; // 通知的名称 - (id)object; // 通知发布者(是谁要发布通知) - (NSDictionary *)userInfo; // 一些额外的信息(通知发布者传递给通知接收者的信息内容) 初始化一个通知(NSNotification)对象 + (instancetype)notificationWithName:(NSString *)aName object:(id)anObject; + (instance

spring autowired时发生异常情况

spring beanFactory那些就不说了,这次发生这个异常纠结好了好久,网上找了很多资料看,终于发现问题. 自动装配bean注入的时候,如果Spring配置定义了aop声明式事务,类似如下方式 <aop:config>  <aop:pointcut id="serviceMethods2"   expression="execution(public * net.villion.framework..*(..))" />  <a

5、通知和代理区别和使用

一.代理Delegate 1.使用的场合 主要用于子控件发生某些动作时,通知父控件,子控件的代理是父控件.常见就控制器就是子控件的的代理. 2.代理的使用步骤 1> 先搞清除谁是谁的代理 2> 定义代理协议,协议名称的规范是:控件类名+Delegate 3> 定义代理方法 *代理方法一般都定义@optional *代理方法名都以控件名开头 *代理方法至少一个参数,将控件本身传递出去 4> 设置代理对象 *代理对象遵守协议 *代理对象实现协议里面的该实现的方法 5> 在恰当的时

ios开发——实用技术篇&amp;Block/KVO/通知/代理

Block/KVO/通知/代理简单介绍与使用 关于iOS开发中数据传递的方法有很多种,但是使用最多的就是这里的四种,而且我们要学会在适当的时候使用合适的方式,才能充分的提高app的性能 下面简单介绍一下这些方法的使用 Block 第一.综述 block是OC中另外一种对象和对象的通信方式,是一对一的关系,类似于delegate,而通知时一对多的关系 第二.定义block类型 int (^myBlock)(int) 第三.block的声明 mylock=^(int a) { int result

SPring+Structs2实现的项目中进行Spring AOP时的相关小记

 SPring+Structs2实现的项目中进行Spring AOP时的相关小记 1.一般为了方便开发Structs2的项目中的action都会建立一个BaseAction如果继承了BaseAction中的子类进行AOP时,只能指定AOP中的PointCut为BaseAction 如果对应的BaseAction如果继承于ActionSupport的话,就只能定义AOP中的PointCut为ActionSupport了 因为Spring生成的代理类中,对同名的方法,只有一个,即子类重写父类的方

页面之间传值方式的总结,五种方式,通知,block,代理,单例,NSUERDEFALUT,

首先代码拿上 1:单例 2:通知 3:代理 4:block方法 5:NSUSERDEFAULT(沙盒文件) 先是单例类: .h文件 @interface DataSource : NSObject @property (nonatomic, strong) NSString *myName;//单例的属性,用于传值 +(DataSource*)sharedDataSource;//建立单例对象 @end .m文件 #import "DataSource.h" @implementati