向请教,Spring 使用 cglib动态代理问题

J2EE 码拜 8年前 (2016-03-20) 1687次浏览
本人要使用spring的AOP做操作日志功能,想代理springmvc的controller
假如使用jdk自带的代理,目标类就必须实现某个接口,对controller不适用。
假如使用cglib,由于本人数据访问层使用的是mybatis,自动生成的mapper,没有默认的构造方法,cglib要求代理的目标类要有默认的构造方法,启动就报错了。
问一下高手们有碰到这种问题吗,能不能给个解决思路。
<aop:aspectj-autoproxy proxy-target-class=”true”/>,加上红色部分,就是使用cglib代理了,否则使用jdk的默认代理。
报错内容如下:
Error creating bean with name “userMapper”: Post-processing of the FactoryBean”s object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy22]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy22
解决方案

20

proxy-target-class=”true” 是强制使用CGLIB生成代理
去掉这个试试呢?

20

引用:
Quote: 引用:

mapper 是什么? controller么? 配置发出来看下

mapper:

public interface UserMapper extends IBaseMapper<UserExample, User, Integer> {
}

IBaseMapper:

package com.szyungu.ecommunity.base.service;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface IBaseMapper<C,R,PK> {

	int countByCondition(C condition);

	int insert(R record);
	int insertSelective(R record);

    int updateByPrimaryKeySelective(R record);
	int updateByPrimaryKey(R record);
    
    int updateByConditionSelective(@Param("record") R record, @Param("example") C condition);
    int updateByCondition(@Param("record") R record, @Param("example") C condition);
    int deleteByCondition(C condition);
    int deleteByPrimaryKey(PK id);
    
    R selectByPrimaryKey(PK id);
    List<R> selectByCondition(C condition);   
}

controller:
就是普通的spring mvc的controller

那你这个userMapper在代理中是什么角色呢? 就是他跟controller什么关系?
你这错误:Error creating bean with name “userMapper”: Post-
没搞清楚你是要代理什么?  配置发出来看看


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明向请教,Spring 使用 cglib动态代理问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)