Announcement Announcement Module
No announcement yet.
XmlBeanFactory - cannot read FileInputStream and can validate InputStreamResource Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • XmlBeanFactory - cannot read FileInputStream and can validate InputStreamResource


    I'm just starting out with Spring and have begun working through the first example in Spring In Action but can't get it to work. I tried the example as stated loading a FileInputStream directly into the XmlBeanFactory constructor and got the following but the code won't compile:

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

    COMPILE ERROR: 'The constructor XmlBeanFactory(FileInputStream) is undefined'

    I did some digging and discovered that an alternative would be to use an InputStreamResource. This compiled but failed during execution:

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

    RUNTIME ERROR: xception in thread "main" org.springframework.beans.factory.BeanDefinitionSt oreException: 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.XmlBeanDefin itionReader.detectValidationMode(XmlBeanDefinition
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.getValidationModeForResource(XmlBeanDe
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.doLoadBeanDefinitions(XmlBeanDefinitio
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR
    at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(
    at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(
    at com.springinaction.chapter01.hello.SpringTestApp.m ain(

    Can anyone advise what's going on and what I need to do to get up and running?

    Many thanks,


  • #2
    The reference manual has some examples that should help. I usually go with an ApplicationContext purely because I need more features than BeanFactory provides. Lots of people also have problems when they forget that BeanFactory only offers bean configuration (it's covered in the first section of the reference manual).


    • #3
      Thanks - I used the following which appears to have worked:

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

      GreetingService greetingService = (GreetingService) factory.getBean("greetingService");


      Now having problems finding the GreetingService bean as described in the xml file - I'll post this separately.




      • #4
        Ok great, I'll take a look at the other post!


        • #5
          You can use ClassPathResource

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

          It should work.


          • #6
            does not also work

            i have similar problem i have all code posted in the thread but it seems not to work please can some on help me