Micronaut GraalVM 本机图像中没有 [com.mongodb.client.MongoClient] 类型的 bean

No bean of type [com.mongodb.client.MongoClient] in Micronaut GraalVM native-image

我在 GraalVm 本机图像(Micronaut 框架)中遇到 MongoDB 问题。当我使用 IDE 和 .jar 进行部署时,一切正常,没有任何异常。当我使用 Micronauts 启动器 ./gradlew dockerBuildNative 生成的标准 Gradle 任务创建本机图像并尝试从数据库中获取实体时,找不到 MongoClient。 这是我的 gradle.build

    plugins {
        id("com.github.johnrengelman.shadow") version "7.0.0"
        id("io.micronaut.application") version "2.0.3"
    }
    
    version = "0.1"
    group = "com.project"
    
    repositories {
        mavenCentral()
    }
    
    micronaut {
        runtime("lambda")
        runtime("tomcat")
        testRuntime("junit5")
        processing {
            incremental(true)
            annotations("com.project*")
        }
    }
    
    dependencies {
    
        implementation 'io.micronaut:micronaut-inject:1.0.5'
    
        implementation("io.micronaut:micronaut-http-client")
        implementation("io.micronaut:micronaut-http-server")
        implementation("io.micronaut:micronaut-runtime")
        implementation("io.micronaut.mongodb:micronaut-mongo-sync")
        implementation("io.micronaut.mongodb:micronaut-mongo-reactive")
        implementation("io.swagger.core.v3:swagger-annotations")
        implementation("javax.annotation:javax.annotation-api")
    
        implementation("io.micronaut:micronaut-validation")
    
        implementation("io.micronaut.aws:micronaut-function-aws-custom-runtime")
    
        compileOnly("org.projectlombok:lombok")
        runtimeOnly("ch.qos.logback:logback-classic")
        testAnnotationProcessor("io.micronaut.spring:micronaut-spring-annotation")
        testAnnotationProcessor("io.micronaut.spring:micronaut-spring-boot")
        testAnnotationProcessor("io.micronaut.spring:micronaut-spring-web-annotation")
        testImplementation("org.testcontainers:junit-jupiter")
        testImplementation("org.testcontainers:mongodb")
        testImplementation("org.testcontainers:testcontainers")
        compileOnly("org.graalvm.nativeimage:svm")
    
        annotationProcessor "io.micronaut:micronaut-inject-java"
        annotationProcessor "io.micronaut:micronaut-graal"
        annotationProcessor("org.projectlombok:lombok")
        annotationProcessor("io.micronaut:micronaut-http-validation")
        annotationProcessor("io.micronaut.openapi:micronaut-openapi")
        annotationProcessor("io.micronaut.security:micronaut-security-annotations")
    
    }
    
    
    application {
    //    mainClass.set("io.micronaut.function.aws.runtime.MicronautLambdaRuntime")
        m

ainClass.set("com.project.ProjectBackend")
}
jar {
    manifest {
        attributes(
                'Main-Class': 'com.project.ProjectBackend'
        )
    }
}
java {
    sourceCompatibility = JavaVersion.toVersion("11")
    targetCompatibility = JavaVersion.toVersion("11")
}

这是我的 DroneService.class 我试图从 MongoDB

获取实体的地方
@Singleton
public class DroneService {
    private MongoClient mongoClient;

    @Inject
    public DroneService(MongoClient mongoClient) {
        this.mongoClient = mongoClient;
    }

    public Drone findById() {
        return mongoClient
                .getDatabase("project_db")
                .getCollection("drones", Drone.class)
                .find(Filters.eq("_id", new ObjectId("61011ceaed86d207214941aa")))
                .first();
    }
}

我的application.yml

micronaut:
  application:
    name: project
mongodb:
  uri: mongodb://admin:admin@localhost:27017/project_db?authSource=admin

日志

|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.0.0)

Sep 01, 2021 8:33:58 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Sep 01, 2021 8:33:58 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Sep 01, 2021 8:33:58 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.38]
Sep 01, 2021 8:33:58 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
17:33:58.835 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 10ms. Server Running: http://0.0.0.0:8080
17:34:05.735 [http-nio-8080-exec-1] ERROR i.m.servlet.http.ServletHttpHandler - Unexpected error occurred: Failed to inject value for parameter [mongoClient] of class: com.project.service.DroneService

Message: No bean of type [com.mongodb.client.MongoClient] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Path Taken: new BookController() --> BookController.droneService --> new DroneService([MongoClient mongoClient])
io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [mongoClient] of class: com.project.service.DroneService

Message: No bean of type [com.mongodb.client.MongoClient] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Path Taken: new BookController() --> BookController.droneService --> new DroneService([MongoClient mongoClient])
    at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBean(AbstractInitializableBeanDefinition.java:1583)
    at io.micronaut.context.AbstractInitializableBeanDefinition.getBeanForConstructorArgument(AbstractInitializableBeanDefinition.java:1044)
    at com.project.service.$DroneService$Definition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2238)
    at io.micronaut.context.DefaultBeanContext.createAndRegisterSingletonInternal(DefaultBeanContext.java:3193)
    at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:3179)
    at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:2723)
    at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2685)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1555)
    at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBean(AbstractInitializableBeanDefinition.java:1561)
    at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBean(AbstractInitializableBeanDefinition.java:1549)
    at io.micronaut.context.AbstractInitializableBeanDefinition.getBeanForField(AbstractInitializableBeanDefinition.java:1259)
    at com.project.$BookController$Definition.injectBean(Unknown Source)
    at com.project.$BookController$Definition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2238)
    at io.micronaut.context.DefaultBeanContext.createAndRegisterSingletonInternal(DefaultBeanContext.java:3193)
    at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:3179)
    at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:2723)
    at io.micronaut.context.DefaultBeanContext.access0(DefaultBeanContext.java:75)
    at io.micronaut.context.DefaultBeanContext.getTarget(DefaultBeanContext.java:460)
    at io.micronaut.context.DefaultBeanContext.invoke(DefaultBeanContext.java:503)
    at io.micronaut.web.router.AbstractRouteMatch.execute(AbstractRouteMatch.java:246)
    at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:111)
    at io.micronaut.http.context.ServerRequestContext.with(ServerRequestContext.java:128)
    at io.micronaut.servlet.http.ServletHttpHandler.lambda$buildResponsePublisher(ServletHttpHandler.java:560)
    at reactor.core.publisher.FluxCreate.subscribe(FluxCreate.java:94)
    at reactor.core.publisher.InternalFluxOperator.subscribe(InternalFluxOperator.java:62)
    at reactor.core.publisher.FluxSubscribeOn$SubscribeOnSubscriber.run(FluxSubscribeOn.java:194)
    at reactor.core.scheduler.ImmediateScheduler$ImmediateSchedulerWorker.schedule(ImmediateScheduler.java:84)
    at reactor.core.publisher.FluxSubscribeOn.subscribeOrReturn(FluxSubscribeOn.java:62)
    at reactor.core.publisher.Flux.subscribe(Flux.java:8388)
    at reactor.core.publisher.Flux.subscribeWith(Flux.java:8575)
    at reactor.core.publisher.Flux.subscribe(Flux.java:8372)
    at reactor.core.publisher.Flux.subscribe(Flux.java:8296)
    at reactor.core.publisher.Flux.subscribe(Flux.java:8266)
    at io.micronaut.servlet.http.ServletHttpHandler.subscribeToResponsePublisher(ServletHttpHandler.java:466)
    at io.micronaut.servlet.http.ServletHttpHandler.invokeRouteMatch(ServletHttpHandler.java:401)
    at io.micronaut.servlet.http.ServletHttpHandler.service(ServletHttpHandler.java:227)
    at io.micronaut.servlet.engine.DefaultServletHttpHandler.service(DefaultServletHttpHandler.java:53)
    at io.micronaut.servlet.engine.DefaultMicronautServlet.service(DefaultMicronautServlet.java:66)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:829)
    at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:567)
    at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:192)
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [com.mongodb.client.MongoClient] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
    at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2698)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1555)
    at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBean(AbstractInitializableBeanDefinition.java:1561)
    ... 60 common frames omitted

有什么想法吗?

找到解决方案。 Docker 忽略了 .properties 文件,并且由于缺少连接 uri 而未创建 MongoClient bean。我的解决方案 - 使用 graal SDK 构建本机映像,而不是在 docker 容器内构建。