【问题标题】:SpringBoot doesn't scan for components in microservices appSpringBoot 不扫描微服务应用程序中的组件
【发布时间】:2019-09-16 19:23:48
【问题描述】:

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

当我添加 @SpringBootApplication("com.carrental.carservice.repository") 应用程序启动但 Rest API 不起作用并返回 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.

并且日志中有一个WARN:

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

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

【问题讨论】:

  • 我不知道为什么,但是您的库版本不兼容。
  • @spencergibb 哪些库?我尝试使用最新版本
  • @Kamilox Spring 库。不要手动添加每个 Spring 库的版本,而是在 maven 构建文件中使用 &lt;parent&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt; &lt;version&gt;2.1.4.RELEASE&lt;/version&gt; &lt;/parent&gt; 之类的东西,并从 spring 依赖项中删除 &lt;version&gt; 标记。让 Spring 的依赖管理为您解析各个库(除非您有充分的理由不这样做)。
  • 您正在混合不同版本的 spring-boot-starter 项目。例如,您使用的是 2.0.9.RELEASE 版本的 spring-boot-starter-web 和 2.1.4.RELEASE 的 spring-boot-starter-data-jpa。
  • 另外spring cloud 1.x库不兼容boot 2.x

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2011-10-14
    • 2020-11-01
    • 2020-04-16
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多