我正在尝试编写一个简单的Spring应用程序(使用Maven),该应用程序将连接到mLab mongoDB数据库,但是当我尝试运行该应用程序时,出现错误消息:
***************************申请无法开始
描述:
main.Controller.CarController中的字段carService需要一个Bean
找不到类型“ main.Service.CarService”。
行动:
考虑在您的应用中定义类型为“ main.Service.CarService”的bean
组态。
我尝试了一些在StackOverflow上发现的方法(例如,重新组织项目结构),但没有帮助,或者我丢失了一些东西。
这是我的项目结构:
和我的项目文件:
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:[email protected]: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);
}
汽车.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 {
只是普通的pojo。
将其标记为bean:
@Service
public class CarService {