Spring @ComponentScan doesn't work on @Repository

spring component-scan xml
spring boot component scan not working
componentscan spring boot
spring boot component scan external jar
componentscan in spring
spring boot disable component scan
spring boot component scan multiple modules
componentscan(basepackages)

I have a repository in different package than the configuration class , so I annotated it as the following with @Repostiory:

package test;

@Repository
public interface UserTest extends JpaRepository<User, Long> {
}

I have done the component scan on it and it didn't work :

package com.app;
@SpringBootApplication
@ComponentScan({"test","com.app"})
public class Application extends SpringBootServletInitializer {
}

Exception : No qualifying bean of type 'test.UserTest' available: expected at least 1 bean which qualifies as autowire candidate.

why doesn't the component scan work on repository unless I add enableJpaRepositories ? I thought componetScan is enough


Update:

as some of the answers provides solution , I'm asking about explanation not solution . The following will work without even doing component scan on "test" :

SpringBootApplication
@EnableJpaRepositories({"test","com.app"})
public class Application extends SpringBootServletInitializer{
}

Now the question why do I even need to use componentscan on @Repository when it doesn't work ? why in the documentation the @Repository is scanned by componentscan when it doesnt have effect and @EnableJpaRepostiories is enoguh?

from Spring documentation on component scan : Indicates whether automatic detection of classes annotated with @Component @Repository, @Service, or @Controller should be enabled.

the @Repository in my case is not detected

In order to let spring knows what DataSource is related to what Repository you should define it at the @EnableJpaRepositories annotation.

Try enabling jpa repositories like below.

@SpringBootApplication
@ComponentScan({"test","com.app"})
@EnableJpaRepositories("test")
public class Application extends SpringBootServletInitializer {
}

UPDATE : Why @EnableJpaRepositories needed?

@SpringBootApplication automatically provides the features of the following annotations

@Configuration @EnableAutoConfiguration @ComponentScan

But if you try defining your own annotation then spring boot will not take care of internal auto configurations so this is the reason we have to enable repositories.

I have projects in which only @SpringBootApplication is enough if you are not writing your own things.

I hope you got the point.

Golden words :

If you want to get the maximum advantage of spring boot’s auto configuration feature, it is expected to put all your class packages under spring boot main application package (directly in main package or indirectly as sub packages).

Spring Component Scanning, With Spring, we use the @ComponentScan annotation along with they are different, it doesn't matter where to put the main application class. Using @ComponentScan in a Spring Application With Spring, we use the  @ComponentScan annotation along with @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

I found an explanation about what I was doing wrong. The @Repository annotation with componentscan will not cause spring to implement the spring jpa repository. for the interfaces that implement crud repository enablejparepository should be used.

Now the use of @Repository with componentscan is when you have a repository class and you have your own DAO not for spring curd repo otherwise the implementation won't be created :

@Repository
public class UserTest {


    public List<Object> findById(long l) {

             .......
    }
}

Spring Component Scan, The @EntityScan annotation, unlike @ComponentScan, does not create beans. It only identifies which classes should be used by a specific  In a boot application, Spring core component scanning doesn't work by just using @EnableAutoConfiguration.We have to additionally use @ComponentScan:. Example using @ComponentScan with @EnableAutoConfiguration

you should use your code like below as @ComponentScan always work with basepackages so your implementation should be like below.

 package com.app;
    @SpringBootApplication
    @ComponentScan(basePackages={"test","com.app"})
    @EnableJPARepositories 
    public class Application extends SpringBootServletInitializer {
    }

Spring, Spring Boot, and Component Scan, Why is Component Scan important? Which packages does Spring Boot scan automatically? How do you define Component Scan with Spring  The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan is also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

ComponentScan (Spring Framework 5.2.6.RELEASE API), Note that the <context:component-scan> element has an annotation-config attribute; however, this annotation does not. This is because in almost all cases when  The use of @Component annotation is to mark a class as an auto-discoverable bean for Spring. So, you can see that if the @Component annotation is used then there is no need to declare beans in the spring-config.xml file, thus keeping the XML light as well. Why automatic component scan is important?

Spring, Spring Boot and Component Scan – Spring Boot Tutorial, Spring Boot does some magic around Component Scan. Let's understand that in this article. You will learn. What is Component Scan? Why is  Basically, Spring is going to scan the project’s classpath (or paths you specified), find all of the @Component classes and make them available for auto-wiring. So if you forget to annotate a class it will not be auto-wired and when you try and use it you will get a null and a NullPointerException .

Spring Component Scan, Unlike @ComponentScan, the @EntityScan annotation does not create beans. It only identifies which classes should be used by a specific  Spring needs to know the packages to scan for annotated components. We can use @ComponentScan annotation to specify the base packages to scan. Any class which is annotated with @Component will be scanned for registered.

Comments
  • If you extend from Spring repositories to use basic CRUD operations , you need to enable them via @EnableJPARepositories in order the springboot to detect and initialize the implementations of them at runtime.
  • @TürkmenMustafaDemirci aaah you mean if I make my own repository it will work ?
  • I guess it should work without @enableJPARepositories . give it a try and please tell us about the result I also wondered.
  • @MohammadKarmi i suggest you to create a package like com.app.repository and move your repository class there, then add @EnableJpaRepositories("com.app.reporsitory") on the runner.
  • @MohammadRezaAlagheband again I know the solution and it will work, but Im asking for an explanation
  • I know this is the solution, but is that expected ? in the documentation they say @ ComponentScan work on @ Repository
  • Thanks for the answer Alien, yes that's right I know spring boot provide the above 3 annotations. but the main question here , if enableJpaReposiotry is enough why do I need componentScan on @Repository if it doesnt work at all ?
  • nothing is needed if you follow the golden words in my answer only @SpringBootApplication is enough
  • you are totally right. but I was curious about this :) . see my updated question
  • I did and here what they say : Indicates whether automatic detection of classes annotated with @ Component @ Repository, @ Service, or @ Controller should be enabled.
  • this doesn't answer the question. the repository is outside com.app . I'm asking why enablejparepositories is needed
  • can you please try this approach
  • if you are doing any crud operation then you have to use jpa dependency and for that you have to enable @EnableJPARepositories
  • @ EnableJPARepositories scan the package you have in your configuration class. I know the solution which is same as @Alien mentioned , I already said the solution in question. I'm asking about the reason not the solution
  • @MohammadKarmi please refer 3rd comment, it's a springboot functionality you have to EnableJPARepositories for any JpaRepository operation.