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