3

i have been trying to get an example @Configuration build to work (in groovy) so i can trigger dependency injection outwith a spring container but all i get is error about the -javaagent which i cant seem to fix

I have a beanConfig class like this

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

then a sample class i will call new on and try and get the injection oustide the spring container, where the diSource bean is declared in the above configuration class

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

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

`

in my sampler class i call this to try and trigger the injection

` ... 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()

`

on the class path i have aspectjeaver-1.6.10.jar, aspectjrt-1.6.10.jar, spring-xxx-3.1.4.jars, etc heres my gradle depdency listing

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" }

in the eclipse project runtime for the vm args i have -javaagent:C:/Users/802518659/aspectjweaver-1.6.10.jar

and have tried same with spring-instrument-3.1.4.jar and had same problem.

when i run the project i get this error

`

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)
...

`

which tells me that my application can't instrument the java classloader - despite the fact i start it with the -javaagent:spring-instrument-xxx or aspectjweaver.jar - both fail

So what am i doing wrong - this is really beginning to bother me - i can get ordinary injection within the context to work (no LTW) but really wanted to get this injection outside the container to work

what am i doing wrong

WILLIAM WOODMAN
  • 1,185
  • 5
  • 19
  • 36

1 Answers1

3

took some time but figured out where it was going wrong

corrected on another post on spring forums see http://forum.spring.io/forum/spring-projects/container/724426-cant-get-aspectj-load-time-weaving-to-work

also i added a longer note to my blog here

http://willwoodman.wordpress.com/

WILLIAM WOODMAN
  • 1,185
  • 5
  • 19
  • 36