NoSuchBeanDefinitionException : no qualifying bean of type

Viewed 3586

I get this error when call my GET request (modes-calcul) and I don't understand why... My dependency injection is correct?

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.margaux.margaux.repository.ModeCalculDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

ModeCalculController :

@Slf4j
@Transactional
@RestController
@RequestMapping("modes-calcul")
public class ModeCalculController {
    private ModeCalculService modeCalculService;

    @Autowired
    public ModeCalculController(ModeCalculService modeCalculService) {
        this.modeCalculService = modeCalculService;
    }

    @GetMapping()
    public ResponseEntity<List<ModeCalculDTO>> getModesCalcul() {
        return new ResponseEntity<List<ModeCalculDTO>>(modeCalculService.getModesCalcul(), HttpStatus.OK);
    }
}

ModeCalculServiceImpl :

@Slf4j
@Service
@Transactional
public class ModeCalculServiceImpl implements ModeCalculService {
    private ModeCalculDAO modeCalculDAO;

    @Autowired
    @Lazy
    public ModeCalculImpl(ModeCalculDAO modeCalculDAO){
        this.modeCalculDAO = modeCalculDAO;
    }

    @Override
    public List<ModeCalculDTO> getModesCalcul() {
        ModelMapper mapper = new ModelMapper();
        List<ModeCalcul> modesCalcul = modeCalculDAO.findAll();

        return modesCalcul
                .stream()
                .map(modeCalcul -> mapper.map(modeCalcul, ModeCalculDTO.class))
                .collect(Collectors.toList());
    }
}

ModeCalculDAO :

@Repository
public interface ModeCalculDAO extends JpaRepository<ModeCalcul, Long> {
}

Thanks for your help..

4 Answers

check whether the ModeCalculDAO is in sub package of main program or if it is not a sub package try to add @ComponentScan(package name) in main program

When you run your application it scans all the files which are inside the sub package of the project main package in which your main class has defined using @SpringBootApplication. So put all the files inside the sub package of your main class that you want to scan or if your package is not a subpackage of your main package use @ComponentScan(your_package name) at your main class.

Make sure your MargauxApplication class to be inside com.margaux.margaux package.

Can you please share MargauxApplication class to check its import packages.

Here is the MargauxApplication class :

package com.margaux.margaux;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class MargauxApplication {

    public static void main(String[] args) {
        SpringApplication.run(MargauxApplication.class, args);
    }

}

I tried to put @ComponentScan("com.margaux.margaux") or @ComponentScan(basePackages = "com.margaux.margaux") in this class but it doesn't work. My other classes have this :

package com.margaux.margaux.repository;
package com.margaux.margaux.service;
etc ... 

in my pom I have this:

<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.3.6.RELEASE</version>
        </dependency>

if I put this, it works :

<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

Why ?

Related