Passed-in Resource [resource loaded through InputStream] contains an open stream:cannot determine

Passed-in Resource [resource loaded through InputStream] contains an open stream2009-09-29 11:58  org.springframework.beans.factory.BeanDefinitionStoreException: 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.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.detectValidationMode(XmlBeanDefinitionReader.java:418)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.getValidationModeForResource(XmlBeanDefinitionReader.java:400)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:360)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:313)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:290)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at com.spring.unit1.TestMain.main(TestMain.java:31)
Exception in thread "main" java.lang.NullPointerException
at com.spring.unit1.TestMain.main(TestMain.java:40)

今天开始正式学习Spring,从《Spring in Action》开始,虽然版本老了一些,但是关键是思想,在做第一个练习的时候,原书中代码如下:

BeanFactory factory = new XmlBeanFactory(new FileInputStream("hello.xml"));

可是在2.0以后的版本中,XmlBeanFactory已经被修改;查阅了API发现构造方法的参数修改为Resource类型

查看了一下InputStreamResource 就是Resource接口的实现类;

于是:

   File file = new File("D:/eclipse/workSpace/aSpringDemo/src/hello.xml");
    InputStream is = new FileInputStream(file);
    InputStreamResource isr = new InputStreamResource(is);
    bf = new XmlBeanFactory(isr);

但是却出了上边的异常;

查看源码发现:

reader.loadBeanDefinitions(resource);方法会调用如下代码:

protected int detectValidationMode(Resource resource)
    {
        InputStream inputStream;
        IOException ex;
        if(resource.isOpen())
            throw new BeanDefinitionStoreException("Passed-in Resource [" + resource + "] 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.");

再次查看InputStreamResource API:

implementation for a given InputStream. Should only be used if no specific Resource implementation is applicable. In particular, prefer or any of the file-based Resource implementations where possible.

In contrast to other Resource implementations, this is a descriptor for an already opened resource - therefore returning "true" from isOpen(). Do not use it if you need to keep the resource descriptor somewhere, or if you need to read a stream multiple times.

显然  

public boolean isOpen()
    {
        return true;
    }


于是,使用其他Resource实现类替换比如FileSystemResource,一切OK

例:

File file = new File("F:/java web2.0/javawebProject/SpringApp/src/spring-beans.xml");

FileSystemResource fsr = new FileSystemResource(file);

XmlBeanFactory factory = new XmlBeanFactory(fsr);

在API中,关于InputStreamResource有这样一段描述:它只有在没有其他合适的Resource实现时才使用。而且,只要有可能就尽量使用ByteArrayResource或者其他基于文件的Resource实现。

与其他Resource实现不同的是,这是个 已经 打开资源的描述符-因此isOpen()方法返回true。如果你需要在其他位置保持这个资源的描述符或者多次读取一个流,请不要使用它。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值