java - 构造函数的参数 0 需要一个找不到的 bean
问题描述
我不确定我的代码有什么问题。我正在尝试学习 Spring Boot WebFlux。但我无法运行应用程序,因为我收到以下错误
“ com.thomsoncodes.todo.controller.ToDoController 中构造函数的参数 0 需要一个无法找到的 'com.thomsoncodes.todo.repository.ToDoRespository' 类型的 bean。”
累了@Autowired,但我仍然有同样的错误。
这是我的代码
构建.gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.thomsoncodes.todo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
compileOnly 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}
应用类
package com.thomsoncodes.todo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootTodoWebfluxApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootTodoWebfluxApplication.class, args);
}
}
控制器类
package com.thomsoncodes.todo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.thomsoncodes.todo.domain.ToDo;
import com.thomsoncodes.todo.repository.ToDoRespository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RestController
public class ToDoController {
private ToDoRespository repository;
public ToDoController(ToDoRespository repository) {
this.repository = repository;
}
@GetMapping("/rodo/{id}")
public Mono<ToDo> getTodo(@PathVariable String id) {
return this.repository.findById(id);
}
@GetMapping("/todo")
public Flux<ToDo> getToDos() {
return this.repository.findAll();
}
}
领域类
package com.thomsoncodes.todo.domain;
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.Data;
@Data
public class ToDo {
private String id;
private String description;
private LocalDateTime created;
private LocalDateTime modified;
private boolean completed;
public ToDo() {
this.id = UUID.randomUUID().toString();
this.created = LocalDateTime.now();
this.modified = LocalDateTime.now();
}
public ToDo(String description) {
this();
this.description = description;
}
public ToDo(String description, boolean completed) {
this();
this.description = description;
this.completed = completed;
}
}
存储库类
package com.thomsoncodes.todo.repository;
import java.util.Arrays;
import com.thomsoncodes.todo.domain.ToDo;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public class ToDoRespository {
private Flux<ToDo> toDoFlux = Flux.fromIterable(Arrays.asList(
new ToDo("Do Homework"),
new ToDo("Workout in the morning", true),
new ToDo("Make dinner tonight"),
new ToDo("Clean the studio", true),
new ToDo("Learn spring boot", true)));
public Mono<ToDo> findById(String id) {
return Mono.from(
toDoFlux.filter( todo -> todo.getId().equals(id)));
}
public Flux<ToDo> findAll() {
return toDoFlux;
}
}
解决方案
你需要做一些改变,
ToDoRepository
用@Repository
or注释@Component
ToDoRepository
在@Autowired
控制器类中注释(可选)
推荐阅读
- node.js - 无法使用 npm 卸载软件包?
- python - 将循环中的变量添加到公式
- java - 使用 maven 运行测试时,测试类找不到 Aspectj 类
- angular - Angular 7,虚拟滚动。无法在 max 中设置 scrollTop
- c - 按降序排列数组中的奇数
- .net - dotnet 命令由 Visual Studio 2017 执行
- jsf-2 - 带有 panelGrid 的 bean 实例
- c - 如果用于查找最小数字的基本代码是什么?
- javascript - Vee-validate 仅验证某些字段
- java - MediaPlayer Android Studio 让我的应用运行缓慢