我正在使用 springboot 创建一个应用程序,后端也使用 micronaut 和 micronaut beans。前端正在使用 vaadin。
如果我将应用程序作为 bootrun 或在 mvn spring-boot:run 中运行,则应用程序正常启动,并且 micronaut bean 会成功注入到 spring 上下文中
但是,当我将应用程序打包为 bootjar 并使用 java -jar myapp.jar 启动应用程序时,会出现以下错误:
22:31:25.693 [main] ERROR o.s.boot.SpringApplication - Application run failed
io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [io.micronaut.context.event.ApplicationEventPublisher<io.micronaut.context.event.StartupEvent>] exists.
at io.micronaut.context.DefaultBeanContext.newNoSuchBeanException(DefaultBeanContext.java:2790)
at io.micronaut.context.DefaultApplicationContext.newNoSuchBeanException(DefaultApplicationContext.java:318)
at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2753)
at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1747)
at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:842)
at io.micronaut.context.BeanLocator.getBean(BeanLocator.java:97)
at io.micronaut.context.DefaultBeanContext.publishEvent(DefaultBeanContext.java:1833)
at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:360)
at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:211)
at io.micronaut.spring.boot.starter.MicronautImportRegistrar.registerBeanDefinitions(MicronautImportRegistrar.java:128)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:376)
at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:721)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:375)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:148)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:429)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:290)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:349)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:118)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:788)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:334)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
at co.yaqut.server.user.controlpanel.UsersControlPanelApplication.main(UsersControlPanelApplication.java:21)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:569)
at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:91)
at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:53)
at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:58)
我认为打包应用程序时缺少一些东西。我没主意了。任何帮助都会非常好。
此处提供了一个示例应用程序 https://github.com/firdouserp/micronaut-in-spring
我得到的所有设置的文章来自这里: https://guides.micronaut.io/latest/spring-boot-micronaut-data-gradle-java.html
快速搜索会导致 Micronaut 问题跟踪器中的此问题。所以这似乎是一个已知问题。
添加以下内容即可解决。
tasks.bootJar { loaderImplementation = org.springframework.boot.loader.tools.LoaderImplementation.CLASSIC }
接下来,我建议对您的依赖项进行一些整理。您正在使用为您管理版本的平台 pom,这将降低使用不兼容版本的风险。
dependencies {
implementation platform("com.vaadin:vaadin-bom:24.4.6")
implementation platform("io.micronaut.platform:micronaut-platform:4.6.3")
annotationProcessor platform("io.micronaut.platform:micronaut-platform:4.6.3")
annotationProcessor("io.micronaut:micronaut-inject-java")
annotationProcessor("io.micronaut.data:micronaut-data-processor")
annotationProcessor("jakarta.annotation:jakarta.annotation-api")
annotationProcessor("jakarta.persistence:jakarta.persistence-api")
implementation("io.micronaut.data:micronaut-data-jdbc")
implementation("io.micronaut.spring:micronaut-spring-boot-starter")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
implementation("jakarta.annotation:jakarta.annotation-api")
implementation("jakarta.persistence:jakarta.persistence-api")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("jakarta.annotation:jakarta.annotation-api")
//implementation("com.vaadin:vaadin-core")
// https://mvnrepository.com/artifact/com.vaadin/vaadin-spring-boot-starter
implementation 'com.vaadin:vaadin-spring-boot-starter'
implementation fileTree(dir: 'libs', includes: ['*.jar'])
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
这种组合似乎有效。