SpringBoot不会在微服务应用程序中扫描组件

时间:2019-04-27 15:01:32

标签: java spring maven spring-boot spring-cloud

我正在基于Spring Cloud编写微服务应用程序。我启动了Eureka Server,现在正在编写汽车服务代码。当我在项目中没有任何自动装配时,它就起作用了。添加存储库,服务并更改Controller后,汽车服务不会启动。

当我添加@SpringBootApplication(“ com.carrental.carservice.repository”)应用程序时,启动了该应用程序,但Rest API不起作用并返回404。我尝试了@Qualifier并命名了Repository,但仍然不起作用。 启动时出现错误:

***************************
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;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableEurekaClient
//@ComponentScan("com.carrental.carservice.repository")
public class CarServiceApp {

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

控制器

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

我尝试了很多东西,但仍然无法正常工作。你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

我认为您需要使用feignClient来避免出现此异常。我有类似的问题,并添加了 @EnableFeignClients到我的ApplicationClass并依赖于您的pom

setScrollY