• springaop问题——Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages


    问题描述:

    在使用springaop对目标对象增强时,若切点的条件过于宽泛就会出现以下异常!

    如:

    @Before("execution(* *(..))")

    @Before("args(..)")

    @Before("within(org..*)")

    ...

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.AutoConfigurationPackages': Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages]: 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 org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    	at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
    	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    	... 26 common frames omitted
    Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages]: 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 org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages
    	at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:211)
    	at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:466)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:349)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:298)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:423)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1634)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    	... 40 common frames omitted
    Caused by: java.lang.IllegalArgumentException: Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages
    	at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:565)
    	at org.springframework.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    	at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    	at org.springframework.aop.framework.CglibAopProxy$ClassLoaderAwareUndeclaredThrowableStrategy.generate(CglibAopProxy.java:999)
    	at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329)
    	at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
    	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
    	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
    	at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
    	at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
    	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
    	at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
    	at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
    	at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337)
    	at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:55)
    	at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:201)
    	... 47 common frames omitted
    

    问题分析:

    通过异常信息可以看出,当spring通过cglib给目标对象org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages进行增强时,发生了异常。

    原因:

    1)cglib增强技术,是通过继承原对象后动态创建子类来实现的。

    2)通过查看BasePackages类源代码,发现该类被final修饰。

    static final class BasePackages {
    	...
    }
    

    3)java不允许继承被final所修饰的类。

    解决方法:

    以上问题出现的原因为aop定义的切点条件中,包含了被final所修饰的类。

    1)通过修改类的访问修饰符解决。

    2)通过调整切点条件,过滤final修饰的类。

  • 相关阅读:
    [Automation] 自动化测试工具和测试框架大集合
    C#借助API实现黑盒自动化测试工具的编写
    C#实现RSA加密解密
    Uipath开发过程中最常见的5类错误
    tqdm介绍及常用方法
    Top-1准确率和Top-5准确率
    focal loss焦点损失
    【深度学习】卷积神经网络中Dropout、BatchNorm的位置选择
    数据增强——mixup
    数据增强之mixup算法详解
  • 原文地址:https://www.cnblogs.com/skychenjiajun/p/8867038.html
Copyright © 2020-2023  润新知