Jun 9th, 2007, 08:01 AM
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 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?
Jun 9th, 2007, 10:43 AM
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).
Jun 9th, 2007, 01:01 PM
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.
Jun 9th, 2007, 04:21 PM
Ok great, I'll take a look at the other post!
May 31st, 2008, 11:15 AM
You can use ClassPathResource
BeanFactory factory =new XmlBeanFactory(new ClassPathResource("hello.xml"));
It should work.
Feb 9th, 2011, 05:28 AM
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