InputStream 是处理流滴,怎么能用来解析 XML , 既然用了Spring 就可以用 SPRING 的组件解析XML ,何必那么麻烦
初学啊,Passed-in Resource [resource loaded through InputStream] contains an open stream: cannot determine validation mode automatically. Either pass in a Resource that is able to create fresh streams, or explicitly specify the validationMode on your XmlBeanDefinitionReader instance.,就这个问题比较纠结不知道怎么解决!
直接用Spring的API不就可以了么
VALIDATION_AUTO),所以要从 InputSteam 方式获取BeanFactory,就必须将 XmlBeanFactory中的reader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) 的模式设置为applicatonContext.xml中对应的模式,但是它为private final 的,所以须自定义一个XmlFileInputStreamBeanFactory类,如下package com.springinaction.chapter01.hello;import org.springframework.core.io.Resource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;/**
* Bean factory takes bean definitions from XML file input stream.
*
* @author Frederick Lewis
* @date Dec 21, 2011
*/
public class XmlFileInputStreamBeanFactory extends DefaultListableBeanFactory {
private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
public XmlFileInputStreamBeanFactory(Resource resource) throws BeansException{
this(resource, null);
}
public XmlFileInputStreamBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
super(parentBeanFactory);
System.out.println(XmlBeanDefinitionReader.VALIDATION_XSD);
System.out.println("The parentBeanFactory of XmlFileInputStreamBeanFactory is: " + parentBeanFactory);
this.reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
this.reader.loadBeanDefinitions(resource);
}
}