3

我一直在尝试让一个示例 @Configuration 构建工作(在 groovy 中),这样我就可以使用 spring 容器触发依赖注入,但我得到的只是关于 -javaagent 的错误,我似乎无法修复

我有一个像这样的 beanConfig 类

@Configuration @EnableSpringConfigured // should turn on AnnotationBeanConfigurerAspect @EnableLoadTimeWeaving (aspectjWeaving=AspectJWeaving.ENABLED) // switch on for this context class BeanConfig {

然后我将调用一个示例类 new 并尝试在 spring 容器之外进行注入,其中 diSource bean 在上面的配置类中声明

` @Configurable (autowire=Autowire.BY_TYPE, dependencyCheck=true) 类 ExtDI { @Autowired DISource diSource

def say () {
    println "ExtDI : diSource set as " + diSource.name
}
}

`

在我的采样器类中,我调用它来尝试触发注入

` ... static void main (String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( /BeanConfig.class /) ctx.scan ("com.softwood") ctx.refresh () ....

    //trigger LTW injection
    ExtDI ext = new ExtDI()
    ext.say()

`

在类路径上,我有 aspectjeaver-1.6.10.jar、aspectjrt-1.6.10.jar、spring-xxx-3.1.4.jars 等,这是我的 gradle depdency 列表

dependencies { compile 'org.codehaus.groovy:groovy-all:2.1.7' compile group: 'commons-collections', name: 'commons-collections', version: '3.2' testCompile group: 'junit', name: 'junit', version: '4.+' compile "org.springframework:spring-core:${spring_version}" compile "org.springframework:spring-beans:${spring_version}" compile "org.springframework:spring-context:${spring_version}" compile "org.springframework:spring-aspects:${spring_version}" compile "org.springframework:spring-aop:${spring_version}" compile "org.springframework:spring-instrument:${spring_version}" compile "org.aspectj:aspectjrt:1.6.10" compile "org.aspectj:aspectjweaver:1.6.10" compile "cglib:cglib:2.2" }

在我拥有的 vm args 的 eclipse 项目运行时 -javaagent:C:/Users/802518659/aspectjweaver-1.6.10.jar

并尝试过使用 spring-instrument-3.1.4.jar 并遇到同样的问题。

当我运行项目时出现此错误

`

Oct 19, 2013 4:02:40 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@492ff1: startup date [Sat Oct 19 16:02:40 BST 2013]; root of context hierarchy
Oct 19, 2013 4:02:40 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@11bedb0: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,beanConfig,willsBean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.aspectj.SpringConfiguredConfiguration,org.springframework.context.config.internalBeanConfigurerAspect,org.springframework.context.annotation.LoadTimeWeavingConfiguration,loadTimeWeaver,publicBean,privateBean,publicBeanWithDI,myDISource,diTarget]; root of factory hierarchy
Oct 19, 2013 4:02:40 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@11bedb0: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,beanConfig,willsBean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.aspectj.SpringConfiguredConfiguration,org.springframework.context.config.internalBeanConfigurerAspect,org.springframework.context.annotation.LoadTimeWeavingConfiguration,loadTimeWeaver,publicBean,privateBean,publicBeanWithDI,myDISource,diTarget]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver' defined in class path resource [org/springframework/context/annotation/LoadTimeWeavingConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.instrument.classloading.LoadTimeWeaver org.springframework.context.annotation.LoadTimeWeavingConfiguration.loadTimeWeaver()] threw exception; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581)
...

`

这告诉我我的应用程序无法检测 java 类加载器 - 尽管我使用 -javaagent:spring-instrument-xxx 或 aspectjweaver.jar 启动它 - 两者都失败了

那么我做错了什么-这真的开始困扰我-我可以在上下文中进行普通注入工作(没有LTW),但真的想在容器外进行这种注入工作

我究竟做错了什么

4

1 回答 1

3

花了一些时间,但弄清楚了哪里出了问题

在春季论坛上的另一篇文章中更正,请参阅 http://forum.spring.io/forum/spring-projects/container/724426-cant-get-aspectj-load-time-weaving-to-work

我还在这里为我的博客添加了更长的注释

http://willwoodman.wordpress.com/

于 2013-10-21T14:48:43.273 回答