springaop问题——Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPac

问题描述:

在使用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修饰的类。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏一枝花算不算浪漫

[Spring框架] Spring中的 ContextLoaderListener 实现原理.

2777
来自专栏杂烩

分布式服务框架之Dubbo整合Spring项目(三)

        代码粘贴完毕,注意一下dubbo依赖的是spring2.5,需要在pom里处理一下,不然使用别的版本的spring的时候,项目会报错。

823
来自专栏名山丶深处

springaop问题——Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPac

2024
来自专栏Netkiller

Spring boot with Hessian

节选自《Netkiller Java 手札》一书 5.20. Spring boot with Hessian 5.20.1. Maven <depende...

3748
来自专栏世界第一语言是java

简化 @RestController @RequestMapping ,自定义spring注解

1234
来自专栏水击三千

SpringMvc学习-增删改查

本节主要介绍SpringMVC简单的增删改查功能。 1.查询 dao中的代码 1 public List<WeatherPojo> getAllWea...

2886
来自专栏javathings

使用 FactoryBean 接口创建实例

可以通过实现 FactoryBean<T> 接口,然后创建一个实例到 Spring 容器中。

1072
来自专栏Netkiller

Phalcon VS Spring 用法对照手册

Phalcon VS Spring 摘要 Phalcon VS Spring 用法对照表 ---- 目录 1. Install 1.1. Phalcon 1.2...

3586
来自专栏面朝大海春暖花开

spring源码分析 contextConfigLocation属性的位置

1171
来自专栏后端之路

SpringMvc4的Zero Xml配置

背景 相信大部分做J2ee的项目的小伙伴都会在各种配置文件中饱受摧残。听说0配置估计都蛮感兴趣。 事实上xml的配置个人觉得对于项目结构比较清晰,纯粹的java...

2285

扫码关注云+社区