Spring-boot: LTW Not Working With Single Jar

Created on 13 May 2014  路  8Comments  路  Source: spring-projects/spring-boot

I'm trying to execute my code below

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, SecurityAutoConfiguration.class})
@ComponentScan//(basePackages = {"com.a.b.c", "com.d.e.f"})
@ImportResource(value = {"classpath:conf/spring/applicationContext-all.xml", "classpath:conf/spring/applicationContext-servlet.xml"})
@EnableLoadTimeWeaving
public class AApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(AApplication.class, args);
    }
}

with

java -javaagent:/path/to/spring-instrument-4.0.3.RELEASE.jar -jar target/project-exec.war

But the javaagent seems to be not honored by Spring Boot.

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver' defined in class org.springframework.context.annotation.LoadTimeWeavingConfiguration: 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 [org.springframework.boot.loader.LaunchedURLClassLoader] 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:597)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
    at com.devsisters.game.oven2.CookierunApplication.main(CookierunApplication.java:27)
    ... 6 more
Caused by: 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 [org.springframework.boot.loader.LaunchedURLClassLoader] 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.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
    ... 23 more
Caused by: java.lang.IllegalStateException: ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader] 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.context.weaving.DefaultContextLoadTimeWeaver.setBeanClassLoader(DefaultContextLoadTimeWeaver.java:91)
    at org.springframework.context.weaving.DefaultContextLoadTimeWeaver.<init>(DefaultContextLoadTimeWeaver.java:67)
    at org.springframework.context.annotation.LoadTimeWeavingConfiguration.loadTimeWeaver(LoadTimeWeavingConfiguration.java:81)
    at org.springframework.context.annotation.LoadTimeWeavingConfiguration$$EnhancerBySpringCGLIB$$392ffbd3.CGLIB$loadTimeWeaver$0(<generated>)
    at org.springframework.context.annotation.LoadTimeWeavingConfiguration$$EnhancerBySpringCGLIB$$392ffbd3$$FastClassBySpringCGLIB$$3878acfb.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
    at org.springframework.context.annotation.LoadTimeWeavingConfiguration$$EnhancerBySpringCGLIB$$392ffbd3.loadTimeWeaver(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 24 more

Most helpful comment

Instead of running with - javaagent:/path/to/org.springframework.instrument-{version}.jar

Declaring a a loadTimerWeaver bean like below worked too .. or am I doing something wrong ???

@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
    InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
    return loadTimeWeaver;
}

All 8 comments

Duplicates #863?

I have this same issue with an identical stacktrace.

The issue is still present when I use:

-javaagent:/path/to/aspectj-weaver.jar -javaagent:/path/to/spring-instrument.jar

I'm going to try to poke at this more and see what is going on, but I can say that adding both agents doesn't resolve the problem.

any solution to this problem?

Which of Spring Boot are you using? This problem should be fixed in 1.1. See #863 for some more details. The commit of the fix was ee08667e8.

If you're using Boot 1.1 and still having trouble please open a new issue and we can take a look.

Instead of running with - javaagent:/path/to/org.springframework.instrument-{version}.jar

Declaring a a loadTimerWeaver bean like below worked too .. or am I doing something wrong ???

@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
    InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
    return loadTimeWeaver;
}

@mrtinkz this doesn't seem to work anymore, as there is already that Bean defined, and overriding is not permitted.

Agent weaving works for me with all the latest versions of Spring Boot. Please ask on Stack Overflow in case someone can spot your error. If you think you have found a new problem, please open a new issue and provide a sample project to reproduce the problem.

@dsyer Hi! Thanks for answering.
I've actually posted two questions yesterday, regarding this issue and LTW, with no responses.

https://stackoverflow.com/questions/54749106/aspectj-ltw-weaving-not-working-with-spring-boot
https://stackoverflow.com/questions/54746336/spring-aop-with-aspectj-load-time-weaving-doubts

Was this page helpful?
0 / 5 - 0 ratings