mongoTemplate为null。无法理解为什么

时间:2016-02-10 16:43:58

标签: mongodb spring-mvc saxparser

我试图解析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";

}

我的解析器:

@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);
}

}

真的不明白为什么我的mongoTemplate为空。因为我的@Controller不是。有谁可以帮助我?

1 个答案:

答案 0 :(得分:0)

因为Spring不知道你的MySaxParser。你不应该以自己的方式来自称,你在控制器里做了什么:

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);
}
}