0

I have a Repository class on the package:

com.repository

And I have defined the @ComponentScan("com.*") in the main class of Spring Boot.

However while running the application I am getting the Exception

Consider defining a bean of type 'com.repository.TopicRepository' in your configuration.

Below are the classes:

TopicRepository

package com.repository;

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

import com.models.Topic;


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



}

Application

package com.example.springboot.springboot.test;

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

@SpringBootApplication(scanBasePackages = "com.*")
public class Application {

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

}

POM.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.3.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.springboot</groupId>
<artifactId>springboot.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot.test</name>
<description>Demo project for Spring Boot</description>


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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.197</version>
        <scope>runtime</scope>
    </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>

<properties>
    <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>

I have already tried the @ComponentScan. If I change the scanBasePackages to "com.repository" then it is able to somehow pickup TopicRepository alongwith other Service and Controller, which should not be the case since they are under different package.

EDIT:

package structure

com.models : Models

com.repository : Repository

com.services : Sevices

com.controllers : Controllers

com.springboot.springboottest : Main Class

ghostrider
  • 2,046
  • 3
  • 23
  • 46

1 Answers1

-1

From the documentation

We generally recommend that you locate your main application class in a root package above other classes. The @SpringBootApplication annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items.

Also as chrylis -on strike points out in the comment , the base package should be just defined as com . The scan would start from package com and on all its subpackages

@ComponentScan("com")

Update

Based on the package structure shared.

@SpringBootApplication(scanBasePackages = {"com.models","com.repository","com.services","com.controllers"})
public class Application {

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

}

or

@SpringBootApplication(scanBasePackages = "com")
public class Application {

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

}

should work.

The best option is to move the class Application to the package com have the following annotation.

@SpringBootApplication
public class Application {

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

}
R.G
  • 6,436
  • 3
  • 19
  • 28
  • No Still the same problem – ghostrider Jan 21 '20 at 03:50
  • You did mention that when 'scanBasePackages' was modified it worked. What did you modify and what is the latest error ? – R.G Jan 21 '20 at 04:26
  • @RG, I modified to include the package like scanBasePackages("com.repository"). It started the application but the controllers and services were not getting picked up since they were in different package.Currently I am getting the same error as defined in the question – ghostrider Jan 21 '20 at 10:35
  • @ghostrider you can add multiple packages to componentscan: eg : @ComponentScan({“package1”,”package2”}). At the same time adding “com” should ideally scan all sub packages and detect the components – R.G Jan 21 '20 at 10:39
  • Same goes for @SpringBootApplication – R.G Jan 21 '20 at 10:45
  • Yes ideally it should but not happening in my case – ghostrider Jan 21 '20 at 10:49
  • @ghostrider controllers and services are under sub packages of com ? Could you please update the question with the structure of your project indicating the main class and the components that are not auto detected? – R.G Jan 21 '20 at 11:04