简体   繁体   中英

No qualifying bean of type 'ru.spb.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate

Repository class:

 package ru.spb.repository.user;

@Repository
public class AnketUserRepository implements UserRepository {

     @Autowired
     private CrudUserRepository crudRepository;

 }

Interface:

package ru.spb.repository;

public interface UserRepository {
}

Service:

package ru.spb.service;

@Service
public class UserService {

   private final UserRepository repository;

   @Autowired
   public UserService (UserRepository repository){
       this.repository = repository;
   }
}

Configs: in spring-app.xml:

 <context:component-scan base-package="ru.spb.service"/> 

in spring-db.xml:

 <context:component-scan base-package="ru.spb.repository.user"/>

But I catch the following exception:

 NoSuchBeanDefinitionException: No qualifying bean of type 'ru.spb.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

I tried to add @Repository on UserRepository-interface, but without success.

You need to Annotate the Interface also

package ru.spb.repository;

@Repository
public interface UserRepository {
}

Your component scan is configured only to the package of the implementing class, but you are autowiring the interface which is on a different (non component scanned) package.
Move the @Repository annotation to the inteface, and add ru.spb.repository to your component-scan

I moved

 <context:component-scan base-package="ru.spb.repository.user"/>  

from spring-db.xml to spring-app.xml. And this problem is solved. However I dont understand the reason of this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM