本帖最后由 deadgod 于 2011-01-17 18:09:01 编辑

解决方案 »

  1.   

    我就是想用InputStream来解决此问题,请高手指点!
      

  2.   

    InputStream 是处理流滴,怎么能用来解析 XML ,  既然用了Spring 就可以用 SPRING  的组件解析XML ,何必那么麻烦
      

  3.   

    初学啊,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.,就这个问题比较纠结不知道怎么解决!
      

  4.   

    为什么要用Java的IO流读取配置文件
    直接用Spring的API不就可以了么
      

  5.   

    你是要解析xml,还是什么。解析xml可以用dom,jdom,dom4j解析撒。
      

  6.   

    InputStreamResource 换成 FileSystemResource就可以,详细原因可以看下源码,主要是InputStreamResource 的isOpen属性为true
      

  7.   

    造成InputStream方式不能创建BeanFactory的原因是,XmlBeanDefintionReader不能自动检测Resource的验证模式(VALIDATION_XSD,.VALIDATION_NONE,.VALIDATION_DTD,
    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);
    }

    }