2

Unable to start server, After updating 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)

// code

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

    Optional<News> findById(Long id);

}
selvi
  • 1,271
  • 2
  • 21
  • 41
  • 1
    It seems that your `NewsRepository ` is not created from Spring Context as a Bean. Do you have `@EnableJpaRepositories` on your @SpringBootApplication class? – Емилиян Йорданов Dec 19 '22 at 15:03
  • @ЕмилиянЙорданов Even I tried using EnableJpaRepositories but still I am getting same error – selvi Dec 20 '22 at 04:39
  • Please check does `@SprinbBootApplication` still have all the related annotations - @ComponentScan and @EnableAutoConfiguration. I think that answer will help you https://stackoverflow.com/questions/33619532/configuration-using-annotation-springbootapplication . My suggestion here is to put `@EnableJpaRepository(basePackages = "com.sample.fa.repository")` or `@ComponentScan("com.sample")` . Also you can try constructor injection. I'm just guessing what doesn't trigger bean creation, because I still use Spring 2.0 – Емилиян Йорданов Dec 20 '22 at 07:33
  • @ЕмилиянЙорданов Added EntityScan(basePackages = { "com.sample.fa.*" }) ComponentScan(basePackages = { "com.sample.fa.*" }) SpringBootApplication EnableCaching EnableJpaRepositories(basePackages = { "com.sample.fa.repository.*" }) – selvi Dec 20 '22 at 09:02
  • Does it work? Or still same problem – Емилиян Йорданов Dec 20 '22 at 13:53
  • @ЕмилиянЙорданов Yes facing still the same issue – selvi Dec 23 '22 at 04:38

2 Answers2

0

I have the same issue:

Field repository in com.lst.lexiconlookup.service.LexiconService required a bean of type 'com.lst.lexiconlookup.repository.LexiconRepository' that could not be found.

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

Action:

Consider defining a bean of type 'com.lst.lexiconlookup.repository.LexiconRepository' in your configuration.

I am using version 3.0.1 from SpringInitialzer. When I take out @Autowired from service class, I can run but get a Null Pointer:

There was an unexpected error (type=Internal Server Error, status=500). Cannot invoke "com.lst.lexiconlookup.repository.LexiconRepository.findAll()" because "this.repository" is null java.lang.NullPointerException: Cannot invoke "com.lst.lexiconlookup.repository.LexiconRepository.findAll()" because "this.repository" is 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();
    }

}
0

If you have the following maven depancy, please remove it and run again. The solution is work for me. I also encountered the same problem like you.

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <scope>test</scope>
</dependency>
JumpThere
  • 1
  • 1