[英]How do I disable MongoDB establishing a connection on start in a Spring Boot application?
I've got a Spring Boot application that has two different database implementations, one is SQL, one is NO-SQL (MongoDB).我有一个 Spring 引导应用程序,它有两种不同的数据库实现,一种是 SQL,一种是 NO-SQL (MongoDB)。 The SQL implementation is default and I am not using currently the NO-SQL one.
SQL 实现是默认实现,我目前没有使用 NO-SQL 实现。 However, the application tries to connect to a MongoDB server and shows an error if one is not available.
但是,应用程序尝试连接到 MongoDB 服务器,如果不可用,则会显示错误。
I tried the following:我尝试了以下方法:
SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
However, in this case I get the following error:但是,在这种情况下,我收到以下错误:
Field operations in ...mongodb.CustomThingRepositoryImpl required a bean of type 'org.springframework.data.mongodb.core.MongoOperations' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.data.mongodb.core.MongoOperations' in your configuration.
My MongoDB Repository classes look as follows:我的 MongoDB 存储库类如下所示:
public interface SpringDataMongoThingRepository extends MongoRepository<ThingDocument, String>,
CustomThingRepository<ThingDocument, String> {
}
public interface CustomThingRepository<T, ID> {
[...]
<S extends T> S save(S entity);
void deleteById(ID id);
}
@Component
public class CustomThingRepositoryImpl<T, ID> implements CustomThingRepository<T, ID> {
@Autowired
private MongoOperations operations;
@Autowired
private ThingDataStorageProcessor thingDataStorageProcessor;
[...]
@Component
public class ThingDataStorageProcessor {
@Autowired
private GridFsOperations gridFsOperations;
[...]
I tried providing dummy implementations of MongoOperations and GridFsOperations but I still get an error:我尝试提供 MongoOperations 和 GridFsOperations 的虚拟实现,但仍然出现错误:
Description:
A component required a bean named 'mongoTemplate' that could not be found.
Action:
Consider defining a bean named 'mongoTemplate' in your configuration.
How can I at least prevent MongoDB from connecting on startup?我怎样才能至少防止 MongoDB 在启动时连接?
This has worked for me:这对我有用:
@Repository
@ConditionalOnProperty(prefix = "spring.data.mongodb", name = "uri")
public interface SpringDataMongoThingRepository...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.