gpt4 book ai didi

java - SpringBoot : Unsatisfied dependency NoSuchBeanDefinitionException, 期望至少有 1 个有资格作为 Autowiring 候选者的 bean

转载 作者:行者123 更新时间:2023-11-30 05:38:58 26 4
gpt4 key购买 nike

我犯了一个非常愚蠢的错误,但不知道如何修复。

我有一个使用配置文件的简单 SpringBoot 应用程序,它连接到 MongoDb。

我的pom.xml依赖项:

<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>
</dependencies>

我的StudentController.java

@RestController
@RequestMapping("/students")
public class StudentController {

@Autowired
private StudentService studentService;

@RequestMapping(method = RequestMethod.GET)
public Collection<Student> getAllStudents(){
return studentService.getAllStudents();
}
}

我的StudentService.java

@Service
public class StudentService {

@Autowired
private StudentDao studentDao;

public Collection<Student> getAllStudents(){
return this.studentDao.getAllStudents();
}
}

我的StudentDao.java接口(interface):

public interface StudentDao {
Collection<Student> getAllStudents();
}

我的MongoStudentDaoImpl.java:

@Repository
@Profile("test")
public class MongoStudentDaoImpl implements StudentDao {

@Autowired
private MongoStudentRepo repo;

@Override
public Collection<Student> getAllStudents() {
return repo.findAll();
}
}

我的MongoStudentRepo.java:

@Profile("test")
public interface MongoStudentRepo extends MongoRepository<Student, String> {
}

当我尝试使用“测试”配置文件启动应用程序时,这是我看到的错误:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentService': Unsatisfied dependency expressed through field 'studentDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoStudentDaoImpl': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'MongoStudentRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我在这里缺少什么?我需要向 MongoStudentRepo.java 添加注释吗?

提前致谢。

最佳答案

堆栈跟踪显示 spring 无法 Autowiring MongoStudentDaoImpl.java 类中的 bean MongoStudentRepo 之一。来自堆栈跟踪:

 Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'MongoStudentRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 

Spring 抛出此异常意味着 MongoStudentRepo.class 的 bean 未正确创建。

可能的解决方案:

  • 问题可能是因为 spring 没有使用 spring 中的默认实现为您的接口(interface)创建 bean。这可能是因为您没有使用注释 @EnableJpaRepositories 来启用默认的扫描和创建存储库 bean。有关更多信息,请阅读 here.

但是如果您使用基于 xml 的配置,对于基于 xml 的配置使用:

<repositories base-package="com.acme.repository" />

<repositories base-package="com.acme.repository" repository-impl-postfix="MyPostfix" />

或者使用@Configuration注释:

@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {

@Bean
EntityManagerFactory entityManagerFactory() {
// …
}
}

当您使用 spring 默认实现时,您只能为“测试”配置文件加载此配置。

最佳阅读:https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.custom-implementations

关于java - SpringBoot : Unsatisfied dependency NoSuchBeanDefinitionException, 期望至少有 1 个有资格作为 Autowiring 候选者的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56069017/

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