6

我已经使用 spring-instrument.jar 配置了带有 AspectJ LTW 的 Spring 3.0.6,并且:

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

从依赖项中删除 CGLIB 时,即使在创建 MyBean 之后(并且它的 @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

这是否意味着即使存在加载时间编织器,我也应该拥有 CGLIB?

4

3 回答 3

11

经过 3 个多小时的代码调查和更改 Spring bean 定义 XML,我终于找到了问题所在。这可以从stacktrace的这一部分找到,我在找到解决方案后注意到:

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

根据此处的注释,我在、 和中将所有代理目标类属性更改为 false <aop:config>,但没​​有成功。<aop:aspectj-autoproxy><tx:annotation-driven>

然后我开始删除我的部分 XML 定义,以找出哪个可以解决这个问题。评论<task:annotation-driven>帮助并解决了问题。然后我看到这个元素有一个mode我没有指定的属性,所以它的默认值 ,proxy被使用,所以 CGLIB 是必需的。当我改变mode="aspectj"问题解决了:

<task:annotation-driven scheduler="dataProviderScheduler" executor="dataProviderExecutor" mode="aspectj" />
于 2011-11-17T18:52:58.270 回答
3

不,但需要创建 AOP 代理(不能代理目标类)。您的班级是否有以下之一:

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

注释?或者也许这个类的方法有一些外部方面?尽管如此,AspectJ 编译器应该负责编织,不应该使用 AFAIR CGLIB。你能告诉我们你的代码吗?

在我的情况下,为了正确启用加载时间编织以支持事务,我必须添加:

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

看看一个工作示例

于 2011-11-17T16:45:21.820 回答
2

如果您使用的是 Spring APO,则需要包含 CGLIB。有两种方法可以将其添加到您的项目中

  1. 下载 CGLIB 库文件并将其添加到您的库文件中。 http://www.java2s.com/Code/Jar/c/Downloadcglib22jar.htm
  2. 在 pom.xml 文件中添加 Maven 依赖项

    https://mvnrepository.com/artifact/cglib/cglib/2.2.2


<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2</version>
</dependency>
于 2018-10-27T11:09:38.507 回答