Results 1 to 6 of 6

Thread: XmlBeanFactory - cannot read FileInputStream and can validate InputStreamResource

  1. #1
    Join Date
    Jun 2007
    Posts
    8

    Default 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. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    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

  3. #3
    Join Date
    Jun 2007
    Posts
    8

    Default

    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

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

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

  5. #5
    Join Date
    May 2008
    Posts
    1

    Default

    You can use ClassPathResource

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

    It should work.

  6. #6
    Join Date
    Feb 2011
    Posts
    1

    Default 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •