简体   繁体   中英

Spring - field requierd a bean type that could not be found

I am trying to write a simple Spring app (with Maven) that would connect to a mLab mongoDB database, but when I try to run it an error comes up that says:

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


Description:

Field carService in main.Controller.CarController required a bean of type 'main.Service.CarService' that could not be found.

Action:

Consider defining a bean of type 'main.Service.CarService' in your configuration.

I tried some of the methods that I found on StackOverflow (for example reorganising the project structure) but that didn't help, or I'm missing something. This is my project structure:

在此处输入图片说明

And my project files:

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

}

application.properties

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

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

}

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

Car.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>

public class CarService { is just a plain pojo.

Mark it as a bean:

@Service
public class CarService {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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