Problem Description:
When using SPRINGAOP to enhance a target object, the following exception occurs if the condition of the pointcut is too broad!
Such as:
@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 [CLA SS 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$basepackagesat 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 : In 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) ... Common frames omittedcaused 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$basepackagesat 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) ... Common frames omittedcaused By:java.lang.IllegalArgumentException:Cannot subclass Final Class Org.springframework.boot.autoconfigure.autoconfigurationpackages$basepackagesat orG.springframework.cglib.proxy.enhancer.generateclass (enhancer.java:565) at Org.springframework.cglib.transform.TransformingClassGenerator.generateClass (Transformingclassgenerator.java : To 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) ... Common frames omitted
Problem Analysis:
With the exception information, we can see that when spring passes cglib to the target object org.springframework.boot.autoconfigure.autoconfigurationpackages$ An exception occurred while the basepackages was being enhanced.
Reason:
1) Cglib Enhancement technology is implemented by dynamically creating subclasses after inheriting the original object.
2) by looking at the Basepackages class source code, it is found that the class is final decorated.
Static Final class Basepackages {...}
3) Java does not allow inheriting classes that are modified by the final.
Workaround:
The above problem occurs because of the tangent condition defined by the AOP, which contains the class that was modified by the final.
1) Resolved by modifying the class's access modifier.
2) Filter the final modified class by adjusting the tangent point condition.
SPRINGAOP question--cannot Subclass final class org.springframework.boot.autoconfigure.autoconfigurationpackages$ Basepackages