【问题标题】:NoSuchBeanDefinitionException : no qualifying bean of typeNoSuchBeanDefinitionException : 没有符合条件的 bean 类型
【发布时间】:2021-01-14 23:27:46
【问题描述】:

我在调用我的 GET 请求(modes-calcul)时收到此错误,但我不明白为什么...我的依赖注入是正确的?

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: {}

模式计算控制器:

@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> {
}

感谢您的帮助..

【问题讨论】:

  • 我在您的代码中看不到任何错误,如果您解决了问题,请告诉我。

标签: spring spring-boot exception repository javabeans


【解决方案1】:

检查ModeCalculDAO是否在主程序的子包中 或者如果它不是子包尝试在主程序中添加@ComponentScan(包名)

【讨论】:

【解决方案2】:

当您运行应用程序时,它会扫描项目主包的子包中的所有文件,您的主类在其中使用@SpringBootApplication 定义。因此,将所有要扫描的文件放在主类的子包中,或者如果您的包不是主包的子包,请在主类中使用 @ComponentScan(your_package name)。

【讨论】:

    【解决方案3】:

    确保您的 MargauxApplication 类位于 com.margaux.margaux 包中。

    您能否分享MargauxApplication 类以检查其导入包。

    【讨论】:

      【解决方案4】:

      这是 MargauxApplication 类:

      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") 在这堂课中,但它不起作用。 我的其他课程有这个:

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

      在我的 pom 中我有这个:

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

      如果我放这个,它会起作用:

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

      为什么?

      【讨论】:

        猜你喜欢
        • 2018-12-20
        • 1970-01-01
        • 1970-01-01
        • 2018-04-05
        • 2017-11-27
        • 2020-06-18
        • 2020-06-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多