双重代理接口时,Spring AspectJ失败:无法生成类的CGLIB子类 [英] Spring AspectJ fails when double-proxying interface: Could not generate CGLIB subclass of class

查看:726
本文介绍了双重代理接口时,Spring AspectJ失败:无法生成类的CGLIB子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring的<aop:aspectj-autoproxy />代理某些 JPA存储库接口./p>

但是,代理失败,并显示以下Cannot subclass final class class $Proxy80:

无法生成类[class $ Proxy80]的CGLIB子类:通用 导致此问题的原因包括使用最终类或不可见类 班级;嵌套异常是java.lang.IllegalArgumentException:无法 子类最终类$ Proxy80

作为错误和快速的google建议,-当代理目标是最终课程时,就会发生这种情况.但是,在此链中,没有类-只有接口. Spring在运行时生成所有实现.

这是失败的接口的定义:

public interface AuthorDAO extends
    CrossStoreJpaRepository<Author,Long>, CrossStoreQueryDslPredicateExecutor<Author> {

}

请注意,我正在使用spring的 QueryDslPredicateExecutor ,定义如下:

public interface CrossStoreJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {}
public interface CrossStoreQueryDslPredicateExecutor<T> extends QueryDslPredicateExecutor<T>{}

在其他地方,我为这些接口上的方法定义了自定义方面:

@Aspect
@Component
public class DocumentLoadingAspect extends AbstractDocumentAspect {

    @Around("execution(* com.mangofactory.crossstore.repository.CrossStore*.find*(..))")
    public Object loadCrossStoreEntity(ProceedingJoinPoint pjp) throws Throwable
    {
         // implementation omitted
    }

我已经确认是这些@Aspect定义通过删除它们并重新运行该应用程序而导致了问题.

是什么导致此错误?似乎是由于某种原因代理失败.

解决方案

我的猜测是,Spring数据JPA将回购实现创建为Java代理(最终),然后<aop:aspectj-autoproxy />尝试使用cglib子类化,根据您的方面创建另一个代理,该代理将不起作用.在自动代理元素上proxy-target-class是否设置为true?

I'm using Spring's <aop:aspectj-autoproxy /> to proxy some JPA repository interfaces.

However, the proxying is failing with the following Cannot subclass final class class $Proxy80:

Could not generate CGLIB subclass of class [class $Proxy80]: 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 $Proxy80

As the error, and a quick google, suggests - this occurs when the proxy target is a final class. However, in this chain, there are no classes - only interfaces. Spring generates all the implementations at runtime.

Here's the definition of the interface that's failing:

public interface AuthorDAO extends
    CrossStoreJpaRepository<Author,Long>, CrossStoreQueryDslPredicateExecutor<Author> {

}

Note I'm using a custom subclass of spring's JpaRepository and QueryDslPredicateExecutor, defined as follows:

public interface CrossStoreJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {}
public interface CrossStoreQueryDslPredicateExecutor<T> extends QueryDslPredicateExecutor<T>{}

Elsewhere, I define custom aspects for methods on these interfaces:

@Aspect
@Component
public class DocumentLoadingAspect extends AbstractDocumentAspect {

    @Around("execution(* com.mangofactory.crossstore.repository.CrossStore*.find*(..))")
    public Object loadCrossStoreEntity(ProceedingJoinPoint pjp) throws Throwable
    {
         // implementation omitted
    }

I've confirmed that it's these @Aspect definitions that are causing the problem by removing them and re-running the app.

What is causing this error? It seems to be that proxying a proxy is failing for some reason.

解决方案

My guess is that Spring data JPA creates the repo implementation as a Java proxy which is final and then <aop:aspectj-autoproxy /> attempts to to create another proxy per your aspect using cglib subclassing which won't work. Is proxy-target-class set to true on the autoproxy element?

这篇关于双重代理接口时,Spring AspectJ失败:无法生成类的CGLIB子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆