繁体   English   中英

没有可用类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选者

[英]No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate

无法启动服务器,更新 Spring boot3 后。

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sample.fa.repository.NewsRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Description:
Field newsRepository in com.sample.fa.service.NewsService required a bean of type 'com.sample.fa.repository.NewsRepository' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)

// 代码

@Repository
public interface NewsRepository extends JpaRepository<News, Long> {

    Optional<News> findById(Long id);

}

我有同样的问题:

com.lst.lexiconlookup.service.LexiconService 中的字段存储库需要找不到类型为“com.lst.lexiconlookup.repository.LexiconRepository”的 bean。

注入点具有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true)

行动:

考虑在您的配置中定义类型为“com.lst.lexiconlookup.repository.LexiconRepository”的 bean。

我使用的是 SpringInitialzer 的 3.0.1 版。 当我从服务 class 中取出@Autowired 时,我可以运行但得到一个 Null 指针:

出现意外错误(类型=内部服务器错误,状态=500)。 无法调用“com.lst.lexiconlookup.repository.LexiconRepository.findAll()”,因为“this.repository”是 null java.lang.NullPointerException:无法调用“com.lst.lexiconlookup.repository.LexiconRepository.findAll()”,因为“ this.repository" 是 null

@Repository
public interface LexiconRepository extends JpaRepository<Lexicon, Long>
{

}


@Service
public class LexiconService 
{
    @Autowired
    private LexiconRepository repository;


    public List<Lexicon> getAllLexicon()
    {
        return repository.findAll();
    }

}


@RestController
public class LexiconController 
{
    @Autowired
    private LexiconService lexiconService;

    @GetMapping("/hello")
    @ResponseBody
    public String hello()
    {
        return "HELLO";

    }

    @GetMapping("/lexiconlookup")
    @ResponseBody
    public List<Lexicon> retrieveAllLexicon()
    {
        return lexiconService.getAllLexicon();
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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