簡體   English   中英

應用程序無法啟動Spring Boot

[英]Application Failed to Start Spring Boot

我有一個使用Spring Boot的框架,它包含一個控制器RestController類,

@RequestMapping("/details")
@RestController
public class DataController {
    private KafkaStreams kafkaStreams;

    public DataController(KafkaStreams kafkaStreams) {
        this.kafkaStreams = kafkaStreams;
    }

    @Autowired
    DataService dataService;

    @RequestMapping(value = "getAllDetails", method = RequestMethod.GET)
    public boolean getAllDetails(KafkaStreams kafkaStreams) {
        return ktableService.getAllDetails(kafkaStreams);
    }
}

在我的服務實現類中,我使用此kafkaStreams對象來查找不同服務的詳細信息。

現在我使用這個框架作為我的另一個應用程序中的依賴項,我有一個運行器類,

import org.apache.kafka.streams.KafkaStreams;
@Component
public class PipelineRunner {
    private final StreamsBuilder streamsBuilder;
    private final KafkaProperties kafkaProperties;
    private final SerdesExt serdesExt;

    @Autowired
    public PipelineRunner(StreamsBuilder streamsBuilder, KafkaProperties kafkaProperties, SerdesExt serdesExt) {
        this.streamsBuilder = streamsBuilder;
        this.kafkaProperties = kafkaProperties;
        this.serdesExt = serdesExt;
    }

    @PostConstruct
    public void run() {
        ReflectData.AllowNull.get().addStringable(Utf8.class);
        ReflectData.get().addStringable(Utf8.class);
        DataProcessor processor = new DataProcessor(streamsBuilder, kafkaProperties,
                serdesExt);
        start();
    }

    private void start() {
        KafkaStreams kafkaStreams = new KafkaStreams(streamsBuilder.build(),
                kafkaProperties.getKafkaStreamsProperties(serdesExt));
        System.out.println("----Its is started----");
        DataController controller = new DataController(kafkaStreams);
        kafkaStreams.start();

    }
}

在這個類中,我正在嘗試為DataController創建對象。

所以當我試圖運行應用程序類時,

@SpringBootApplication(scanBasePackages = { "framework package" })
@EnableConfigurationProperties(KafkaProperties.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我收到這個錯誤,

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in frameworkpackage.controllers.DataController required a bean of type 'org.apache.kafka.streams.KafkaStreams' that could not be found.


Action:

Consider defining a bean of type 'org.apache.kafka.streams.KafkaStreams' in your configuration.

我是Spring Boot的新手。 所以我可能在這里做錯了。 如果需要更多信息我可以提供。

UPDATE

我的pom文件,

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

    <properties>
        <confluent.version>4.1.0</confluent.version>
        <kafka.version>1.1.0</kafka.version>
        <lombok.version>1.18.0</lombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.confluent</groupId>
            <artifactId>kafka-avro-serializer</artifactId>
            <version>${confluent.version}</version>
        </dependency>
        <dependency>
            <groupId>io.confluent</groupId>
            <artifactId>kafka-streams-avro-serde</artifactId>
            <version>${confluent.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-streams</artifactId>
            <version>${kafka.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>${kafka.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-avro</artifactId>
            <version>2.8.5</version>
        </dependency>

    </dependencies>

問題是您正在嘗試在類DataController中使用bean kafkaStreams ,但在Spring上下文中沒有具有此名稱的bean。 您需要手動創建它,以便稍后自動裝配。

在你的情況下,我建議像這樣更新PipelineRunner.java:

import javax.annotation.PostConstruct;
import org.apache.kafka.streams.KafkaStreams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class PipelineRunner
{
    private final StreamsBuilder streamsBuilder;
    private final KafkaProperties kafkaProperties;
    private final SerdesExt serdesExt;


    @Autowired
    public PipelineRunner(StreamsBuilder streamsBuilder, KafkaProperties kafkaProperties, SerdesExt serdesExt)
    {
        this.streamsBuilder = streamsBuilder;
        this.kafkaProperties = kafkaProperties;
        this.serdesExt = serdesExt;
    }


    @PostConstruct
    public void run()
    {
        ReflectData.AllowNull.get().addStringable(Utf8.class);
        ReflectData.get().addStringable(Utf8.class);
        DataProcessor processor = new DataProcessor(streamsBuilder, kafkaProperties,
            serdesExt);
        start();
    }


    @Bean
    KafkaStreams kafkaStreams()
    {
        KafkaStreams kafkaStreams = new KafkaStreams(
            streamsBuilder.build(),
            kafkaProperties.getKafkaStreamsProperties(serdesExt));
        System.out.println("----Its is started----");
        kafkaStreams.start();
        return kafkaStreams;
    }
}

你不需要自己創建一個DataController實例,這將由Spring自動完成。

關於Spring辦法豆更多信息,請

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM