gpt4 book ai didi

java - 考虑在您的配置中定义一个类型的 bean

转载 作者:可可西里 更新时间:2023-11-01 09:11:12 29 4
gpt4 key购买 nike

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

Description: Parameter 0 of constructor in com.mongotest.demo.Seeder required a bean of type 'com.mongotest.repositories.StudentRepository' that could not be found.

Action: Consider defining a bean of type 'com.mongotest.repositories.StudentRepository' in your configuration.

项目结构。

enter image description here

这是我的类(class)

    @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 = new Student("1","Tom",1);
Student s2 = new Student("2","Jerry",1);
Student s3 = new Student("3","Kat",2);
studentRepo.deleteAll();
studentRepo.save(Arrays.asList(s1,s2,s3));

}

}

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

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

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mongotest</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<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>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

最佳答案

请在DemoApplication中添加如下注释

@SpringBootApplication
@ComponentScan("com.mongotest") //to scan packages mentioned
@EnableMongoRepositories("com.mongotest") //to activate MongoDB repositories
public class DemoApplication { ... }

关于java - 考虑在您的配置中定义一个类型的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240271/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com