我尝试使用SpringBoot设置mongodb,我不使用context.xml,但尝试使用配置类和注释进行配置。
package com.configuration;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@Configuration
@EnableMongoRepositories(basePackages = "com.mongo")
public class MongoConfig extends AbstractMongoConfiguration {
@Override
protected String getDatabaseName() {
return "mongodbname";
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient("127.0.0.1", 27017);
}
@Override
protected String getMappingBasePackage() {
return "com.mongo";
}
}
我的存储库看起来像这样:
package com.mongo.repositories;
import com.mongo.documents.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("sequenceRepository")
public class SequenceRepository{
@Autowired
private MongoTemplate mongoTemplate;
public Sequence findOne(Query query){
return this.mongoTemplate.findOne(query,Sequence.class);
}
public List<Sequence> find(Query query){
return this.mongoTemplate.find(query,Sequence.class);
}
public void save(Sequence object){
this.mongoTemplate.save(object);
}
public void delete(Sequence object){
this.mongoTemplate.remove(object);
}
}
如果我在其余控制器中使用这样的话,它可以正常工作:
package com.controllers;
import com.mongo.documents.Sequence;
import com.mongo.repositories.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@Autowired
private SequenceRepository sequenceRepository;
@RequestMapping("/")
public String index(){
Sequence sequence = new Sequence();
sequence.setClassName("class.Test");
sequence.setActual(1);
sequenceRepository.save(sequence);
return "index";
}
}
但是,如果我想在Sequence文档中使用SequenceRepository,如下所示:
package com.mongo.documents;
import com.mongo.repositories.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Sequence {
@Autowired
private SequenceRepository sequenceRepository;
@Id
private String id;
private String className;
private int actual;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public int getActual() {
return actual;
}
public void setActual(int actual) {
this.actual = actual;
}
public void save(){
this.sequenceRepository.save(this);
}
public void delete(){
this.sequenceRepository.delete(this);
}
}
之后我将控制器中的代码更改为:
package com.controllers;
import com.mongo.documents.Sequence;
import com.mongo.repositories.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@Autowired
private SequenceRepository sequenceRepository;
@RequestMapping("/")
public String index(){
Sequence sequence = new Sequence();
sequence.setClassName("class.Test");
sequence.setActual(1);
sequence.save();
return "index";
}
}
此时我在save()方法中得到了一个nullPointerExceeption。
答案 0 :(得分:3)
没有必要在域对象Sequence
中注入您的存储库,因为在域对象和存储库类之间存在依赖关系是非常糟糕的设计。
要遵循一些良好做法,您的Service
课程,或者如果您不需要服务层,您的Controller
课程应该注入您的存储库bean。
在您的上一个代码段中,您已经在执行此操作,但应在您的存储库.save()
上调用SequenceRepository
方法,而不是在域对象Sequence
上调用。
一个例子如下:
@RestController
public class IndexController {
@Autowired
private SequenceRepository sequenceRepository;
@RequestMapping("/")
public String index(){
Sequence sequence = new Sequence();
sequence.setClassName("class.Test");
sequence.setActual(1);
// now let's save it
sequenceRepository.save(sequence);
return "index";
}
}
答案 1 :(得分:1)
您只能将Spring管理的组件自动装配到其他Spring管理的组件中。您的序列对象不是组件(不使用@ Component,@ Service,@ Repository或@Controller等进行注释)。
我建议你遵循rieckpil的建议。