Spring 异常 Couldn't generate CGLIB subclass of class [class spring.dao.UserDAO]: Common causes of this problem include using a f

来源:互联网 发布:现代远程网络教育 编辑:程序博客网 时间:2024/05/16 08:35

Couldn't generate CGLIB subclass of class [class spring.dao.UserDAO]: Common causes of this problem include using a final class or a non-visible class;

我的一部分配置文件如下:

<!-- 配置UserDAO -->
<bean id="userDAO" class="spring.dao.UserDAO">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>


<bean id="UserDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="userDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED </prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly </prop>
</props>
</property>
</bean>


ERROR - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDAOProxy' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Couldn't generate CGLIB subclass of class [class spring.dao.UserDAO]: Common causes of this problem include using a final class or a non-visible class; nested exception is net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
org.springframework.aop.framework.AopConfigException: Couldn't generate CGLIB subclass of class [class spring.dao.UserDAO]: Common causes of this problem include using a final class or a non-visible class; nested exception is net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:237)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:189)

 

 

解决办法:

 

这个问题是因为使用了代理bean,

在spring中有两种代理CGLIB和JDK代理,理论上来说使用CGLIB可以直接对类进行代理,可是我也没有调出来。(外国一些网站上说是这个包本身有问题,但还是不太明白)

只好使用JDK来实现代理,这样,要代理的目标类必须实现一个接口。然后通过该接口来实现代理。 

 

也就是说为要实现代理的类写一个接口IUserDAO,并且在配置文件的

<bean id="UserDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
中加入

<property name="proxyInterfaces">
                 <value>spring.dao.IUserDAO</value>
</property>

 

这样就使用了JDK的代理。也就没有了这个异常,不过……

 

不过还要再改一个地方不然会出下面这个异常,转换异常,因为现在使用的是接口,而原来是对类的操作,当然报这个异常了。

(Web 项目会出这个异常)Failed to convert property value of type [java.lang.String] to required type [java.lang.Class[]] for property 'proxyInterfaces'; nested exception is java.lang.IllegalArgumentException: Cannot find class [IUserDAO]. Root cause: java.lang.ClassNotFoundException: IUserDAO

 

(Java Project 会出这个异常) java.lang.ClassCastException: $Proxy0

 

要在使用这个UserDAOProxy的地方(也就是声明UserDAO的set方法的地方),用接口来new 这个类。如下

IUserDAO userDAO = new UserDAO();

 

这样就彻底解决这个问题了。

 

在此要感谢江楼月(网易博客)的指导啊。呵呵

还有流火,

还有许多人

原创粉丝点击