简体   繁体   English

如何在 Spring 引导应用程序中禁用 MongoDB 在启动时建立连接?

[英]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.

相关问题 在 Spring 启动应用程序中配置 MongoDB 连接池 - Configure MongoDB connection pooling in Spring Boot application java spring boot中如何在启动时不尝试数据库连接? - How can I do in java spring boot not try a database connection on start up? 如何在不使用ComponentScan的情况下启动Spring Boot Web应用程序 - How do I start a Spring Boot Web Application without using ComponentScan 我们如何知道,如果Spring Boot应用程序正在使用连接池 - How to do we know , if spring boot application is using connection pool 如何使用配置文件在Docker中启动Spring Boot应用程序? - How can I start spring boot application in docker with profile? 如何通过Spring Boot在mongoDB中获得计数? - How do I get the count in mongoDB through Spring Boot? 我如何在 mongodb spring boot 中进行排序和搜索 - How can I sort and do a search in mongodb spring boot 如何禁用Spring Data MongoDB文档的字段映射? - How do I disable mapping of fields for Spring Data MongoDB documents? 如何在 Spring 引导应用程序中禁用 302 重定向 - How to disable 302 redirect in Spring Boot application 如何使用mongodb应用程序在spring boot中修复UnsatisfiedDependencyException? - How to fix UnsatisfiedDependencyException in spring boot with mongodb application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM