资源获取总结(转)
资源获取总结:
Spring提供了很多 Resource 的实现,下面对以下四种进行总结:
ClassPathResource与FileSystemResource,ClassPathXmlApplicationContext与FileSystemXmlApplicationContextzhi。
以spring in actiong 中第一个例子为背景讨论:spring版的helloworld
程序清单1.1GreetingService接口,将实现与接口分离出来
package com.springinaction.chapter01.hello; public interface GreetingService { public void sayGreeting(); }
package com.springinaction.chapter01.hello; public class GreetingServiceImpl implements GreetingService { private String greeting; public GreetingServiceImpl() {} /** * @param greeting */ public GreetingServiceImpl(String greeting) { this.greeting = greeting; } public void sayGreeting() { System.out.println(this.greeting); } public void setGreeting(String greeting) { this.greeting = greeting; } } package com.springinaction.chapter01.hello;public class GreetingServiceImpl implements GreetingService { private String greeting; public GreetingServiceImpl() {} /** * @param greeting */ public GreetingServiceImpl(String greeting) { this.greeting = greeting; } public void sayGreeting() { System.out.println(this.greeting); } public void setGreeting(String greeting) { this.greeting = greeting; }}
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="greetingService" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <bean id="greetingService" name="code">package com.springinaction.chapter01.hello; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.Resource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.ClassPathResource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloApp { /** * @param args */ public static void main(String[] args) throws Exception{ //代码分析,具体实现在后面 } } package com.springinaction.chapter01.hello;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.Resource;import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.ClassPathResource;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloApp { /** * @param args */ public static void main(String[] args) throws Exception{ //代码分析,具体实现在后面 }}
Resource resource=new ClassPathResource("hello.xml"); BeanFactory factory=new XmlBeanFactory(resource); GreetingService greetingService=(GreetingService)factory.getBean("greetingService"); greetingService.sayGreeting(); Resource resource=new ClassPathResource("hello.xml");BeanFactory factory=new XmlBeanFactory(resource);GreetingService greetingService=(GreetingService)factory.getBean("greetingService");greetingService.sayGreeting();
//直接使用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(); //直接使用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();
//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(); //classpath:前缀可要可不要,不能用src/hello.xml,或classpath:src/hello.xmlApplicationContext 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();
//没有盘符的是项目工作路径,即项目的根目录;不能写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(); //没有盘符的是项目工作路径,即项目的根目录;不能写hello.xml,要写src/hello.xmlApplicationContext 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();
//个人认为最简单的一种 ApplicationContext factory=new ClassPathXmlApplicationContext("hello.xml"); GreetingService greetingService=(GreetingService)factory.getBean("greetingService"); greetingService.sayGreeting();