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

  • XmlBeanFactory - cannot read FileInputStream and can validate InputStreamResource

    Hi,

    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 Reader.java:449)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.getValidationModeForResource(XmlBeanDe finitionReader.java:438)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.doLoadBeanDefinitions(XmlBeanDefinitio nReader.java:404)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:357)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:73)
    at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:61)
    at com.springinaction.chapter01.hello.SpringTestApp.m ain(SpringTestApp.java:23)

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

    Many thanks,

    David

  • #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).
    http://www.springframework.org/docs/...-instantiation

    Comment


    • #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");

      greetingService.sayGreeting();



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

      Thanks,

      David

      Comment


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

        Comment


        • #5
          You can use ClassPathResource

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

          It should work.

          Comment


          • #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

            Comment

            Working...
            X