Spring @ComponentScan doesn't work on @Repository

3825 views java
3

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

answered question

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.

1 Answer

3

you should use your code like below

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

posted this

Have an answer?

JD

Please login first before posting an answer.