Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-10856

Detect LoadTimeWeaver bean when declared through @Bean method as well

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 3.2.4, 4.0 M2
    • Fix Version/s: 4.3 RC2
    • Component/s: Core
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      This may be a Java-config problem only. I haven't tried it with XML config.

      I'm configuring a LocalContainerEntityManagerFactoryBean, and I want to enable load time weaving. @EnableLoadTimeWeaving works (I see in the log that it found addTransformer on the ClassLoader and created a load time weaver), but setLoadTimeWeaver is never called on the LocalContainerEntityManagerFactoryBean.

      LocalContainerEntityManagerFactoryBean implements LoadTimeWeaverAware, so my (possibly incorrect) assumption was that Spring should set the LoadTimeWeaver property, but it does not. If my assumption was incorrect, the documentation should be updated to indicate that you must call this method manually. If my assumption was correct, there is a bug here, because Spring is not calling this method.

      Instead, I have to do this in my configuration:

      @Configuration
      ...
      @EnableLoadTimeWeaving
      ...
      public class RootContextConfiguration
      {
          ...
          @Inject LoadTimeWeaver loadTimeWeaver;
          ...
       
          @Bean
          public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
          {
              ...
              LocalContainerEntityManagerFactoryBean factory =
                      new LocalContainerEntityManagerFactoryBean();
              ...
              factory.setLoadTimeWeaver(this.loadTimeWeaver);
              ...
              return factory;
          }
          ...
      }

      That code works. The LoadTimeWeaver is injected and I successfully add it to my factory, then the JPA provider starts instrumenting my classes. However, without this the LoadTimeWeaver is never added to the LocalContainerEntityManagerFactoryBean and the JPA provider cannot instrument my classes.

        Issue Links

          Activity

          Hide
          guitarking117 Nick Williams added a comment -

          Juergen Hoeller, are you not going to be able to get this fixed for 4.0.0? That's disappointing.

          Show
          guitarking117 Nick Williams added a comment - Juergen Hoeller , are you not going to be able to get this fixed for 4.0.0? That's disappointing.
          Hide
          guitarking117 Nick Williams added a comment -

          Any possibility this gets fixed in 4.0.1 or 4.0.2?

          Show
          guitarking117 Nick Williams added a comment - Any possibility this gets fixed in 4.0.1 or 4.0.2?
          Hide
          smckay Steve McKay added a comment -

          I encountered this issue with Spring 4.1.1, and it's caused by the way the ApplicationContext registers LoadTimeWeaverAwareProcessor. LoadTimeWeaver definition is checked for in AbstractApplicationContext.prepareBeanFactory(), which runs before BeanFactoryPostProcessors. Because @Configuration classes are handled by a BeanFactoryPostProcessor, the LoadTimeWeaver registered by @EnableLoadTimeWeaving is added to late too trigger registration of LoadTimeWeaverAwareProcessor.

          Because I'm using Spring Boot, I used a SpringApplicationRunListener to register a LoadTimeWeaver in the contextLoaded() callback. Of course that only works with Spring Boot so anyone not using it will have to find some other workaround. The important thing is to have a LoadTimeWeaver definition available before calling ApplicationContext.refresh().

          Show
          smckay Steve McKay added a comment - I encountered this issue with Spring 4.1.1, and it's caused by the way the ApplicationContext registers LoadTimeWeaverAwareProcessor. LoadTimeWeaver definition is checked for in AbstractApplicationContext.prepareBeanFactory(), which runs before BeanFactoryPostProcessors. Because @Configuration classes are handled by a BeanFactoryPostProcessor, the LoadTimeWeaver registered by @EnableLoadTimeWeaving is added to late too trigger registration of LoadTimeWeaverAwareProcessor. Because I'm using Spring Boot, I used a SpringApplicationRunListener to register a LoadTimeWeaver in the contextLoaded() callback. Of course that only works with Spring Boot so anyone not using it will have to find some other workaround. The important thing is to have a LoadTimeWeaver definition available before calling ApplicationContext.refresh().
          Hide
          michael.simons Michael Simons added a comment - - edited

          Here's a snippet that works with Spring Boot in case anybody lands here:

          import org.springframework.boot.SpringApplication;
          import org.springframework.boot.autoconfigure.SpringBootApplication;
          import org.springframework.boot.context.event.ApplicationPreparedEvent;
          import org.springframework.context.ApplicationListener;
          import org.springframework.context.ConfigurableApplicationContext;
          import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
           
          @SpringBootApplication
          public class Application {    
              public static void main(final String... args) {
                  final SpringApplication springApplication = new SpringApplication(Application.class);
                  springApplication.addListeners(new ApplicationListener<ApplicationPreparedEvent>() {
                      @Override
                      public void onApplicationEvent(final ApplicationPreparedEvent event) {
                          event.getApplicationContext().getBeanFactory().registerSingleton(ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, new InstrumentationLoadTimeWeaver());
                      }
                  });
                  springApplication.run(args);
              }
          }
          

          Show
          michael.simons Michael Simons added a comment - - edited Here's a snippet that works with Spring Boot in case anybody lands here: import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver; @SpringBootApplication public class Application { public static void main( final String... args) { final SpringApplication springApplication = new SpringApplication(Application. class ); springApplication.addListeners( new ApplicationListener<ApplicationPreparedEvent>() { @Override public void onApplicationEvent( final ApplicationPreparedEvent event) { event.getApplicationContext().getBeanFactory().registerSingleton(ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, new InstrumentationLoadTimeWeaver()); } }); springApplication.run(args); } }
          Hide
          juergen.hoeller Juergen Hoeller added a comment -

          Thanks for the pointers! It seems that we need to detect a LoadTimeWeaver bean when declared through an @Bean method as well, after we registered all BeanFactoryPostProcessors. We did not support that at all before but we are doing it in AbstractApplicationContext as of 4.3 now.

          That said, it might make sense for Spring Boot to go a step further and have first-class support for LoadTimeWeaver setup, registering an externally declared LoadTimeWeaver class at the earliest possible point.

          Show
          juergen.hoeller Juergen Hoeller added a comment - Thanks for the pointers! It seems that we need to detect a LoadTimeWeaver bean when declared through an @Bean method as well, after we registered all BeanFactoryPostProcessors . We did not support that at all before but we are doing it in AbstractApplicationContext as of 4.3 now. That said, it might make sense for Spring Boot to go a step further and have first-class support for LoadTimeWeaver setup, registering an externally declared LoadTimeWeaver class at the earliest possible point.
          Hide
          shadow Andrei Ivanov added a comment -

          Sorry to hijack this issue, but maybe I can draw attention to my related issue too, SPR-13892

          Show
          shadow Andrei Ivanov added a comment - Sorry to hijack this issue, but maybe I can draw attention to my related issue too, SPR-13892

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              guitarking117 Nick Williams
              Last updater:
              Juergen Hoeller
            • Votes:
              9 Vote for this issue
              Watchers:
              14 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                1 year, 32 weeks, 2 days ago