首页 > 解决方案 > Spring - 字段需要一个找不到的 bean 类型

问题描述

我正在尝试编写一个简单的 Spring 应用程序(使用 Maven)来连接到 mLab mongoDB 数据库,但是当我尝试运行它时会出现一个错误,上面写着:

****************************** 应用程序无法启动


描述:

main.Controller.CarController 中的字段 carService 需要找不到类型为“main.Service.CarService”的 bean。

行动:

考虑在您的配置中定义一个“main.Service.CarService”类型的 bean。

我尝试了一些在 StackOverflow 上找到的方法(例如重新组织项目结构),但没有帮助,或者我遗漏了一些东西。这是我的项目结构:

在此处输入图像描述

还有我的项目文件:

主.java

package main.Controller;

import main.Entity.Car;
import main.Service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@RequestMapping("/cars")

public class CarController {
    @Autowired
    private CarService carService;

    @RequestMapping(method = RequestMethod.GET)
    public Collection<Car> getAllCars(){
        return this.carService.getAllCars();
    }

}

应用程序属性

server.port = 3000
spring.data.mongodb.uri=mongodb://user:pass@ds211309.mlab.com:11309/cars

汽车控制器.java

package main.Controller;

import main.Entity.Car;
import main.Service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@RequestMapping("/cars")

public class CarController {
    @Autowired
    private CarService carService;

    @RequestMapping(method = RequestMethod.GET)
    public Collection<Car> getAllCars(){
        return this.carService.getAllCars();
    }

}

汽车服务.java

package main.Service;

import main.Dao.CarRepository;
import main.Entity.Car;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;

public class CarService {

    @Autowired
    private CarRepository carRepo;

    public Collection<Car> getAllCars() {
        return carRepo.findAll();
    }
}

CarRepository.java

package main.Dao;

import main.Entity.Car;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface CarRepository extends MongoRepository<Car, String> {

    public Car findByModel(String model);
    public List<Car> findByMake(String make);
}

汽车.java

package main.Entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Date;

@Document(collection = "cars")
public class Car {

    @Id
    public String id;

    public String make;
    public String model;
    public String body;
    public String engine;
    public int totalRentals;
    public int seats;
    public int price;
    public double totalIncome;
    public Date updated_date;

    public Car(String id, String make, String model, String body, String engine, int totalRentals, int seats, int price, double totalIncome, Date updated_date) {
        this.id = id;
        this.make = make;
        this.model = model;
        this.body = body;
        this.engine = engine;
        this.totalRentals = totalRentals;
        this.seats = seats;
        this.price = price;
        this.totalIncome = totalIncome;
        this.updated_date = updated_date;
    }

    @Override
    public String toString() {
        return String.format(
                "Car[id=%s, make='%s', model='%s', body='%s', engine='%s', totalRentals='%s', seats='%s', price='%s', totalIncome='%s', updated_date='%s']",
                id, make, model, body, engine, totalRentals, seats, price, totalIncome, updated_date);
    }



}

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0

<groupId>com.gdyniarent</groupId>
<artifactId>GdyniaRent backend API</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </repository>

</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>

</pluginRepositories>

标签: javaspringmaven

解决方案


public class CarService {只是一个普通的pojo。

将其标记为 bean:

@Service
public class CarService {

推荐阅读