小编Cha*_*ase的帖子

考虑在配置中定义类型的Bean

我正在关注本教程(https://www.youtube.com/watch?v=Hu-cyytqfp8),并尝试在Spring Boot中连接到远程服务器上的MongoDB。运行应用程序时,我收到以下消息。

说明:com.mongotest.demo.Seeder中构造函数的参数0需要找不到“ com.mongotest.repositories.StudentRepository”类型的Bean。

行动:考虑在配置中定义一个类型为“ com.mongotest.repositories.StudentRepository”的bean。

项目结构。

在此处输入图片说明

这是我的课

    @Document(collection = "Students")
    public class Student {

        @Id
        private String number;
        private String name;
        @Indexed(direction = IndexDirection.ASCENDING)
        private int classNo;

    //Constructor and getters and setters.
    }

    ================================

    @Repository
    public interface StudentRepository extends MongoRepository<Student, String>{

    }

    ================================

    @Component
    @ComponentScan({"com.mongotest.repositories"})
    public class Seeder implements CommandLineRunner{

        private StudentRepository studentRepo;

        public Seeder(StudentRepository studentRepo) {
            super();
            this.studentRepo = studentRepo;
        }

        @Override
        public void run(String... args) throws Exception {
            // TODO Auto-generated method stub

            Student s1 …
Run Code Online (Sandbox Code Playgroud)

java spring mongodb spring-boot

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

mongodb ×1

spring ×1

spring-boot ×1