[英]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:配置:在 spring-app.xml 中:
<context:component-scan base-package="ru.spb.service"/>
in spring-db.xml:在 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.我试图在 UserRepository-interface 上添加 @Repository,但没有成功。
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移动@Repository
注解的inteface,并添加ru.spb.repository
到您的组件扫描
I moved我搬家了
<context:component-scan base-package="ru.spb.repository.user"/>
from spring-db.xml to spring-app.xml.从 spring-db.xml 到 spring-app.xml。 And this problem is solved.而这个问题就解决了。 However I dont understand the reason of this problem.但是我不明白这个问题的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.