我在GraalVm native-image(Micronaut框架)中遇到了MongoDB的问题。当我使用IDE和。jar进行部署时,所有工作都正常,没有任何异常。当我使用由Micronauts launcher./gradlew DockerBuildNative
生成的标准Gradle任务创建本机映像并试图从数据库中获取实体时,找不到MongoClient
。这是我的成绩。建造
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();
}
}
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.access$100(DefaultBeanContext.java:75)
at io.micronaut.context.DefaultBeanContext$4.getTarget(DefaultBeanContext.java:460)
at io.micronaut.context.DefaultBeanContext$4.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$18(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容器内构建。
问题内容: 我正在使用MySQL作为数据库的Spring JPA应用程序。我确保已加载所有spring-jpa库,hibernate和mysql-connector- java。 我正在运行mysql 5实例。这是我的application.properties文件的摘录: 执行集成测试时,spring可以正常启动,但无法创建hibernate的SessionFactory,但以下情况除外: 我认
虽然这个标题存在一些问题,但我的查询无法从这些线程中解决。 我在postgres中通过hibernate执行递归(使用with子句)查询,查询结果也包含搜索路径 ex:一行查询结果 Hibernate是否具有除字符串以外的的任何映射类型,类似于或。 下面是查询输出的示例 Hibernate正在抛出异常 原因:org.hibernate.MappingException:没有JDBC类型的方言映射:
我已经成功地用Quarkus/Graal构建了一个本机映像,我可以在终端中运行它(还没有容器)。但是,它会因“分段错误(堆芯转储)”消息而崩溃。如何获得有关错误的更多信息?是否有标志要传递给本机可执行文件或在构建映像时传递? 我使用的是GraalVM版本20.2.0(Java版本11.0.8) 任何帮助都是非常感激的。 谢谢
问题内容: 尽管此标题存在一些问题,但我的查询并没有从那些线程中解决。 我正在通过postgres中的hibernate执行递归查询(与子句一起使用),查询结果也包含搜索路径 例如:查询结果的一行 hibernate状态是否具有String以外的任何映射类型,类似于或。 下面是查询输出的示例 Hibernate抛出异常 原因:org.hibernate.MappingException:没有JDB
我正在开发一个Spring JPA应用程序,使用MySQL作为数据库。我确保加载了所有spring-jpa库、hibernate和mysql-connector-java。 期待您的回答,谢谢! 顺便说一句,应用程序已经在使用spring Boot了。
好吧,我是Docker的新手,正在做一些教程,我想删除和图像: 所以我从开始: 为什么这不起作用? 最后我发现确实删除了图像,但我仍然不明白为什么这不起作用。 第一列是什么?这是映像名称,还是其他名称? 如果是映像名称,那么为什么不能使用删除映像? 如果不是映像名称,则 是什么? 为什么显示它而不是图像名称? 如何获取实际的映像名称?具体是什么?我还尝试了和但未成功。