mongoTemplate 为空。不明白为什么
mongoTemplate is null. Cannot understand why
我正在尝试解析 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 中它不是。谁能帮帮我?
因为 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);
}
}
我正在尝试解析 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 中它不是。谁能帮帮我?
因为 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);
}
}