Java SpringBoot没有';t扫描MicroService应用程序中的组件

Java SpringBoot没有';t扫描MicroService应用程序中的组件,java,spring,maven,spring-boot,spring-cloud,Java,Spring,Maven,Spring Boot,Spring Cloud,我正在编写基于SpringCloud的微服务应用程序。我启动了Eureka服务器,现在我正在编写一个汽车服务代码。当我在项目中并没有任何自动布线时,它就工作了。添加存储库、服务和更改控制器后,汽车服务不会启动 当我添加@SpringBootApplication(“com.carrental.carservice.repository”)时,应用程序启动了,但RESTAPI不工作,返回404。我尝试使用@Qualifier和命名存储库,但仍然不起作用。 启动时出现错误: ************

我正在编写基于SpringCloud的微服务应用程序。我启动了Eureka服务器,现在我正在编写一个汽车服务代码。当我在项目中并没有任何自动布线时,它就工作了。添加存储库、服务和更改控制器后,汽车服务不会启动

当我添加@SpringBootApplication(“com.carrental.carservice.repository”)时,应用程序启动了,但RESTAPI不工作,返回404。我尝试使用@Qualifier和命名存储库,但仍然不起作用。 启动时出现错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.carrental.carservice.service.impl.CarTypeServiceImpl required a bean of type 'com.carrental.carservice.repository.CarTypeRepository' that could not be found.


Action:

Consider defining a bean of type 'com.carrental.carservice.repository.CarTypeRepository' in your configuration.
日志中有一个警告:

 Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.carrental.carservice.repository.CarTypeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
汽车服务的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>carrental</artifactId>
        <groupId>com.carrental</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>car-service</artifactId>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.3.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.9.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.199</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>





    </dependencies>


</project>
控制器

package com.carrental.carservice.controller;


import com.carrental.carservice.dto.CarTypeDto;
import com.carrental.carservice.model.entity.CarType;
import com.carrental.carservice.service.CarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/car")
public class CarTypeController {

    private final CarTypeService carTypeService;

    @Autowired
    public CarTypeController(CarTypeService carTypeService){
        this.carTypeService = carTypeService;
    }

    @PostMapping("/cartype/add")
    public ResponseEntity<CarTypeDto> addCarType(@Valid @RequestBody CarTypeDto dto){
        CarType entity = Mapper.mapToCarTypeEntity(dto);
        this.carTypeService.add(entity);
        return new ResponseEntity<CarTypeDto>(Mapper.mapToCarTypeDto(entity), HttpStatus.CREATED);
    }

    @GetMapping
    public String get(){
        return "jajo";
    }
}

package com.carrental.carservice.controller;
导入com.carrental.carservice.dto.CarTypeDto;
导入com.carrental.carservice.model.entity.CarType;
导入com.carrental.carservice.service.CarTypeService;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.bind.annotation.*;
导入javax.validation.Valid;
@RestController
@请求映射(“/car”)
公共类CarTypeController{
私有最终CarTypeService CarTypeService;
@自动连线
公共CarTypeController(CarTypeService CarTypeService){
this.carTypeService=carTypeService;
}
@后期映射(“/cartype/add”)
public ResponseEntity addCarType(@Valid@RequestBody CarTypeDto dto){
cartypeentity=Mapper.mapToCarTypeEntity(dto);
this.carTypeService.add(实体);
返回新的响应属性(Mapper.mapToCarTypeDto(实体),HttpStatus.CREATED);
}
@GetMapping
公共字符串get(){
返回“jajo”;
}
}
服务

package com.carrental.carservice.service.impl;

import com.carrental.carservice.model.entity.CarType;
import com.carrental.carservice.repository.CarTypeRepository;
import com.carrental.carservice.service.CarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarTypeServiceImpl implements CarTypeService {

    private final CarTypeRepository carTypeRepository;

    @Autowired
    public CarTypeServiceImpl(CarTypeRepository carTypeRepository){
        this.carTypeRepository = carTypeRepository;
    }

    @Override
    public void add(CarType carType) {
        if(this.carTypeRepository.existsByName(carType.getName()))
            return;
        this.carTypeRepository.save(carType);
    }

    @Override
    public List<CarType> getAll() {
        return this.carTypeRepository.findAll();
    }

    @Override
    public void update(CarType carType) {
        if(this.carTypeRepository.existsById(carType.getId()))
            this.carTypeRepository.save(carType);
    }

    @Override
    public void delete(CarType carType) {
        this.carTypeRepository.delete(carType);
    }
}

package com.carrental.carservice.service.impl;
导入com.carrental.carservice.model.entity.CarType;
导入com.carrental.carservice.repository.CarTypeRepository;
导入com.carrental.carservice.service.CarTypeService;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入java.util.List;
@服务
公共类CarTypeServiceImpl实现CarTypeService{
私有最终CarTypeRepository CarTypeRepository;
@自动连线
公共CarTypeServiceImpl(CarTypeRepository CarTypeRepository){
this.carTypeRepository=carTypeRepository;
}
@凌驾
公共作废添加(CarType CarType){
if(this.carTypeRepository.existsByName(carType.getName()))
返回;
this.carTypeRepository.save(carType);
}
@凌驾
公共列表getAll(){
返回此.carTypeRepository.findAll();
}
@凌驾
公共作废更新(CarType CarType){
if(this.carTypeRepository.existsById(carType.getId()))
this.carTypeRepository.save(carType);
}
@凌驾
公共作废删除(CarType CarType){
this.carTypeRepository.delete(carType);
}
}
存储库

package com.carrental.carservice.repository;

import com.carrental.carservice.model.entity.CarType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CarTypeRepository extends JpaRepository<CarType, Long> {
    boolean existsByName(String name);
    CarType getByName(String name);
    boolean existsById(Long id);
}

package com.carrental.carservice.repository;
导入com.carrental.carservice.model.entity.CarType;
导入org.springframework.data.jpa.repository.JpaRepository;
导入org.springframework.stereotype.Repository;
@存储库
公共接口CarTypeRepository扩展了JpaRepository{
布尔existsByName(字符串名称);
CarType getByName(字符串名称);
布尔existsById(长id);
}

我尝试了很多东西,但仍然不起作用。您能帮忙吗?

我想您需要使用FaignClient来避免这个异常。我有类似的问题,并添加了 @EnableFaignClient到my ApplicationClass及其对pom的依赖性

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId
</dependency>

org.springframework.cloud

SpringCloudStarter openfeign我认为您需要使用feignClient来避免这个异常。我有类似的问题,并添加了 @EnableFaignClient到my ApplicationClass及其对pom的依赖性

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId
</dependency>

org.springframework.cloud

SpringCloudStarter openfeignI不知道为什么,但您有不兼容版本的库。@Spencergib哪些库?我试着用最新的versions@KamiloxSpring库。不要手动添加每个Spring库的版本,而是在maven构建文件中使用类似于
org.springframework.boot-Spring-boot-starter-parent 2.1.4.RELEASE
,并从Spring依赖项中删除
标记。让Spring的依赖关系管理为您解决单个库(除非您有充分的理由不这样做)。您混合了不同版本的
Spring boot starter
项目。例如,您使用的是Spring Booer-Stter -DATA JavaSuxBaseEnter和Base2.01.9.代码版本> 2.1.4.发布< /COD>。Spring Cuy 1 .x库与Buff.2.Xi不兼容,不知道为什么,但是您有不兼容的库版本。@ SpCurrGiBB哪些库?我试着用最新的versions@KamiloxSpring库。不要手动添加每个Spring库的版本,而是在maven构建文件中使用类似于
org.springframework.boot-Spring-boot-starter-parent 2.1.4.RELEASE
,并从Spring依赖项中删除
标记。让Spring的依赖关系管理为您解决单个库(除非您有充分的理由不这样做)。您混合了不同版本的
Spring boot starter
项目。例如,您正在使用spring boot starter web的
2.0.9.RELEASE
版本和spring-boot-starter-data-jpa的
2.1.4.RELEASE
。此外,spring cloud 1.x库与boot 2.x不兼容