Controller 中构造函数的参数 0 需要一个找不到的 Service 类类型的 bean
Posted
技术标签:
【中文标题】Controller 中构造函数的参数 0 需要一个找不到的 Service 类类型的 bean【英文标题】:Parameter 0 of constructor in Controller required a bean of type Service class that could not be found 【发布时间】:2020-02-23 11:15:47 【问题描述】:应用程序启动失败
Description:
Parameter 0 of constructor in edu.rohit.circuitbreakclient.controller.CircuitBreakerController required a bean of type 'edu.rohit.circuitbreakclient.service.CircuitBreakerService$BookService' that could not be found.
Action:
Consider defining a bean of type 'edu.rohit.circuitbreakclient.service.CircuitBreakerService$BookService' in your configuration.
CircuitBreakerService
如下。
@Service
@Component
@SpringBootApplication(scanBasePackages="edu.rohit.circuitbreakclient.service", "edu.rohit.circuitbreakclient.controller")
//CircuitBreakerClientApplication
package edu.rohit.circuitbreakclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
@EnableCircuitBreaker
@SpringBootApplication(scanBasePackages= "edu.rohit.circuitbreakclient.service", "edu.rohit.circuitbreakclient.controller")
public class CircuitBreakerClientApplication
public static void main(String[] args)
SpringApplication.run(CircuitBreakerClientApplication.class, args);
//CircuitBreakerController
package edu.rohit.circuitbreakclient.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import edu.rohit.circuitbreakclient.service.CircuitBreakerService.BookService;
@RestController
@CrossOrigin
@RequestMapping("/read")
public class CircuitBreakerController
BookService bookService;
@Autowired
CircuitBreakerController(BookService bs)
this.bookService=bs;
@RequestMapping("/get")
public String toRead()
return bookService.readingList();
//BookingService
package edu.rohit.circuitbreakclient.service;
import java.net.URI;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
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";
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>edu.rohit</groupId>
<version>1</version>
<description>Demo project for Spring Boot CircuitBreakerClient</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2.1.3.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>$spring-cloud.version</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>$spring-cloud.version</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>$spring-cloud.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<artifactId>CircuitBreakerClient</artifactId>
<name>CircuitBreakerClient</name>
</project>
application.properties
server.port=8061
hystrix.command.default.circuitBreaker.requestVolumeThreshold=20
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=500
【问题讨论】:
【参考方案1】:BookService
是一个嵌套类,它的实例与一个 CircuitBreakerService
关联,但没有配置 CircuitBreakerService
的实例,因此 Spring 无法弄清楚如何初始化内部类。
所以要么
(1) 将BookService
重构为独立类或
(2) 将BookService
标记为public static
或
CircuitBreakerService
@Component
public static class BookService
(3) 用@Component
标记外部类CircuitBreakerService
@Component
CircuitBreakerService
@Component
public class BookService
【讨论】:
感谢 Manh,它有效。我不确定内部类是否需要额外的 @Component 注释。我认为 @SpringBootApplication(scanBasePackages "") 会处理这些事情。再次感谢。 亲爱的 Manh 如果你能找到一些线索,请检查下面的问题:***.com/questions/58619789/…以上是关于Controller 中构造函数的参数 0 需要一个找不到的 Service 类类型的 bean的主要内容,如果未能解决你的问题,请参考以下文章
“”中构造函数的参数 0 需要一个“”类型的 bean,但无法找到
com.din.OSS 中构造函数的参数 0 需要找不到类型为“java.lang.String”的 bean
如何解决此问题:LoginController 中构造函数的参数 0 需要找不到类型为“OktaOAuth2Properties”的 bean