没有符合条件的bean类型

没有符合条件的bean类型

本文介绍了NoSuchBeanDefinitionException:没有符合条件的bean类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用GET请求(modes-calcul)时出现此错误,我不明白为什么...我的依赖项注入正确吗?

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:

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:

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:

ModeCalculDAO :

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

感谢您的帮助.

推荐答案

这是MargauxApplication类:

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);
    }

}

我试图将 @ComponentScan("com.margaux.margaux") @ComponentScan(basePackages ="com.margaux.margaux")在这个班上,但没有用.我的其他班级有这个:

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 ...

在我的pom中我有这个:

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>

为什么?

这篇关于NoSuchBeanDefinitionException:没有符合条件的bean类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:09