java - 如何在 spring-boot 中禁用 spring-data-mongodb

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

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

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

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

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

我有一个 DAO 接口(interface) 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 的简单接口(interface):

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>

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

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

提前致谢。

最佳答案

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

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

关于java - 如何在 spring-boot 中禁用 spring-data-mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55961632/

相关文章:

java - Android 客户端连接到 RestFul Webservice

Java 内存模型保证 volatile

java - 组件扫描未找到@Repository

java - HttpPost 添加到 url HTTP/1.1

spring - 在集成测试中使用 Spring @ActiveProfile

node.js - 即使在版本 4.11.1 之后也警告 Db.prototype.authenticate 方法

node.js - Meteor远程ddp连接集合不允许更新操作

java - 迭代二维数组错误

java - 处理 null 的优雅方式——在 2 个变量中只需要单个 null 值

ios - 当我想使用“时,在 Swift 中 joinWithSeparator 的正确用法是什么?