java - 构建 spring boot 项目而不实例化 mongo

标签 java spring mongodb spring-boot

我有一个带有 mongo 依赖项的 Spring Boot 项目。建筑使用mvn clean package使用 spring.data.mongodb.host=localhost 工作但是当我使用spring.data.mongodb.host=<remote-ip-address>时它失败。由于我无法访问本地计算机上的 IP 地址,我可以在 Spring 不尝试连接到 mongo 的情况下构建 jar 吗?

现在出现以下错误 -

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=10.37.130.100:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Exception receiving message}, caused by {java.net.SocketException: Operation timed out}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=10.37.130.100:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Exception receiving message}, caused by {java.net.SocketException: Operation timed out}}]

最佳答案

默认情况下,在 Spring Boot 应用程序的构建中,有一个测试,用于检查上下文,并且它已打开。只需关闭它(-DskipTests 属性)即可。

或者使用 -Dspring.data.mongodb.host=localhost 进行构建以进行集成测试。

关于java - 构建 spring boot 项目而不实例化 mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46004572/

相关文章:

java - 我有一个字符串在 spring 上下文中包含 ${key}

java - 从命令行启动应用程序时,如何将 Spring Boot 应用程序 jar 中的文件引用为 -D 属性值?

mongodb - PouchDB/CouchDB 类似 MongoDB 的替代品

c# - 接口(interface)会在子类中自动实现吗?

java - 如何将选定的 Maven 配置文件传递到 Spring 配置文件?

java - 使用 spring 查询数据库中的列时出错

node.js - Angular 使用错误的 URL 通过 NodeJS 查询 MongoDB

mongodb - 如何使用 Perl 对 mongodb 中的多个字段进行排序?

java - 小对象池是否比 Android 的 Java 垃圾收集器更有效?

java - 带有postgres的sql2o,查询没有返回结果