我有一个带有 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/