【发布时间】:2017-11-02 12:30:21
【问题描述】:
我是 Spring Boot 的新手,我正在尝试使用 Spring Boot 创建一个简单的应用程序,但出现以下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:Error 创建名为'carsApplication'的bean:不满足的依赖关系 通过字段'carMongoRepository'表示;
嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建错误 名为'carMongoRepository'的bean:调用init方法 失败;嵌套异常是 org.springframework.data.mapping.model.MappingException:不能 查找域类 java.lang.Object 的映射元数据!
这是我实现的 Model 类:
具有 id、make 和 model 的汽车模型类,它包括 get 和 set 方法
package com.example.cars;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.stereotype.Component;
@Document(collection = "cars")
public class Car {
private String id;
private String make;
private String model;
public Car() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
这是我实现的 Main 类:
spring boot 应用运行应用的主类
package com.example.cars;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class CarsApplication {
@Autowired
CarMongoRepository carMongoRepository;
public static void main(String[] args) {
SpringApplication.run(CarsApplication.class, args);
}
}
这是我实现的 Controller 类:
用于 API 调用的控制器类,用于添加汽车值,例如 model 和 make。包括 API 调用,如下所示,具有 POST 方法实现。
package com.example.cars;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class CarController {
@Autowired
CarMongoRepository carMongoRepository;
@RequestMapping(value = "/addCar", method = RequestMethod.POST)
public void addCar(@ModelAttribute Car car) {
carMongoRepository.save(car);
}
}
这是我实现的存储库
为应用程序的 CRUD 存储库调用实现的接口为 如下所示。为此实现使用了@Repository 注释。 类名是 CarMongoRepository,它扩展了 CRUDRepository,如下所示。
package com.example.cars;
import org.springframework.data.repository.CrudRepository;
import com.example.cars.Car;
import org.springframework.stereotype.Repository;
@Repository
public interface CarMongoRepository extends CrudRepository {
}
当我尝试运行 spring boot 主类时,出现上述错误。所以,请帮我解决这个问题。
【问题讨论】:
标签: java spring spring-boot spring-data-mongodb