Exception encountered during spring context initialization BeanCreationException: Error creating bean with name 'flywayInitializer'

huangapple go评论90阅读模式
英文:

Exception encountered during spring context initialization BeanCreationException: Error creating bean with name 'flywayInitializer'

问题

在将Spring Boot及其相关包(包括spring-boot-starter-test)从2.2.4.RELEASE迁移到3.1.1后,在运行集成测试时收到以下错误。

在类路径资源com/freit/medicalservice/config/CustomFlywayConfiguration.class中定义的名为flywayInitializer的bean创建错误。

错误的原因是SpringContext为空,可能是由于在测试中使用的flyway配置或注解的某些错误配置导致的。

.properties文件中的Flyway配置如下:

spring.flyway.locations=classpath:db/migration,classpath:/db/test_data,classpath:/db_scripts,com/freit/medicalservice/common/migrations
spring.flyway.enabled=true
spring.flyway.baseline-on-migrate=true
spring.flyway.validate-on-migrate=true

测试文件中使用的注解如下:

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@ActiveProfileTest

错误日志示例:

12:33:50.412 [main] ERROR o.f.core.internal.command.DbMigrate - org.flywaydb.core.internal.logging.slf4j.Slf4jLog.error:45 - { } - Migration of schema medical_service to version "1.1.13 - Rename Logo Files To Header Files" failed! Please restore backups and roll back database and code!
12:33:50.458 [main] WARN o.s.w.c.s.GenericWebApplicationContext - org.springframework.context.support.AbstractApplicationContext.refresh:616 - { } - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [com/freit/medicalservice/config/CustomFlywayConfiguration.class]: Migration failed !
12:33:50.459 [main] INFO com.zaxxer.hikari.HikariDataSource - com.zaxxer.hikari.HikariDataSource.close:350 - { } - HikariPool-1 - Shutdown initiated...
12:33:50.474 [main] INFO com.zaxxer.hikari.HikariDataSource - com.zaxxer.hikari.HikariDataSource.close:352 - { } - HikariPool-1 - Shutdown completed.
12:33:50.513 [main] INFO o.s.b.a.l.ConditionEvaluationReportLogger - org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLogger.logMessage:82 - { } -

错误的根本原因是在com.freit.medicalservice.config.SpringContext.context为空的情况下无法调用org.springframework.context.ApplicationContext.getBean(java.lang.Class)

以上是要翻译的内容。

英文:

Receiving the below error on running integration tests after migrating spring boot and it's related packages(including spring-boot-starter-test) from 2.2.4.RELEASE to 3.1.1.

Error creating bean with name flywayInitializer defined in class path resource
[com/freit/medicalservice/config/CustomFlywayConfiguration.class]

@Configuration
class CustomFlywayConfiguration extends FlywayAutoConfiguration.FlywayConfiguration {
    
    @DependsOn("springContext")
    public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
       return super.flywayInitializer(flyway, null);
    }
}

The error is occuring because SpringContext is null, which could be the result of some miss configuration of flyway or annotations being used in tests.

Flywayconfig in .properties file
spring.flyway.locations=classpath:db/migration,classpath:/db/test_data,classpath:/db_scripts,com/freit/medicalservice/common/migrations
spring.flyway.enabled=true
spring.flyway.baseline-on-migrate=true
spring.flyway.validate-on-migrate=true

Annotations being used in tests files:
@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@ActiveProfileTest

<br>

12:33:50.412 [main] ERROR o.f.core.internal.command.DbMigrate - org.flywaydb.core.internal.logging.slf4j.Slf4jLog.error:45 - {  } - Migration of schema `medical_service` to version &quot;1.1.13 - Rename Logo Files To Header Files&quot; failed! Please restore backups and roll back database and code!
12:33:50.458 [main] WARN  o.s.w.c.s.GenericWebApplicationContext - org.springframework.context.support.AbstractApplicationContext.refresh:616 - {  } - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;flywayInitializer&#39; defined in class path resource [com/freit/medicalservice/config/CustomFlywayConfiguration.class]: Migration failed !
12:33:50.459 [main] INFO  com.zaxxer.hikari.HikariDataSource - com.zaxxer.hikari.HikariDataSource.close:350 - {  } - HikariPool-1 - Shutdown initiated...
12:33:50.474 [main] INFO  com.zaxxer.hikari.HikariDataSource - com.zaxxer.hikari.HikariDataSource.close:352 - {  } - HikariPool-1 - Shutdown completed.
12:33:50.513 [main] INFO  o.s.b.a.l.ConditionEvaluationReportLogger - org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLogger.logMessage:82 - {  } - 
Error starting ApplicationContext. To display the condition evaluation report re-run your application with &#39;debug&#39; enabled.
12:33:50.556 [main] ERROR o.s.boot.SpringApplication - org.springframework.boot.SpringApplication.reportFailure:824 - {  } - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;flywayInitializer&#39; defined in class path resource [com/freit/medicalservice/config/CustomFlywayConfiguration.class]: Migration failed !
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:931)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.test.context.SpringBootContextLoader.lambda$loadContext$3(SpringBootContextLoader.java:137)
at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58)
at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46)
at org.springframework.boot.SpringApplication.withHook(SpringApplication.java:1406)
at org.springframework.boot.test.context.SpringBootContextLoader$ContextLoaderHook.run(SpringBootContextLoader.java:545)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:137)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:108)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:187)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:119)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:127)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:191)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:130)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:241)
at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$10(ClassBasedTestDescriptor.java:377)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:382)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$11(ClassBasedTestDescriptor.java:377)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:310)
at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735)
at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:376)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$instantiateAndPostProcessTestInstance$6(ClassBasedTestDescriptor.java:289)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:288)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$4(ClassBasedTestDescriptor.java:278)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$5(ClassBasedTestDescriptor.java:277)
at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:105)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:104)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:68)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$2(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:90)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:147)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:127)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:90)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:55)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:102)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateException: Migration failed !
at org.flywaydb.core.internal.command.DbMigrate.doMigrateGroup(DbMigrate.java:384)
at org.flywaydb.core.internal.command.DbMigrate.lambda$applyMigrations$1(DbMigrate.java:274)
at org.flywaydb.core.internal.jdbc.TransactionalExecutionTemplate.execute(TransactionalExecutionTemplate.java:55)
at org.flywaydb.core.internal.command.DbMigrate.applyMigrations(DbMigrate.java:273)
at org.flywaydb.core.internal.command.DbMigrate.migrateGroup(DbMigrate.java:246)
at org.flywaydb.core.internal.command.DbMigrate.lambda$migrateAll$0(DbMigrate.java:141)
at org.flywaydb.database.mysql.MySQLNamedLockTemplate.execute(MySQLNamedLockTemplate.java:58)
at org.flywaydb.database.mysql.MySQLConnection.lock(MySQLConnection.java:152)
at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory.lock(JdbcTableSchemaHistory.java:143)
at org.flywaydb.core.internal.command.DbMigrate.migrateAll(DbMigrate.java:141)
at org.flywaydb.core.internal.command.DbMigrate.migrate(DbMigrate.java:98)
at org.flywaydb.core.Flyway.lambda$migrate$0(Flyway.java:176)
at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:204)
at org.flywaydb.core.Flyway.migrate(Flyway.java:128)
at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766)
... 95 common frames omitted
Caused by: org.flywaydb.core.api.FlywayException: Migration failed !
at org.flywaydb.core.internal.resolver.java.JavaMigrationExecutor.executeOnce(JavaMigrationExecutor.java:76)
at org.flywaydb.core.internal.resolver.java.JavaMigrationExecutor.lambda$execute$0(JavaMigrationExecutor.java:54)
at org.flywaydb.core.internal.database.DefaultExecutionStrategy.execute(DefaultExecutionStrategy.java:27)
at org.flywaydb.core.internal.resolver.java.JavaMigrationExecutor.execute(JavaMigrationExecutor.java:53)
at org.flywaydb.core.internal.command.DbMigrate.doMigrateGroup(DbMigrate.java:376)
... 111 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke &quot;org.springframework.context.ApplicationContext.getBean(java.lang.Class)&quot; because &quot;com.freit.medicalservice.config.SpringContext.context&quot; is null
at com.freit.medicalservice.config.SpringContext.getBean(SpringContext.java:20)
at com.freit.medicalservice.common.migrations.V1_1_13__Rename_Logo_Files_To_Header_Files.migrate(V1_1_13__Rename_Logo_Files_To_Header_Files.java:30)
at org.flywaydb.core.internal.resolver.java.JavaMigrationExecutor.executeOnce(JavaMigrationExecutor.java:62)
... 115 common frames omitted
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
-----------------
None
Negative matches:
-----------------
None
Exclusions:
-----------
None
Unconditional classes:
----------------------
None

答案1

得分: 0

我认为你需要完全删除CustomFlywayConfiguration,因为据我所了解,它依赖于com.freit.medicalservice.config.SpringContext,这是你的自定义bean,但FlywayMigrationInitializer是一个基础设施bean。通常,基础设施bean不应该依赖于自定义bean。

如果你仍然需要自定义初始化顺序,请注意,问题出在特定字段com.freit.medicalservice.config.SpringContext.context上,而不是bean本身。也许在com.freit.medicalservice.config.SpringContext的构造函数中有Objects.requireNonNull()?尝试调试创建com.freit.medicalservice.config.SpringContext bean的过程。

英文:

I think you need to remove CustomFlywayConfiguration completely, because as far as I understand in declares dependency on com.freit.medicalservice.config.SpringContext which is your custom bean, but FlywayMigrationInitializer is an infrastructural one. Usually infrastructural beans mustn't depend on custom ones.

If you still need custom initialization order then pat attention, that the problem is about particular field com.freit.medicalservice.config.SpringContext.context and not about the bean itself. Maybe you have Objects.requireNonNull() in constructor of com.freit.medicalservice.config.SpringContext? Try to debug creation of bean com.freit.medicalservice.config.SpringContext.

答案2

得分: 0

刚刚在flywayInitializer()函数上添加了@Bean("flywayInitializer");

@Configuration
class CustomFlywayConfiguration extends FlywayAutoConfiguration.FlywayConfiguration {

@DependsOn("springContext")
@Bean("flywayInitializer")
public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
return super.flywayInitializer(flyway, null);
}

}

英文:

Just added @Bean("flywayInitializer") over flywayInitializer() function;

@Configuration
class CustomFlywayConfiguration extends FlywayAutoConfiguration.FlywayConfiguration {
@DependsOn(&quot;springContext&quot;)
@Bean(&quot;flywayInitializer&quot;)
public FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
return super.flywayInitializer(flyway, null);
}
}

huangapple
  • 本文由 发表于 2023年7月27日 16:07:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定