2

I am in the process of learning Java spring Boot from the tutorial found here

I keep getting the "Field topicRepository in api.dataBase.basic.apiDatabase.Models.TopicService required a bean of type 'api.dataBase.basic.apiDatabase.Interface.TopicRepository' that could not be found."

I read that this error can occur because of componentScan is failing for poor annotation of the class or poor package layout.

Layout of my java packages

lay out of my project

My classes are the following;

    package api.dataBase.basic.apiDatabase;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication ()
public class ApiDatabaseApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiDatabaseApplication.class, args);
    }

}

Topic.java

package api.dataBase.basic.apiDatabase.Models;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Topic {
    @Id
    private String id;
    private String name;
    private String description;
    //private Address address;

    public Topic(final String id, final String name, final String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public Topic() {
    }

@Id
public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(final String description) {
        this.description = description;
    }
}

TopicRepository.java

package api.dataBase.basic.apiDatabase.Interface;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


import api.dataBase.basic.apiDatabase.Models.Topic;

@Repository
public interface TopicRepository extends CrudRepository  <Topic, String>{


}

TopicService.java

package api.dataBase.basic.apiDatabase.Models;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import api.dataBase.basic.apiDatabase.Interface.TopicRepository;


@Service
public class TopicService {

    @Autowired
    private TopicRepository topicRepository;

    private List<Topic> Oldtopics = new ArrayList<>(Arrays.asList(
            new Topic("1001", "test", "hello 1"),
            new Topic("1002", "hello", "hello 2")
    ));

    public List<Topic> getTopics() {

        List<Topic> topics = new ArrayList<>();
        topicRepository.findAll().forEach(topics::add);
        return topics;
    }

    public Topic getTopic(String id) {
        return Oldtopics.stream().filter(t -> t.getId().equals(id)).findFirst().get();
    }


    public void addTopic(final Topic topic) {
        topicRepository.save(topic);
    }

    public void updateTopic(final Topic topic, final String id) {
        for (int i = 0; i < Oldtopics.size(); i++) {
            if (Oldtopics.get(i).getId().equals(id)) {
                Oldtopics.set(i, topic);
                return;
            }
        }
    }

    public void deleteTopic(String id) {
        Oldtopics.removeIf(t -> t.getId().equals(id));
    }

    public void deleteTopic(final String[] id) {
        for (int i = 0; i < Oldtopics.size(); i++) {
            for (String ids : id) {
                if (Oldtopics.get(i).getId().equals(ids)) {
                    Oldtopics.remove(i);
                }
            }
        }
    }
}

Any help will be appreciated.

Full Stack Trace.

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-21 19:18:05.350 ERROR 4192 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field topicRepository in api.dataBase.basic.apiDatabase.Models.TopicService required a bean of type 'api.dataBase.basic.apiDatabase.Interface.TopicRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'api.dataBase.basic.apiDatabase.Interface.TopicRepository' in your configuration.

POM:

    <?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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>api.dataBase.basic</groupId>
    <artifactId>apiDatabase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>apiDatabase</name>
    <description>API project with JPA database</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
fullmetal2
  • 51
  • 1
  • 6

1 Answers1

0

This can be the case for missed configuration and thus messed component scan.

I'd suggest that you should add 'Config.java' with following content to your source root (src/main/java)

@Configuration
@EnableJpaRepositories
class Config {
}
im_infamous
  • 972
  • 1
  • 17
  • 29