Spring: Is CGLIB required to inject a specific class using @Resource

I configured Spring 3.0.6 with AspectJ LTW using spring -instrument.jar and:

<context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> 

When removing CGLIB from dependencies, I get the following exception even after creating MyBean (and its launch @PostConstruct):

 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlItemService' defined in file [/path/to/project/foo/bar/MyBean.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:435) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:409) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:541) at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:147) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:297) ... 72 more Caused by: org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces. at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:67) at org.springframework.aop.framework.ProxyCreatorSupport.createAopProxy(ProxyCreatorSupport.java:104) at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ... 83 more 

Does this mean that I should have a CGLIB even with a time-weaver?

+4
source share
2 answers

After more than three hours of researching the code and modifying the Spring bean's XML definitions, I finally found the problem. This can be found from this part of stacktrace, which I noticed after finding a solution:

 at org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor.postProcessAfterInitialization(AsyncAnnotationBeanPostProcessor.java:126) 

Based on the note here , I changed all the attributes of the proxy target class to false in <aop:config> , <aop:aspectj-autoproxy> and <tx:annotation-driven> , but for no success.

Then I started removing parts of my XML definition to find which one fixes this problem. Commenting out <task:annotation-driven> helped, and the problem was resolved. Then I saw that this element has a mode attribute, which I did not specify, therefore its default proxy value is used, and therefore CGLIB is required. When I changed mode="aspectj" , the problem is resolved:

 <task:annotation-driven scheduler="dataProviderScheduler" executor="dataProviderExecutor" mode="aspectj" /> 
+10
source

No, but you need to create AOP proxies (cannot assign the target class). Your class has one of:

  • @Transactional
  • @Cacheable
  • @Async
  • ...

annotations? Or maybe there is some kind of external aspect around the methods of this class? However, the AspectJ compiler should take care of weaving; AFAIR CGLIB should not be used. Can you show us your code?

In my case, in order to correctly allow loading times for transaction support, I had to add:

  <context:load-time-weaver/> <tx:annotation-driven mode="aspectj"/> <aop:config proxy-target-class="true"/> 

Take a look at a working example .

+3
source

Source: https://habr.com/ru/post/1381746/


All Articles