Mongodb mongoTemplate为空。不明白为什么

Mongodb mongoTemplate为空。不明白为什么,mongodb,spring-mvc,saxparser,Mongodb,Spring Mvc,Saxparser,我正在尝试解析xml文件并在mongodb中保存一些信息。我得到要解析的文件并将其交给我的@Controller。以下是@Controller的POST方法的代码: @RequestMapping(value = TracksGeopointsRoutes.TRACKS, method = RequestMethod.POST) public String tracks(@RequestParam MultipartFile file){ TracksGeopointsDoc trac

我正在尝试解析xml文件并在mongodb中保存一些信息。我得到要解析的文件并将其交给我的@Controller。以下是@Controller的POST方法的代码:

@RequestMapping(value = TracksGeopointsRoutes.TRACKS, method = RequestMethod.POST)
public String tracks(@RequestParam MultipartFile file){

    TracksGeopointsDoc tracksGeopointsDoc = new TracksGeopointsDoc();
    try {
        tracksGeopointsDoc.setFile(tracksGeopointsService.convert(file));
    } catch (IOException e) {
        e.printStackTrace();
    }
    mongoTemplate.save(tracksGeopointsDoc);

   new MySaxParser(tracksGeopointsDoc.getFile().getAbsolutePath()); // here I give my file to my parser
   return "com.ub.geopoints_test.index";

}
new MySaxParser(tracksGeopointsDoc.getFile().getAbsolutePath());
还有我的解析器:

@Component
public class MySaxParser extends DefaultHandler{

@Autowired
MongoTemplate mongoTemplate;

private List<DotGeopointsDoc> dotGeopointsDocList;
String xmlFileName;
private String tmpValue;
private DotGeopointsDoc currentDotGeopointsDoc;
private DotGeopointsDoc dotGeopointsDoc;
String bookXmlFileName;

public MySaxParser() {
}

public MySaxParser(String bookXmlFileName) {
    this.xmlFileName = bookXmlFileName;
    dotGeopointsDocList = new ArrayList<DotGeopointsDoc>();
    dotGeopointsDoc = new DotGeopointsDoc();
    parseDocument();
}

private void parseDocument() {
    // parse
    SAXParserFactory factory = SAXParserFactory.newInstance();
    try {
        SAXParser parser = factory.newSAXParser();
        parser.parse(xmlFileName, this);
    } catch (ParserConfigurationException e) {
        System.out.println("ParserConfig error");
    } catch (SAXException e) {
        System.out.println("SAXException : xml not well formed");
    } catch (IOException e) {
        System.out.println("IO error");
    }
}

@Override
public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
    if (elementName.equalsIgnoreCase("trkpt")) {
        dotGeopointsDoc.setId(new ObjectId());
        dotGeopointsDoc.setLat(attributes.getValue("lat"));
        dotGeopointsDoc.setLon(attributes.getValue("lon"));
    }

}

@Override
public void endElement(String s, String s1, String element) throws SAXException {
    if (element.equals("trkpt")) {
        dotGeopointsDocList.add(dotGeopointsDoc);
        mongoTemplate.save(dotGeopointsDoc); // here I'm getting NullPointerException. My mongoTemplate is null
        dotGeopointsDoc = new DotGeopointsDoc();
    }

}

@Override
public void characters(char[] ac, int i, int j) throws SAXException {
    tmpValue = new String(ac, i, j);
}
@组件
公共类MySaxParser扩展了DefaultHandler{
@自动连线
MongoTemplate MongoTemplate;
私有列表点地理点DocList;
字符串xmlFileName;
私有字符串tmpValue;
专用点地理点Doc currentDotGeopointsDoc;
专用点地理点DotGeopointsDoc;
字符串文件名;
公共MySaxParser(){
}
公共MySaxParser(字符串bookXmlFileName){
this.xmlFileName=bookXmlFileName;
dotGeopointsDocList=新的ArrayList();
dotGeopointsDoc=新的dotGeopointsDoc();
解析文档();
}
私有文档(){
//解析
SAXParserFactory=SAXParserFactory.newInstance();
试一试{
SAXParser parser=factory.newSAXParser();
parser.parse(xmlFileName,this);
}捕获(ParserConfiguration异常e){
System.out.println(“ParserConfig错误”);
}捕获(SAXE异常){
System.out.println(“SAXException:xml格式不正确”);
}捕获(IOE异常){
System.out.println(“IO错误”);
}
}
@凌驾
public void startElement(字符串s、字符串s1、字符串elementName、属性)引发SAXException{
if(elementName.equalsIgnoreCase(“trkpt”)){
dotgeopPointsDoc.setId(新ObjectId());
dotgeopPointsDoc.setLat(attributes.getValue(“lat”);
dotGeopointsDoc.setLon(attributes.getValue(“lon”));
}
}
@凌驾
公共void endElement(字符串s、字符串s1、字符串元素)引发SAXException{
if(元素等于(“trkpt”)){
dotGeopointsDocList.add(dotGeopointsDoc);
save(dotGeopointsDoc);//这里我得到的是NullPointerException。我的mongoTemplate为null
dotGeopointsDoc=新的dotGeopointsDoc();
}
}
@凌驾
公共无效字符(char[]ac,int i,int j)引发异常{
tmpValue=新字符串(ac,i,j);
}
}


我真的不明白为什么我的mongoTemplate是空的。因为在我的@Controller中它不是。有人能帮我吗?

这是因为Spring不知道您的
MySaxParser
。你不应该亲自指示你在控制器中所做的事情:

@RequestMapping(value = TracksGeopointsRoutes.TRACKS, method = RequestMethod.POST)
public String tracks(@RequestParam MultipartFile file){

    TracksGeopointsDoc tracksGeopointsDoc = new TracksGeopointsDoc();
    try {
        tracksGeopointsDoc.setFile(tracksGeopointsService.convert(file));
    } catch (IOException e) {
        e.printStackTrace();
    }
    mongoTemplate.save(tracksGeopointsDoc);

   new MySaxParser(tracksGeopointsDoc.getFile().getAbsolutePath()); // here I give my file to my parser
   return "com.ub.geopoints_test.index";

}
new MySaxParser(tracksGeopointsDoc.getFile().getAbsolutePath());
以下是控制器的外观:

@Autowired
private MySaxParser mySaxParser;//this is how you can inject a spring managed object

@RequestMapping(value = TracksGeopointsRoutes.TRACKS, method = RequestMethod.POST)
public String tracks(@RequestParam MultipartFile file) {

    TracksGeopointsDoc tracksGeopointsDoc = new TracksGeopointsDoc();
    try {
        tracksGeopointsDoc.setFile(tracksGeopointsService.convert(file));
    } catch (IOException e) {
        e.printStackTrace();
    }
    mongoTemplate.save(tracksGeopointsDoc);

    mySaxParser.setUpMySaxParser(tracksGeopointsDoc.getFile().getAbsolutePath());
    return "com.ub.geopoints_test.index";

}
以下是您的xml解析器应该如何更改:

@Service//this is more of a service then a component
public class MySaxParser extends DefaultHandler {

@Autowired
private MongoTemplate mongoTemplate;

private List<DotGeopointsDoc> dotGeopointsDocList;
private String xmlFileName;
private String tmpValue;
private DotGeopointsDoc currentDotGeopointsDoc;
private DotGeopointsDoc dotGeopointsDoc;
private String bookXmlFileName;

//you do not need constuctors here just a "setup method ex."
public void setUpMySaxParser(String bookXmlFileName) {
    this.xmlFileName = bookXmlFileName;
    dotGeopointsDocList = new ArrayList<DotGeopointsDoc>();
    dotGeopointsDoc = new DotGeopointsDoc();
    parseDocument();
}

private void parseDocument() {
    // parse
    SAXParserFactory factory = SAXParserFactory.newInstance();
    try {
        SAXParser parser = factory.newSAXParser();
        parser.parse(xmlFileName, this);
    } catch (ParserConfigurationException e) {
        System.out.println("ParserConfig error");
    } catch (SAXException e) {
        System.out.println("SAXException : xml not well formed");
    } catch (IOException e) {
        System.out.println("IO error");
    }
}

@Override
public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
    if (elementName.equalsIgnoreCase("trkpt")) {
        dotGeopointsDoc.setId(new ObjectId());
        dotGeopointsDoc.setLat(attributes.getValue("lat"));
        dotGeopointsDoc.setLon(attributes.getValue("lon"));
    }

}

@Override
public void endElement(String s, String s1, String element) throws SAXException {
    if (element.equals("trkpt")) {
        dotGeopointsDocList.add(dotGeopointsDoc);
        mongoTemplate.save(dotGeopointsDoc); // here I'm getting NullPointerException. My mongoTemplate is null
        dotGeopointsDoc = new DotGeopointsDoc();
    }

}

@Override
public void characters(char[] ac, int i, int j) throws SAXException {
    tmpValue = new String(ac, i, j);
}
}
@Service//这与其说是一个组件,不如说是一个服务
公共类MySaxParser扩展了DefaultHandler{
@自动连线
私有MongoTemplate MongoTemplate;
私有列表点地理点DocList;
私有字符串xmlFileName;
私有字符串tmpValue;
专用点地理点Doc currentDotGeopointsDoc;
专用点地理点DotGeopointsDoc;
私有字符串bookXmlFileName;
//这里不需要构造函数,只需要一个“setup method ex”
public void setUpMySaxParser(字符串bookXmlFileName){
this.xmlFileName=bookXmlFileName;
dotGeopointsDocList=新的ArrayList();
dotGeopointsDoc=新的dotGeopointsDoc();
解析文档();
}
私有文档(){
//解析
SAXParserFactory=SAXParserFactory.newInstance();
试一试{
SAXParser parser=factory.newSAXParser();
parser.parse(xmlFileName,this);
}捕获(ParserConfiguration异常e){
System.out.println(“ParserConfig错误”);
}捕获(SAXE异常){
System.out.println(“SAXException:xml格式不正确”);
}捕获(IOE异常){
System.out.println(“IO错误”);
}
}
@凌驾
public void startElement(字符串s、字符串s1、字符串elementName、属性)引发SAXException{
if(elementName.equalsIgnoreCase(“trkpt”)){
dotgeopPointsDoc.setId(新ObjectId());
dotgeopPointsDoc.setLat(attributes.getValue(“lat”);
dotGeopointsDoc.setLon(attributes.getValue(“lon”));
}
}
@凌驾
公共void endElement(字符串s、字符串s1、字符串元素)引发SAXException{
if(元素等于(“trkpt”)){
dotGeopointsDocList.add(dotGeopointsDoc);
save(dotGeopointsDoc);//这里我得到的是NullPointerException。我的mongoTemplate为null
dotGeopointsDoc=新的dotGeopointsDoc();
}
}
@凌驾
公共无效字符(char[]ac,int i,int j)引发异常{
tmpValue=新字符串(ac,i,j);
}
}