我用的是Flyway 8.5和Springboot 2.7。我试图在完成flyway的迁移后从服务调用函数(即在After_Migrate事件处),其再次调整数据库。
@Configuration
public class FlywayConfiguration implements FlywayConfigurationCustomizer {
@Override
public void customize(FluentConfiguration configuration) {
configuration.callbacks(new FlywayCallback());
}
}
public class FlywayCallback extends BaseCallback {
@Autowired
@Lazy
private ProfileService profileService;
@Override
public void handle(Event event, Context context) {
if (event == Event.AFTER_MIGRATE) {
profileService.setMissingThumbnailsForProfiles();
}
}
}
然而,它并不完全工作,因为profileService总是null。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Error while executing afterMigrate callback: null
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1156) ~[spring-context-5.3.27.jar:5.3.27]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:910) ~[spring-context-5.3.27.jar:5.3.27]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.27.jar:5.3.27]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.11.jar:2.7.11]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~[spring-boot-2.7.11.jar:2.7.11]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.11.jar:2.7.11]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.7.11.jar:2.7.11]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.7.11.jar:2.7.11]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.7.11.jar:2.7.11]
at de.openknowledge.mipro2.Mipro2Application.main(Mipro2Application.java:11) ~[classes/:na]
Caused by: org.flywaydb.core.api.FlywayException: Error while executing afterMigrate callback: null
at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.handleEvent(DefaultCallbackExecutor.java:136) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.execute(DefaultCallbackExecutor.java:129) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.lambda$execute$0(DefaultCallbackExecutor.java:116) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.internal.jdbc.TransactionalExecutionTemplate.execute(TransactionalExecutionTemplate.java:55) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.execute(DefaultCallbackExecutor.java:114) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.onMigrateOrUndoEvent(DefaultCallbackExecutor.java:70) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.internal.command.DbMigrate.migrate(DbMigrate.java:114) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.Flyway$1.execute(Flyway.java:172) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.Flyway$1.execute(Flyway.java:124) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:205) ~[flyway-core-8.5.13.jar:na]
at org.flywaydb.core.Flyway.migrate(Flyway.java:124) ~[flyway-core-8.5.13.jar:na]
at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-2.7.11.jar:2.7.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.27.jar:5.3.27]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.27.jar:5.3.27]
... 18 common frames omitted
Caused by: java.lang.NullPointerException: null
at de.openknowledge.mipro2.infrastructure.FlywayCallback.handle(FlywayCallback.java:20) ~[classes/:na]
at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.handleEvent(DefaultCallbackExecutor.java:134) ~[flyway-core-8.5.13.jar:na]
... 31 common frames omitted
Process finished with exit code 1
我还尝试了This方法,这导致了一个循环bean异常。
1条答案
按热度按时间hc8w905p1#
@Autowired
在创建FlywayCallback
的示例时被忽略,Spring对此一无所知:这意味着它不是Spring管理的bean。要使其成为Spring管理的bean,您可以假设其包被组件扫描覆盖,使用
@Component
对其进行注解。或者,您可以从@Configuration
类中的@Bean
方法返回它的示例。