Java – The application failed to start Spring Boot

The application failed to start Spring Boot… here is a solution to the problem.

The application failed to start Spring Boot

I have a framework that uses Spring Boot and it contains a Controller RestController class

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

In my service implementation class, I use this kafkaStreams object to find details of my different services.

Now that I’m using this framework as a dependency in my other application, I have a runner class

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

}
}

In this class, I’m trying to create an object for a DataController.

So when I try to run the application class,

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

I’m getting this error,

***************************
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.

I’m new to Spring Boot. So I may be doing something wrong here. If more information is needed, I can provide it.

UPDATE

My pom file,

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

Solution

The problem is that you are trying to use bean kafkaStreams in a DataController class, but there is no bean with this name in the Spring context. You need to create it manually so that you can autowire it later.

In your case, I recommend updating 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;
    }
}

You don’t need to create an instance of the DataController yourself, this will be done automatically by Spring.

More information about the Spring bean method is available there

Related Problems and Solutions