简体   繁体   English

构造函数的参数 0 需要一个找不到的 bean

[英]Parameter 0 of constructor required a bean that could not be found

I am not sure what is wrong with my codes.我不确定我的代码有什么问题。 I was trying to learn Spring Boot WebFlux.我正在尝试学习 Spring Boot WebFlux。 But I am not able to run the application as i get the below error但我无法运行应用程序,因为我收到以下错误

" Parameter 0 of constructor in com.thomsoncodes.todo.controller.ToDoController required a bean of type 'com.thomsoncodes.todo.repository.ToDoRespository' that could not be found." “ com.thomsoncodes.todo.controller.ToDoController 中的构造函数参数 0 需要一个无法找到的 'com.thomsoncodes.todo.repository.ToDoRespository' 类型的 bean。”

Tired @Autowired but still I have the same error.累了@Autowired,但我仍然有同样的错误。

Here is my code这是我的代码

build.gradle build.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'
}

Application class应用 class

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);
 }

}

Controller class Controller class

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();
   }
}

domain class域 class

 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;
  }
}

Repository class存储库 class

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;
  }


}

You need to make a couple of changes,你需要做一些改变,

  1. Annotate ToDoRepository with @Repository or @Component注释ToDoRepository@Repository@Component
  2. Annotate ToDoRepository with @Autowired in controller class (optional)在控制器类中使用@Autowired注释ToDoRepository (可选)

ToDoRepository 未标记为@Component。

Hystrix Hystrix

In CitcuitBreakClient, I put @Component but still i have issue of bean not found 在CitcuitBreakClient中,我放了@Component,但仍然有找不到bean的问题

 public class CircuitBreakerService {

 //@Service
 @Component
 public class BookService {

  private final RestTemplate restTemplate;

  public BookService(RestTemplate rest) {
    this.restTemplate = rest;
  }

  @HystrixCommand(fallbackMethod = "reliable")
  public String readingList() {
    URI uri = URI.create("http://localhost:8060/recommended");

    return this.restTemplate.getForObject(uri, String.class);
  }

  public String reliable() {
    return "Cloud not navigate to Circuit Breaker Server";
  }

}

@RestController
@CrossOrigin
@RequestMapping("/read")
public class CircuitBreakerController {

BookService bookService;

@Autowired
CircuitBreakerController(BookService bs){
    this.bookService=bs;
}

@RequestMapping("/get")
public String toRead() {
    return bookService.readingList();
  }

}

Yeah, found out adding @AutoConfiguration to the class where the issue is happening usually solves the problem for this bug.是的,发现将@AutoConfiguration 添加到发生问题的 class 通常可以解决此错误的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &quot;&quot; 中构造函数的参数 0 需要一个无法找到的类型为 &quot;&quot; 的 bean - Parameter 0 of constructor in "" required a bean of type "" that could not be found “ ”中构造函数的参数 0 需要找不到类型为“ ”的 bean - Parameter 0 of constructor in ' ' required a bean of type ' ' that could not be found 构造函数中的参数 3 需要一个无法找到的“包”类型的 bean - Parameter 3 of constructor in required a bean of type 'package' that could not be found Controller 中构造函数的参数 0 需要一个找不到的 Service class 类型的 bean - Parameter 0 of constructor in Controller required a bean of type Service class that could not be found 服务中构造函数的参数 0 需要无法找到类型存储库的 bean - Parameter 0 of constructor in service required a bean of type repository that could not be found ..RoleService 中构造函数的参数 0 需要一个名为“entityManagerFactory”的 bean,但无法找到 - Parameter 0 of constructor in ..RoleService required a bean named 'entityManagerFactory' that could not be found “服务”中构造函数的参数 0 需要找不到类型为“存储库”的 bean - Parameter 0 of constructor in 'Service' required a bean of type 'Repository' that could not be found *Service 中构造函数的参数 0 需要找不到类型为“*Repository”的 bean - Parameter 0 of constructor in *Service required a bean of type '*Repository' that could not be found B类中构造函数的参数0需要一个找不到A类类型的Bean - Parameter 0 of constructor in Class B required a bean of type Class A that could not be found ... 中构造函数的参数 5 需要找不到类型为“...Mapper”的 bean - Parameter 5 of constructor in … required a bean of type '…Mapper' that could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM