为什么我不能让弹簧加载时间编织工作

时间:2013-10-19 15:43:28

标签: spring groovy spring-annotations load-time-weaving

我一直试图让一个示例@Configuration构建工作(在groovy中)所以我可以触发依赖注入弹出容器,但我得到的是关于-javaagent的错误,我似乎无法修复

我有像这样的beanConfig类

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

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

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

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

`

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

`     ...     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)但是真的想让这个注射器在容器外工作

我做错了什么

1 个答案:

答案 0 :(得分:3)

花了一些时间,但想出了它出错的地方

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

我也在这里给我的博客添加了一个更长的音符

http://willwoodman.wordpress.com/