资源获取总结:
Spring提供了很多 Resource 的实现,下面对以下四种进行总结:
ClassPathResource与FileSystemResource,ClassPathXmlApplicationContext与FileSystemXmlApplicationContextzhi。
以spring in actiong 中第一个例子为背景讨论:spring版的helloworld
程序清单1.1GreetingService接口,将实现与接口分离出来
package com.springinaction.chapter01.hello;
public interface GreetingService {
public void sayGreeting();
}
程序清单1.2GreetingServiceImpl负责打印问候语
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;
}
}
程序清单1.3在spring中配置helloworld,路径在src下面:
<?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"
class="com.springinaction.chapter01.hello.GreetingServiceImpl">
<property name="greeting">
<value>Hello world!</value>
</property>
</bean>
</beans>
程序清单1.4helloworld示例的主类
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{
//代码分析,具体实现在后面
}
}
具体实现一: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/否则报错。
//个人认为最简单的一种
ApplicationContext factory=new ClassPathXmlApplicationContext("hello.xml");
GreetingService greetingService=(GreetingService)factory.getBean("greetingService");
greetingService.sayGreeting();
分享到:
相关推荐
4. 资源包中包括:Win32_TemperatureProbe class, WinIO,Intel用户手册,Io.dll,Ring0(获取Ring0权限),Core Temp工具,Coretemp.c(Linux内核包中的代码)以及自己写的自测程序CPUTemperature
Android中如何获取网络资源(如图片,音乐,视频,文档等)的大小! 较小的一个总结 不过很实用!
在这里,我来总结一下关于资源文件的相关操作。 //1.比较常见的有获取资源文件对应的文件流,然后转换到相对应的文件 //比较典型的做法是通过代码程序集加载指定资源 //如下通过Assembly的静态方法...
摘要:为更加有效的完成开放获取资源采集,本文通过对专家遴选的开放获取资源进行调研分析,总结了开放获取资源的特点。并通过对当前元数据采集方法和采集系统的特点以及在
使用 Delphi7 + IdDay 编写的互联网时间同步小程序,能够获取互联网上多台网络服务器的时间,并...资源分定得不低,但是下载使用后你会觉得这10分下载得很超值,因为我也是花了不少积分才总结出这么个资源,谢谢支持。
一、获取android工程里面的各种资源的id; 1.1 string型 比如下面: <”OK”>> 客户端请求成功 << / string>> //string资源中 名称为OK的 id 获取方式 int strId = getResources().getIdentifier(OK, ...
在本篇文章里小编给大家整理的是关于java中获取类资源的方法总结,需要的朋友们可以学习参考下。
文档介绍了用C#怎样获取Windows诸如我的电脑,桌面的图标,还能够根据文件名(含文件后缀)获取文件类型的图标,如...本人是在做资源管理器的时候用到了,所以总结了下代码。希望对大家有用。同时也参考了网上资源。
以山东能源集团为例,分析“走出去”获取资源的实践经验,总结企业获取资源存在的问题,对企业未来的高质量可持续发展提出了建议,为其他煤炭企业获取资源提供有价值的参考和借鉴,同时也为推动我国煤炭产业可持续发展、...
网上盗版,想看看这个网站如何获取积分 网上盗版,想看看这个网站如何获取积分
新能源汽车学习总结.doc
C# 获取网络流量和硬盘信息 本人收藏了3年的资源 现放出 都是总结了很多系统 软件项目实施过程中的经验的 慢慢积累的
NET视频获取摄像头源代码及截图 本人收藏了3年的资源 现放出 都是总结了很多系统 软件项目实施过程中的经验的 慢慢积累的
Activity中的Menu使用总结示例源码
仁爱英语七年级下册Unit6-topic2复习总结材料.doc
资源包含环境搭建的说明文档和...资源主要介绍在VS2010使用C++语言实现28181协议获取h264流的环境和工程搭建完整过程,以及包含调试环境配置的说明,并提供完整的工程和环境编译的资源和代码。工程可直接编译和运行。
数据库调优可以使数据库应用运行得更快,它需要综合考虑各种复杂的因素。将数据均匀分布在磁盘上可以提高i/o利用率,提高数据的读写性能;适当程度的非规范化可以改善系统查询性能;建立索引和编写高效的sql语句能...
C#编写获取计算机名MAC地址,IP地址工具 源代码 本人收藏了3年的资源 现放出 都是总结了很多系统 软件项目实施过程中的经验的 慢慢积累的
症状:安装了防火墙(包括WINXP系统自带的防火墙),出现无法Ping通,或者是访问共享资源的时候提示"XXX无法访问"、"您可能没有权限使用网络资源"、"请与这台服务器的管理员联系以查明您是否有访问权限"、"找不到...