简体   繁体   English

没有可用的“ru.spb.repository.UserRepository”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选

[英]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.

相关问题 没有为依赖项找到UserRepository类型的限定bean:预期至少有1个bean符合此依赖项的autowire候选者 - No qualifying bean of type UserRepository found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency 没有可用类型的合格Bean:预计至少有1个合格为自动装配候选的Bean - No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate NoSuchBeanDefinitionException:没有符合类型的合格bean <package> &#39;可用:至少有1个符合自动装配候选条件的bean - NoSuchBeanDefinitionException: No qualifying bean of type '<package>' available: expected at least 1 bean which qualifies as autowire candidate 没有可用的&#39;xxx.dao.AreaDao&#39;类型的合格Bean:预计至少有1个符合自动装配候选条件的Bean - No qualifying bean of type 'xxx.dao.AreaDao' available: expected at least 1 bean which qualifies as autowire candidate 没有可用类型的合格 bean - 预计至少有 1 个 bean 有资格作为自动装配候选 - No qualifying bean of type available - expected at least 1 bean which qualifies as autowire candidate NoSuchBeanDefinitionException:没有可用类型的限定bean:预期至少有1个bean有资格作为autowire候选者 - NoSuchBeanDefinitionException: No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate 没有可用的“Package.TestDaoRepo”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选 - No qualifying bean of type 'Package.TestDaoRepo' available: expected at least 1 bean which qualifies as autowire candidate 没有可用的“javax.sql.DataSource”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean - No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate 没有可用类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选者 - No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate 没有可用的“java.lang.String”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。 依赖注解: - No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM