nosuchbeandefinitionexception:没有类型的限定bean

jtjikinw  于 2021-07-26  发布在  Java
关注(0)|答案(4)|浏览(335)

我在调用我的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());
    }
}

计算方式:

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

谢谢你的帮助。。

kiz8lqtg

kiz8lqtg1#

检查modecalculdao是否在主程序的子包中,如果它不是子包,请尝试在主程序中添加@componentscan(包名)

qvsjd97n

qvsjd97n2#

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

8yoxcaq7

8yoxcaq73#

确保您的 MargauxApplication 班级在里面 com.margaux.margaux 包裹。
你能分享一下吗 MargauxApplication 类来检查其导入包。

hwazgwia

hwazgwia4#

这是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>

为什么?

相关问题