【问题标题】:How to disable spring-data-mongodb in spring-boot如何在 spring-boot 中禁用 spring-data-mongodb
【发布时间】:2019-05-02 23:54:48
【问题描述】:

我是 SpringBoot 的新手。我已经构建了一个简单的应用程序,它应该在开发环境中使用假数据,并在测试环境中连接到 MongoDb。开发环境没有mongodb设置。

我已经尝试使用 Spring Boot 限定符/配置文件来实现它。

我有一个主类,如下所示:

@SpringBootApplication
public class Main {
   public static void main(String[] args) {
       SpringApplication.run(Main.class, args);
   }
}

我有一个DAO接口StudentDao.java

public interface StudentDao {
    Student getStudentById(String id);
}

然后我为 DAO 创建了几个实现,一个用于伪造数据,一个用于来自 Mongo 的数据

FakeStudentDaoImpl.java

@Repository
@Qualifier("fakeData")
public class FakeStudentDaoImpl implements StudentDao {

    private static Map<String, Student> students;

    static {

        students = new HashMap<String, Student>(){
            {
                put("1", new Student("Ram", "Computer Science"));
            }
        };
    }

    @Override
    public Student getStudentById(String id){
        return this.students.get(id);
    }
}

MongoStudentDaoImpl.java

@Repository
@Qualifier("mongoData")
public class MongoStudentDaoImpl implements StudentDao {

    @Autowired
    private MongoStudentRepo repo;

    @Override
    public Student getStudentById(String id) {
        return  repo.findById(id).get();
    }
}

MongoStudentRepo 是一个扩展 MongoRepository 的简单接口:

public interface MongoStudentRepo extends MongoRepository<Student, String> {
}

并且我的 POM 文件调用了以下依赖项:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

当然,我还有其他控制器类。 这在测试环境中运行良好,那里有一个 MongoDb,并且它能够连接到它。但是,当我尝试在本地环境中启动它时,它无法启动,因为它在启动时没有找到 MongoDb。

如何在我的本地环境中禁用 MongoDb 部分(并且只使用假数据)?我想让相同的代码在两种环境中工作。

提前致谢。

【问题讨论】:

  • 对于local 和dev 环境只需在内存中使用H2 数据库,您不需要更改配置以外的任何内容
  • 你试过嵌入式 mongodb 数据库吗?
  • 或者,您可以考虑使用嵌入式 mongodb 进行开发,或者使用 docker 运行 mongodb 实例(我就是这样做的)。请参阅:baeldung.com/spring-boot-embedded-mongodb。这将节省您创建和维护虚假存储库等的时间。

标签: java spring mongodb spring-boot spring-boot-starter


【解决方案1】:

您可以使用嵌入式 MongoDB 数据库。 Here 一个例子。

【讨论】:

    【解决方案2】:

    几个可能的选项:

    1) 你可以使用弹簧profiles。用@Profile("test) 映射一个bean,用@Profile("prod") 映射第二个bean。指定使用哪个配置文件--spring.profiles.active=test

    2) 你可以有不同的configurations。

    application-prod.yml
    --------------
    mongo-url:produrl
    
    application-test.yml
    --------------
    mongo-url:localhost
    

    使用 spring active 配置文件来选择配置。要使用本地配置文件,您需要设置本地 mongo 实例。你可以再次有几个选择:just download instance、docker image、embeded mongo。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,并在另一个问题中找到了您要求的解决方案:

      Spring Boot. How to disable Initialization of JPA Conditionaliy

      例如,如果您想在开发环境中禁用 spring-data-mongodb,那么,假设您在“dev”配置文件下运行:

      application-dev.yml:

      spring:
        autoconfigure:
          exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
      

      【讨论】:

        猜你喜欢
        • 2015-04-29
        • 2020-08-04
        • 2019-10-05
        • 2014-10-21
        • 2014-11-17
        • 2015-11-16
        • 1970-01-01
        • 2015-10-07
        • 2016-12-10
        相关资源
        最近更新 更多