how to setup load-time weaving with Spring and Tomcat WITHOUT using the -javaagent on the command line

后端 未结 3 1172
走了就别回头了
走了就别回头了 2021-01-12 14:40

I am using Spring 3.2.9, Tomcat 6.0.44

I am trying to configure my application\'s Spring instrumentation provider (e.g. spring-instrumentation.jar) for load-time wea

相关标签:
3条回答
  • 2021-01-12 15:20

    I used invesdwin-instrument to perform that. It allows you to use Load time weaving instrumentation dynamically so that you don't have to use any javaagent.

    It took me a bit of effort to make it work with Tomcat 8.5 though. But it finally work using this configuration with Spring Boot :

    @SpringBootApplication
    @EnableLoadTimeWeaving // instead of @ImportResource(locations = "classpath:/META-INF/ctx.spring.weaving.xml")
    public class MySpringBootApplication {
        public static void main(final String[] args) {
            DynamicInstrumentationLoader.waitForInitialized(); //dynamically attach java agent to jvm if not already present
            DynamicInstrumentationLoader.initLoadTimeWeavingContext(); //weave all classes before they are loaded as beans
            SpringApplication.run(MySpringBootApplication.class, args); //start application, load some classes
        }
    }
    

    It should also work with previous version of Tomcat.

    Regards

    0 讨论(0)
  • 2021-01-12 15:28
    https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving
    

    Try maven plug-in from the above link. It's working

    0 讨论(0)
  • 2021-01-12 15:30

    This is the code that I managed to use in order to removed the exception that you mentioned. Basically you have to implement the LoadTimeWeavingConfigurer and override the method getLoadTimeWeaver().

    @Configuration
    @ComponentScan(basePackages = "org.myproject")
    @EnableAspectJAutoProxy
    @EnableSpringConfigured
    @EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.AUTODETECT)
    public class Config implements LoadTimeWeavingConfigurer {
    
        @Override
        public LoadTimeWeaver getLoadTimeWeaver() {
            return new ReflectiveLoadTimeWeaver();
        }
    
        @Bean
        public InstrumentationLoadTimeWeaver loadTimeWeaver()  throws Throwable {
            InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
            return loadTimeWeaver;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题