0

I am trying to enable loadtimeweaving without javaagent jar files of aspectweaver and spring-instrument. This what I have implemented to achieve the same But it's not working.

@ComponentScan("com.myapplication")
@EnableAspectJAutoProxy
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.AUTODETECT)
public class AopConfig implements LoadTimeWeavingConfigurer {
 
 @Override
 public LoadTimeWeaver getLoadTimeWeaver() {
     return new ReflectiveLoadTimeWeaver();
 }
 
  /**
  * Makes the aspect a Spring bean, eligible for receiving autowired components.
  */
 @Bean
 public InstrumentationLoadTimeWeaver loadTimeWeaver()  throws Throwable {
     InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
     return loadTimeWeaver;
 }

}
nami20
  • 31
  • 1
  • 2

1 Answers1

0

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();
  }
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202