Параметр 0 конструктора в Controller требует bean-компонента типа Service, который не может быть найден - PullRequest
0 голосов
/ 28 октября 2019

Приложение не удалось запустить


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 Ответ

1 голос
/ 28 октября 2019

BookService является вложенным классом, его экземпляр связан с одним CircuitBreakerService, но ни один экземпляр CircuitBreakerService не настроен, поэтому Spring не может понять, как инициализировать внутренний класс.

Итаклибо

  • (1) рефакторинг BookService в отдельный класс, либо
  • (2) знак BookService с public static или
CircuitBreakerService {
    @Component
    public static class BookService {}
}
  • (3) отметить внешний класс CircuitBreakerService как @Component
@Component
CircuitBreakerService {
    @Component
    public class BookService {}
}
...