![](/img/trans.png)
[英]No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate
[英]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.