A workaround I found was to hot-attach InstrumentationSavingAgent
from spring-instrument
instead of starting the agent via -javaagent
command line parameter. But for that you need an Instrumentation
instance. I just used the tiny helper library byte-buddy-helper
(works independently of ByteBuddy, don't worry) which can do just that. Make sure that in Java 9+ JVMs the Attach API is activated if for some reason this is not working.
So get rid of implements LoadTimeWeavingConfigurer
and the two factory methods in your configuration class and just do it like this:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.10.14</version>
</dependency>
@SpringBootApplication
public class Application {
public static void main(String[] args) {
Instrumentation instrumentation = ByteBuddyAgent.install();
InstrumentationSavingAgent.premain("", instrumentation);
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
// ...
}
}
Feel free to ask follow-up questions if there is anything you do not understand.
Update: One more thing I noticed is that this only works for me with aspectjWeaving = ENABLED
, not with AUTODETECT
. And for one sample Spring bean I noticed that @Component
did not work, probably because of some bootstrapping issue between Spring vs AspectJ. Hence, I replaced it by an explicit @Bean
configuration, then it worked. Something like this:
@Configuration
@ComponentScan("com.spring.aspect.dynamicflow")
@EnableLoadTimeWeaving(aspectjWeaving = ENABLED)
public class ApplicationConfig {
@Bean
public JobProcess jobProcess() {
return new JobProcessImpl();
}
}