Spring 异常 Couldn't generate CGLIB subclass of class [class spring.dao.UserDAO]: Common causes of this problem include using a f
Couldnt generate CGLIB subclass of class [class spring.dao.UserDAO]: Common causes of this problem include using a final class or a non-visible class; 我的一部分配置文件如下:class="org.springframework
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();
这样就彻底解决这个问题了。
在此要感谢江楼月(网易博客)的指导啊。呵呵
还有流火,
还有许多人
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)