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. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *