繁体   English   中英

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

[英]SpringBoot doesn't scan for components in microservices app

我正在编写基于 Spring Cloud 的微服务应用程序。 我启动了 Eureka Server,现在我正在编写汽车服务。 当我在项目中没有任何自动装配时,它就起作用了。 添加 Repository、Service 和更改 Controller 后,car-service 不会启动。

当我添加 @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);
}

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

我认为您需要使用 feignClient 来避免此异常。我有类似的问题并将 @EnableFeignClients 添加到我的 ApplicationClass 及其对您的 pom 的依赖

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM