【发布时间】:2017-11-20 03:42:45
【问题描述】:
我的学生实体类
package com.example.entity;
import java.io.Serializable;
import javax.persistence.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "student")
public class StudentMongo implements Serializable {
private static final long serialVersionUID = 8764013757545132519L;
@Id
private Long id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
我的仓库
package com.example.repository;
import javax.annotation.Resource;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import org.springframework.stereotype.Repository;
import com.example.entity.StudentMongo;
@Repository
public class StudentMongoRepository extends SimpleMongoRepository<StudentMongo, Long> {
@Resource
MongoOperations mongoOperations;
public StudentMongoRepository(MongoEntityInformation<StudentMongo, Long> metadata, MongoOperations mongoOperations) {
super(metadata, mongoOperations);
}
}
我的配置类
@Configuration
@EnableMongoRepositories(basePackageClasses = {com.example.repository.StudentMongoRepository.class})
public class MongoConfiguration {
}
Spring 启动应用程序
当我尝试启动应用程序时,我得到以下应用程序
2017-11-20 09:04:48.937 ERROR 23220 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.repository.StudentMongoRepository required a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' in your configuration.
考虑在你的配置中定义一个 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' 类型的 bean 如spring框架所说,如何创建EntityInformation bean 运行我的应用程序时遇到上述问题。如何传递实体信息
建议我如何使用 SimpleMongorepository
【问题讨论】:
-
将@Service 注解添加到您的 mongoOperations 类中。
-
@Habil 为什么要在 mongoOperations 上使用 @Service??
标签: spring mongodb spring-boot spring-data-mongodb