spring boot fails to start-- define a bean of type 'TopicRepository' in configuration

coda :

I was following this JavaBrains tutorials of Spring Boot.

My project structure is as follows:

enter image description here

CourseApiApp.java:

@SpringBootApplication
@ComponentScan(basePackages = {
    "com.bloodynacho.rishab.topics"
})
@EntityScan("com.bloodynacho.rishab.topics")
public class CourseApiApp {

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

TopicController.java:

@RestController
public class TopicController {

    @Autowired
    private TopicService topicService;

    @RequestMapping(
        value = "/topics"
    )
    public List<Topic> getAllTopcs() {
        return topicService.getAllTopics();
    }
}

TopicService.java:

@Service
public class TopicService {

    @Autowired
    private TopicRepository topicRepository;

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

Topic.java:

@Entity
public class Topic {
    @Id
    private String id;
    private String name;
    private String description;
}

TopicRepository.java:

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

pom.xml:

<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>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

I was using the lombok @Getter, @Getter and @AllArgsConstructor in Topic.java but I removed it after reading one of the answers here.

I read this1, this2, this3

Still, I get

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

Description:
Field topicRepository in com.bloodynacho.rishab.topics.TopicService required a bean of type 'com.bloodynacho.rishab.topics.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 'com.bloodynacho.rishab.topics.TopicRepository' in your configuration.
Process finished with exit code 1

EDIT: I read this explaining how even without actually implementing the interface the @Autowired works. I understand the solution, but I don't understand how to solve my issue. Clearly, there is some problem with the way Spring Data is set up and configured (as mentioned in the answer)

LHCHIN :

Because if your other packages hierarchies are below your main application with the @SpringBootApplication annotation, you’re covered by implicit components scan.

Therefore, one simple solution can be done by following 2 steps:

  1. Rename the package of main class to be com.bloodynacho.rishab.
    (That is what I suggest that the complete package name of main app. is supposed to be root of other packages.)
  2. Remove @ComponentScan and @EntityScan annotation.
    (Although @ComponentScan is different from @EntityScan, it can be also removed in my experience.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Define a bean name in configuration spring boot

Spring boot CrudRepo Define a bean

Spring Boot APPLICATION FAILED TO START due to required a bean of type AuthenticationManager

Consider defining a bean of type 'package' in your configuration [Spring-Boot]

Consider defining a bean of type 'service' in your configuration [Spring boot]

what will happen if we didn't define @Bean under @Configuration class in spring boot

Spring Boot @Mapper Bean creation issue : Application Failed to start. Error : Consider defining a bean of Type

Spring Boot application fails to start due to a circular dependency between 1 bean

Spring boot Test fails saying, Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

Spring Boot - Injecting Repository into Controller throws Consider defining a bean of type 'Type' in your configuration

Bean validation fails for Spring Boot application

Spring Boot defining bean in configuration problem

Spring boot use bean inside configuration

Autowiring YML/Java Configuration into a Spring Boot Bean

Initializa Bean with YAML configuration in Spring Boot

Spring Boot start fails for Autoconfigure on RabbitMQ

Spring Boot App Fails to Start on Elastic Beanstalk

spring boot project define dependency class as spring bean

Spring Boot Inject Bean Map with Generic Type

Spring boot test "No qualifying bean of type available"

"No qualifying bean of type" for JPA repository in Spring Boot

Spring Boot JPA - No qualifying bean of type

spring boot could not find bean of type

Where do you define spring bean configuration files

Java Spring configuration when to define bean and when new

How to define a bean of String array in Spring (using XML configuration)

Define Bean in combination with @ConditionalOnMissingBean does not work since Spring Boot Upgrade

APPLICATION FAILED TO START - Consider defining a bean of type in your configuration [SpringBoot]

Example keycloak spring-boot app fails to find bean KeycloakSpringBootConfigResolver

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive