spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
Apache License 2.0
2.74k stars 356 forks source link

Error creating bean with name "org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration" #315

Closed ivangfr closed 4 years ago

ivangfr commented 4 years ago

Hi guys! I am trying to build a native docker image for a JPA/Hibernate application. The build succeeds but there is an exception when I run it.

The app is called book-api. It's a simple spring-boot application that uses basically dependencies like actuator, data-jpa, validation, webflux, lombok and mapstruct. MySQL is used as storage.

I am using spring-boot version 2.4.0-M3 and spring-graalvm-native version 0.8.1. Besides, I've followed what is described in the spring-graalvm-native documentation https://repo.spring.io/milestone/org/springframework/experimental/spring-graalvm-native-docs/0.8.1/spring-graalvm-native-docs-0.8.1.zip!/reference/index.html

Here is the project link https://github.com/ivangfr/graalvm-quarkus-micronaut-springboot/tree/master/book-api/springboot-book-api

The exception I am getting is this one

2020-10-04 14:00:59.659  WARN 1 --- [           main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalStateException: Failed to asynchronously initialize native EntityManagerFactory: java.lang.ExceptionInInitializerError
2020-10-04 14:00:59.659  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-10-04 14:00:59.659  WARN 1 --- [           main] o.s.b.f.support.DisposableBeanAdapter    : Invocation of destroy method failed on bean with name 'entityManagerFactory': java.lang.IllegalStateException: Failed to asynchronously initialize native EntityManagerFactory: java.lang.ExceptionInInitializerError
2020-10-04 14:00:59.659  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-10-04 14:00:59.663  INFO 1 --- [           main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-10-04 14:00:59.665 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalStateException: Failed to asynchronously initialize native EntityManagerFactory: java.lang.ExceptionInInitializerError
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1415) ~[na:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:608) ~[na:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[na:na]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[na:na]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[na:na]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[na:na]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[na:na]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[na:na]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925) ~[na:na]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588) ~[na:na]
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:63) ~[na:na]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:770) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:421) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:329) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at com.mycompany.springbootbookapi.SpringbootBookApiApplication.main(SpringbootBookApiApplication.java:10) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
Caused by: java.lang.IllegalStateException: Failed to asynchronously initialize native EntityManagerFactory: java.lang.ExceptionInInitializerError
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.getNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:573) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:516) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:731) ~[na:na]
    at com.sun.proxy.$Proxy332.getStatistics(Unknown Source) ~[na:na]
    at io.micrometer.core.instrument.binder.jpa.HibernateMetrics.<init>(HibernateMetrics.java:110) ~[na:na]
    at org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration.bindEntityManagerFactoryToRegistry(HibernateMetricsAutoConfiguration.java:68) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration.lambda$bindEntityManagerFactoriesToRegistry$0(HibernateMetricsAutoConfiguration.java:60) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:na]
    at org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa.HibernateMetricsAutoConfiguration.bindEntityManagerFactoriesToRegistry(HibernateMetricsAutoConfiguration.java:60) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:755) ~[na:na]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[na:na]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    ... 18 common frames omitted
Caused by: java.lang.ExceptionInInitializerError: null
    at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:291) ~[na:na]
    at net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$ParameterBinder.<clinit>(TargetMethodAnnotationDrivenBinder.java:164) ~[na:na]
    at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:351) ~[na:na]
    at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:271) ~[na:na]
    at net.bytebuddy.implementation.MethodDelegation.withDefaultConfiguration(MethodDelegation.java:601) ~[na:na]
    at net.bytebuddy.implementation.MethodDelegation.to(MethodDelegation.java:271) ~[na:na]
    at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState$ProxyDefinitionHelpers$1.run(ByteBuddyState.java:263) ~[na:na]
    at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState$ProxyDefinitionHelpers$1.run(ByteBuddyState.java:259) ~[na:na]
    at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState$ProxyDefinitionHelpers.<init>(ByteBuddyState.java:269) ~[na:na]
    at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState$ProxyDefinitionHelpers.<init>(ByteBuddyState.java:244) ~[na:na]
    at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState.<init>(ByteBuddyState.java:86) ~[na:na]
    at org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl.<init>(BytecodeProviderImpl.java:53) ~[na:na]
    at org.hibernate.cfg.Environment.buildBytecodeProvider(Environment.java:345) ~[na:na]
    at org.hibernate.cfg.Environment.buildBytecodeProvider(Environment.java:337) ~[na:na]
    at org.hibernate.cfg.Environment.<clinit>(Environment.java:230) ~[na:na]
    at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:351) ~[na:na]
    at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:271) ~[na:na]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$MergedSettings.<init>(EntityManagerFactoryBuilderImpl.java:1355) ~[na:na]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$MergedSettings.<init>(EntityManagerFactoryBuilderImpl.java:1345) ~[na:na]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.mergeSettings(EntityManagerFactoryBuilderImpl.java:478) ~[na:na]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:217) ~[na:na]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:168) ~[na:na]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:52) ~[na:na]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[com.mycompany.springbootbookapi.SpringbootBookApiApplication:na]
    at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at java.lang.Thread.run(Thread.java:834) ~[na:na]
    at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:517) ~[na:na]
    at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:192) ~[na:na]
Caused by: java.lang.IllegalStateException: size = 0
    at net.bytebuddy.matcher.FilterableList$AbstractBase.getOnly(FilterableList.java:139) ~[na:na]
    at net.bytebuddy.implementation.bind.annotation.Super$Binder.<clinit>(Super.java:223) ~[na:na]
    at com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:351) ~[na:na]
    at com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:271) ~[na:na]
    ... 30 common frames omitted

Any idea what I should do? Thanks!

Query-Interface commented 4 years ago

Hello,

I encounter similar issues with HSQLDB.

You may follow what have been done in the petclinic-jpa sample, and this blog post Spring Tips: Spring and GraalVM (pt. 2).

First, remove bytebuddy from your dependencies, see maven pom.xml. And add an hibernate.properties file with the following property: hibernate.bytecode.provider=none

So now, nothing will be generated at startup time. To allow hibernate to work correctly, you will have to generate required artifacts at build time. For this in maven, one can use the hibernate-enhance-maven-plugin.

These steps should help you. Then you may have to add some classes in reflect-config.json if they are not correctly detected.

Regards

ivangfr commented 4 years ago

Hi @Query-Interface , thanks for the tips. I've followed the examples and I was able to build and run the docker image of my app.

Here are my changes (https://github.com/ivangfr/graalvm-quarkus-micronaut-springboot/commit/b88a0d59647a81f733e7dc4494dd11e12b7cf247) in case someone faces the same issue.