I'm newly at spring.
I can't understand why when I write some easy example and try it load with ApplicationContents
as follows:
package com.appres.prospring3.ch5.factory;
public class MessageDigestExample {
public static void main(String[] args) {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load("classpath:factory/factory.xml");
context.refresh();
MessageDigester digester = (MessageDigester) context.getBean("digester");
digester.digest("Hello World !!!!!!!!!");
}
}
Exactly after this one line :
context.load("classpath:factory/factory.xml");
Going exception message:
17:59:44,480 INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean >definitions from class path resource [factory/factory.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: >IOException parsing XML document from class path resource [factory/factory.xml]; nested >exception is java.io.FileNotFoundException: class path resource [factory/factory.xml] >cannot be opened because it does not exist
To my mind all should work. And I couldn't figure out what is wrong here.
Here is my project structure:
But when I move myFile.xml
to resources
package:
And change context.load() to context.load("classpath:factory.xml");
All works fine and I can see correct result:
Using digest1
Using algorithm: SHA1
[B@5e9ed26e
Using digest2
Using algorithm: MD5
[B@d09644a
Edit:
Of course I tried longest paths for loading this .xml
file, as:
context.load("classpath:com/appress/prospring3/ch5/factory/factory.xml");
And it throws bunch of exceptions:
Exception in thread "main" 18:15:22,385 INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean definitions from class path resource [com/appress/prospring3/ch5/factory/factory.xml]
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/appress/prospring3/ch5/factory/factory.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/appress/prospring3/ch5/factory/factory.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.GenericXmlApplicationContext.load(GenericXmlApplicationContext.java:105)
at com.appres.prospring3.ch5.factory.MessageDigestExample.main(MessageDigestExample.java:8)
Caused by: java.io.FileNotFoundException: class path resource [com/appress/prospring3/ch5/factory/factory.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 7 more
- Why exactly this happen?
- Does exist some way do this from the same location. Where is main()?