照着书上的例程写的,其中HelloApp.java如下:
~
BeanFactory factory=new XmlBeanFactory(new ClassPathResource("hello.xml"));
运行程序,出异常:class path resource [hello.xml] cannot be opened because it does not exist;
调试N久查不出问题所在。。。
最后baidu了下,把语句改成下面:
Resource resource=new FileSystemResource("src/main/resources/hello.xml");
BeanFactory factory=new XmlBeanFactory(resource);
运行程序,成功!
hello.xml采用相对路径或绝对路径都没有问题。。。~~
由此可见,书本例程可能会误导人~~
查了下原因,网上是这么说的:具体还不是很理解~
主要是载入Spring配置文件的方式不同。
ClassPathResource --- 从系统的类路径中加载
FileSystemResource --- 从文件系统加载,比如说自己指定配置文件的全路径
InputStreamResource --- 从输入流中加载
ServletContextResource --- 从Servlet 上下文环境中加载
UrlResource --- 从指定的Url加载
每次跑个HelloWorld程序都这么折腾,NND~难道就是为了印证万事开头难这句话么?。。。
参考文档:http://www.360doc.com/content/090320/13/117846_2863720.html