Spring 引导中的上下文初始化问题
context initialization Issue in Spring Boot
我是 spring boot 的新手 我正在尝试使用 spring 创建一个简单的应用程序 boot 我收到如下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:Error
creating bean with name'carsApplication':Unsatisfied dependency
expressed through field'carMongoRepository';
nested exception is
org.springframework.beans.factory.BeanCreationException:Error creating
bean with name'carMongoRepository':Invocation of init method
failed;nested exception is
org.springframework.data.mapping.model.MappingException:Could not
lookup mapping metadata for domain class java.lang.Object!
This is the project structure
这是我实现的模型class:
型号 class 用于具有 ID、品牌和型号的汽车,它包括获取和设置方法
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;
}
}
这是我实现的主要class:
主class为spring启动应用运行应用
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);
}
}
这是我实现的控制器class:
Controller class 用于 API 调用添加汽车值如模型和 make.Include 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 存储库调用实现了接口
显示了此实现的 below.Used @Repository 注释。
Class 名称是扩展 CRUDRepository 的 CarMongoRepository,如下所示。
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 main class 遇到上述错误 above.So,请帮我解决这个问题。
CrudRepository 需要通用参数来知道它应该处理哪个实体(或文档)。
让你的 CarMongoRepository
扩展 CrudRepository<Car, String>
它应该可以工作。
第一个参数指定存储库应处理的 entity/document 类型,第二个参数是此 entity/document 的 ID 类型(我假设它是字段 id
类型String
在你的车里class)
如果您不指定任何参数,它会假设您的 CarMongoRepository
扩展 CrudRepository<Object, Object>
,这就是错误消息告诉您的内容。
org.springframework.data.mapping.model.MappingException:Could not lookup mapping metadata for domain class java.lang.Object!
您的应用程序尝试查找未定义的 Entity/Document 类型 Object
的映射。
我不太熟悉使用 spring 记录存储数据库,但我认为您必须在 Car
class 中使用 @Id
(至少那是 @Entity
classes 的注释。也许 @Document
classes 有不同的等价物。)
我是 spring boot 的新手 我正在尝试使用 spring 创建一个简单的应用程序 boot 我收到如下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name'carsApplication':Unsatisfied dependency expressed through field'carMongoRepository';
nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name'carMongoRepository':Invocation of init method failed;nested exception is org.springframework.data.mapping.model.MappingException:Could not lookup mapping metadata for domain class java.lang.Object!
This is the project structure
这是我实现的模型class:
型号 class 用于具有 ID、品牌和型号的汽车,它包括获取和设置方法
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;
}
}
这是我实现的主要class:
主class为spring启动应用运行应用
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);
}
}
这是我实现的控制器class:
Controller class 用于 API 调用添加汽车值如模型和 make.Include 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 存储库调用实现了接口 显示了此实现的 below.Used @Repository 注释。 Class 名称是扩展 CRUDRepository 的 CarMongoRepository,如下所示。
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 main class 遇到上述错误 above.So,请帮我解决这个问题。
CrudRepository 需要通用参数来知道它应该处理哪个实体(或文档)。
让你的 CarMongoRepository
扩展 CrudRepository<Car, String>
它应该可以工作。
第一个参数指定存储库应处理的 entity/document 类型,第二个参数是此 entity/document 的 ID 类型(我假设它是字段 id
类型String
在你的车里class)
如果您不指定任何参数,它会假设您的 CarMongoRepository
扩展 CrudRepository<Object, Object>
,这就是错误消息告诉您的内容。
org.springframework.data.mapping.model.MappingException:Could not lookup mapping metadata for domain class java.lang.Object!
您的应用程序尝试查找未定义的 Entity/Document 类型 Object
的映射。
我不太熟悉使用 spring 记录存储数据库,但我认为您必须在 Car
class 中使用 @Id
(至少那是 @Entity
classes 的注释。也许 @Document
classes 有不同的等价物。)