1. The purpose of this post
I would demo how to use java to solve this exception when @Autowire a property in spring.
org . springframework . beans . factory . BeanCreationException : Error creating bean with name ' myService ' : Injection of autowired dependencies failed ; nested exception is org . springframework . aop . framework . AopConfigException : Could not generate CGLIB subclass of class [ class java8 . learn . autowire . MyObject ]: 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 java8 . learn . autowire . MyObject
at org . springframework . beans . factory . annotation . AutowiredAnnotationBeanPostProcessor . postProcessPropertyValues ( AutowiredAnnotationBeanPostProcessor . java : 372 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . support . AbstractAutowireCapableBeanFactory . populateBean ( AbstractAutowireCapableBeanFactory . java : 1268 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . support . AbstractAutowireCapableBeanFactory . doCreateBean ( AbstractAutowireCapableBeanFactory . java : 553 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . support . AbstractAutowireCapableBeanFactory . createBean ( AbstractAutowireCapableBeanFactory . java : 483 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . support . AbstractBeanFactory $ 1 . getObject ( AbstractBeanFactory . java : 312 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . support . DefaultSingletonBeanRegistry . getSingleton ( DefaultSingletonBeanRegistry . java : 230 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . support . AbstractBeanFactory . doGetBean ( AbstractBeanFactory . java : 308 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . support . AbstractBeanFactory . getBean ( AbstractBeanFactory . java : 197 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . support . DefaultListableBeanFactory . preInstantiateSingletons ( DefaultListableBeanFactory . java : 761 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . context . support . AbstractApplicationContext . finishBeanFactoryInitialization ( AbstractApplicationContext . java : 867 ) ~[ spring - context - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . context . support . AbstractApplicationContext . refresh ( AbstractApplicationContext . java : 543 ) ~[ spring - context - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . boot . SpringApplication . refresh ( SpringApplication . java : 693 ) [ spring - boot - 1.5 . 12 . RELEASE . jar : 1.5 . 12 . RELEASE ]
at org . springframework . boot . SpringApplication . refreshContext ( SpringApplication . java : 360 ) [ spring - boot - 1.5 . 12 . RELEASE . jar : 1.5 . 12 . RELEASE ]
at org . springframework . boot . SpringApplication . run ( SpringApplication . java : 303 ) [ spring - boot - 1.5 . 12 . RELEASE . jar : 1.5 . 12 . RELEASE ]
at org . springframework . boot . SpringApplication . run ( SpringApplication . java : 1118 ) [ spring - boot - 1.5 . 12 . RELEASE . jar : 1.5 . 12 . RELEASE ]
at org . springframework . boot . SpringApplication . run ( SpringApplication . java : 1107 ) [ spring - boot - 1.5 . 12 . RELEASE . jar : 1.5 . 12 . RELEASE ]
at java8 . learn . TestJava8App . main ( TestJava8App . java : 18 ) [ classes /: na ]
Caused by: org . springframework . aop . framework . AopConfigException : Could not generate CGLIB subclass of class [ class java8 . learn . autowire . MyObject ]: 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 java8 . learn . autowire . MyObject
at org . springframework . aop . framework . CglibAopProxy . getProxy ( CglibAopProxy . java : 211 ) ~[ spring - aop - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . aop . framework . ProxyFactory . getProxy ( ProxyFactory . java : 109 ) ~[ spring - aop - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . context . annotation . ContextAnnotationAutowireCandidateResolver . buildLazyResolutionProxy ( ContextAnnotationAutowireCandidateResolver . java : 115 ) ~[ spring - context - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . context . annotation . ContextAnnotationAutowireCandidateResolver . getLazyResolutionProxyIfNecessary ( ContextAnnotationAutowireCandidateResolver . java : 50 ) ~[ spring - context - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . support . DefaultListableBeanFactory . resolveDependency ( DefaultListableBeanFactory . java : 1063 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . annotation . AutowiredAnnotationBeanPostProcessor $AutowiredFieldElement . inject ( AutowiredAnnotationBeanPostProcessor . java : 585 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . annotation . InjectionMetadata . inject ( InjectionMetadata . java : 88 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . beans . factory . annotation . AutowiredAnnotationBeanPostProcessor . postProcessPropertyValues ( AutowiredAnnotationBeanPostProcessor . java : 366 ) ~[ spring - beans - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
... 16 common frames omitted
Caused by: java . lang . IllegalArgumentException : Cannot subclass final class java8 . learn . autowire . MyObject
at org . springframework . cglib . proxy . Enhancer . generateClass ( Enhancer . java : 565 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . transform . TransformingClassGenerator . generateClass ( TransformingClassGenerator . java : 33 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . core . DefaultGeneratorStrategy . generate ( DefaultGeneratorStrategy . java : 25 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . aop . framework . CglibAopProxy $ClassLoaderAwareUndeclaredThrowableStrategy . generate ( CglibAopProxy . java : 999 ) ~[ spring - aop - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . core . AbstractClassGenerator . generate ( AbstractClassGenerator . java : 329 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . proxy . Enhancer . generate ( Enhancer . java : 492 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . core . AbstractClassGenerator $ClassLoaderData $ 3 . apply ( AbstractClassGenerator . java : 93 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . core . AbstractClassGenerator $ClassLoaderData $ 3 . apply ( AbstractClassGenerator . java : 91 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . core . internal . LoadingCache $ 2 . call ( LoadingCache . java : 54 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at java . util . concurrent . FutureTask . run ( FutureTask . java : 266 ) ~[ na: 1.8 . 0 _121 ]
at org . springframework . cglib . core . internal . LoadingCache . createEntry ( LoadingCache . java : 61 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . core . internal . LoadingCache . get ( LoadingCache . java : 34 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . core . AbstractClassGenerator $ClassLoaderData . get ( AbstractClassGenerator . java : 116 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . core . AbstractClassGenerator . create ( AbstractClassGenerator . java : 291 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . proxy . Enhancer . createHelper ( Enhancer . java : 480 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . cglib . proxy . Enhancer . createClass ( Enhancer . java : 337 ) ~[ spring - core - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . aop . framework . ObjenesisCglibAopProxy . createProxyClassAndInstance ( ObjenesisCglibAopProxy . java : 55 ) ~[ spring - aop - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
at org . springframework . aop . framework . CglibAopProxy . getProxy ( CglibAopProxy . java : 201 ) ~[ spring - aop - 4.3 . 16 . RELEASE . jar : 4.3 . 16 . RELEASE ]
... 23 common frames omitted
2. Environments
java 1.8+
spring framework 4+
3. The Code
The object to be autowired:
@Component
public final class MyObject {
public String getTime () {
return ( new Date ()). toString ();
}
}
The class is final because I don’t want others to override its methods. But when I use @Autowire and @Lazy in other classes like this:
@Autowired @Lazy
private MyObject myObject ;
Run the app, I got the above exceptions.
Because spring would use CGLIB to generate a subclass of the @Lazy class, and MyObject is final ,so the exception occurred, I change the code like this:
@Component
public class MyObject { //remove final
public String getTime () {
return ( new Date ()). toString ();
}
}
Clean and re-run the app, everything works fine now.