JAVA读取Spring配置的bean文件
2015-03-30 17:39
453 查看
转http://xplq.iteye.com/blog/222912
资源获取总结:
Spring提供了很多 Resource 的实现,下面对以下四种进行总结:
ClassPathResource与FileSystemResource,ClassPathXmlApplicationContext与FileSystemXmlApplicationContextzhi。
以spring in actiong 中第一个例子为背景讨论:spring版的helloworld
具体实现一:ClassPathResource
(1)不能用src/hello.xml,
(2)不能用classpath前缀,如classpath:hello.xml,或classpath:src/hello.xml
(3)不能用绝对路径。
具体实现二:FileSystemResource
(1)使用:src/hello.xml,而不能使用classpath:前缀如classpath:src/hello.xml,或classpath:hello.xml
(2)使用绝对路径,但不能用前缀file
具体实现三:ClassPathXmlApplicationContext
(1)classpath: 前缀可要可不要的, 默认就是指项目的classpath路径下面;但是不能用src/hello.xml,或classpath:src/hello.xml
(2)如果要使用绝对路径,需要加上 file: 前缀表示这是绝对路径;注意,一定要加上file
具体实现四:FileSystemXmlApplicationContext
总结:使用FileSystemXmlApplicationContext,默认表示的是两种:
(1)没有盘符的是项目工作路径,即项目的根目录;不能写hello.xml,要写src/hello.xml
(2)有盘符表示的是 文件绝对路径:file:前缀可要可不要
(3)如果要使用classpath路径,需要前缀classpath,但是加上classpath則不能加上src/否则报错。
资源获取总结:
Spring提供了很多 Resource 的实现,下面对以下四种进行总结:
ClassPathResource与FileSystemResource,ClassPathXmlApplicationContext与FileSystemXmlApplicationContextzhi。
以spring in actiong 中第一个例子为背景讨论:spring版的helloworld
具体实现一:ClassPathResource
Resource resource=new ClassPathResource("hello.xml"); BeanFactory factory=new XmlBeanFactory(resource); GreetingService greetingService=(GreetingService)factory.getBean("greetingService"); greetingService.sayGreeting();说明:使用ClassPathResource,只能直接使用:hello.xml,而:
(1)不能用src/hello.xml,
(2)不能用classpath前缀,如classpath:hello.xml,或classpath:src/hello.xml
(3)不能用绝对路径。
具体实现二:FileSystemResource
//直接使用src/hello.xml,而不能使用classpath:前缀 Resource resource=new FileSystemResource("src/hello.xml"); //或使用绝对路径,但不能用file:前缀 //Resource resource=new FileSystemResource("E:/Eclipse-JEE/mywork/SpringInAction/src/hello.xml"); BeanFactory factory=new XmlBeanFactory(resource); GreetingService greetingService=(GreetingService)factory.getBean("greetingService"); greetingService.sayGreeting();说明:使用FileSystemResource,
(1)使用:src/hello.xml,而不能使用classpath:前缀如classpath:src/hello.xml,或classpath:hello.xml
(2)使用绝对路径,但不能用前缀file
具体实现三:ClassPathXmlApplicationContext
//classpath:前缀可要可不要,不能用src/hello.xml,或classpath:src/hello.xml ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:hello.xml");//也可以为hello.xml //或使用绝对路径,需要加上 file: 前缀表示这是绝对路径;注意,一定要加上file: //ApplicationContext factory=new ClassPathXmlApplicationContext("file:E:/Eclipse-JEE/mywork/SpringInAction/src/hello.xml"); GreetingService greetingService=(GreetingService)factory.getBean("greetingService"); greetingService.sayGreeting();总结:使用ClassPathXmlApplicationContext:
(1)classpath: 前缀可要可不要的, 默认就是指项目的classpath路径下面;但是不能用src/hello.xml,或classpath:src/hello.xml
(2)如果要使用绝对路径,需要加上 file: 前缀表示这是绝对路径;注意,一定要加上file
具体实现四:FileSystemXmlApplicationContext
//没有盘符的是项目工作路径,即项目的根目录;不能写hello.xml,要写src/hello.xml ApplicationContext factory=new FileSystemXmlApplicationContext("src/hello.xml"); //文件绝对路径:file:前缀可要可不要 //ApplicationContext factory=new FileSystemXmlApplicationContext("E: /Eclipse-JEE/mywork/SpringInAction/src/hello.xml");//也可加上file: //可以使用classpath路径, 需要前缀 classpath:但是如加上classpath則不能加上src/否则报错。 //ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:hello.xml"); GreetingService greetingService=(GreetingService)factory.getBean("greetingService"); greetingService.sayGreeting();
总结:使用FileSystemXmlApplicationContext,默认表示的是两种:
(1)没有盘符的是项目工作路径,即项目的根目录;不能写hello.xml,要写src/hello.xml
(2)有盘符表示的是 文件绝对路径:file:前缀可要可不要
(3)如果要使用classpath路径,需要前缀classpath,但是加上classpath則不能加上src/否则报错。
相关文章推荐
- Spring中读取bean配置文件的几种方式
- 如何告诉spring初始化指定配置文件中的javabean
- 从spring配置文件中读取bean的方法
- spring中读取xml配置文件、获取bean的几种方式
- spring中读取xml配置文件、获取bean的几种方式
- java读取多个spring 配置文件
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- spring读取配置文件,获取bean的几种方式
- Java 学习笔记04:Spring XML配置文件Bean
- 用dom4j读取配置文件来模拟spring的bean工厂
- java代码中获得spring配置文件中的bean
- spring中读取xml配置文件、获取bean的几种方式
- 自定义spring容器--spring容器读取bean配置文件的原理
- SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
- Java opencsv--使用 Spring 的 PostConstruct机制,项目启动时,将CSV配置文件读取到内存 map 中
- Spring中读取bean配置文件的几种方式
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
- spring中读取xml配置文件、获取bean的几种方式
- 模拟Spring注入和读取配置文件信息(支持bean 书写顺序随意)