Get your InstrumentationLoadTimeWeaver recognized by LocalContainerEntityManagerFactoryBean

April 10, 2016 by Michael

org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean is LoadTimeWeaverAware and as thus should be provided with such. Usually this would be done with a @Configuration, but then there is SPR-10856. In short: a LoadTimeWeaverAwareProcessor is provided before post processing the bean factory. This means that an @Bean inside a configuration class comes to load for load time weaving.

At least for Spring Boot there’s a solution. You can register the LoadTimeWeaver before startup:

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
@EnableConfigurationProperties
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);
    }
}

Be careful not to use a lambda, because there’s SPR-14109.

Anyway: This way byte code enhancement for JPA / Hibernate entities will work. Another solution would be build time byte code enhancement, as Vlad points out here: For Hibernate 5 and 4.

No comments yet

Post a Comment

Your email is never published nor shared. Required fields are marked *