Spring中的Resource
Spring中的资源定义:Resource
此接口的全名为:org.springframework.core.io.Resource
比较常用的资源定义的实现类为:
1.ClassPathResource 从classpath中读取
2.FileSystemResource 从文件系统中读取
3.UrlResource 从指定URL中读取
4.ServletContextResource 必须要在web环境下使用
1.ClassPathResource
@Test
public void testClassPathResource_1() throws Exception {
// 指定一个相对于classpath根目录的相对路径
Resource resource = new ClassPathResource(//
"com/winner/resource/applicationContext.xml");
System.out.println(resource.getFile().getAbsolutePath());
} @Test
public void testClassPathResource_2() throws Exception {
// 指定一个相对于指定类的相对路径,以下表示配置文件和MainTest在相同的路径下
Resource resource = new ClassPathResource("applicationContext.xml", MainTest.class);
System.out.println(resource.getFile().getAbsolutePath());
}
2.FileSystemResource
@Test
public void testFileSystemResource() throws Exception {
// 指定一个资源路径,推荐写绝对路径,以下表示在C盘下
Resource resource = new FileSystemResource("c:/applicationContext.xml");
System.out.println(resource.getFile().getAbsolutePath());
}
3.UrlResource
@Test
public void testUrlResource() throws Exception {
// 指定一个URL,如file://...或是http://...等等
Resource resource = new UrlResource("file://c:/applicationContext.xml");
System.out.println(resource.getFile().getAbsolutePath());
}
4.ServletContextResource
// 注:要在Web环境中使用
@Test
public void testServletContextResource(){
// 路径开头的斜线代表当前Web应用的根目录
String path = "/WEB-INF/classes/applicationContext.xml";
Resource resource = new ServletContextResource(servletContext, path);
System.out.println(resource.getFile().getAbsolutePath());
}
使用特定格式的字符串表示各种类型的Resource
| Prefix | Example | Explanation |
| classpath: | classpath:com/winner/config.xml | load from the classpath |
| file: | file:/data/config.xml | load from the filesystem |
| http: | http://myserver/logo.jpg | load as a url |
| (none) | /data/config.xml | it depends |
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {//
"com/winner/spring/applicationContext_service.xml",//
"com/winner/spring/applicationContext_dao.xml" });
ApplicationContext ac2 = new ClassPathXmlApplicationContext(new String[] {//
"applicationContext_dao.xml", "applicationContext_service.xml" }, this.getClass());
Spring中的Resource的更多相关文章
- 2:spring中的@resource
@Resource 其实是spring里面的注解注入. @Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定, 如果没有指定name属性,当注解写在字段 ...
- Spring 中的 Resource和ResourceLoader
Spring 内部框架使用org.springframework.core.io.Resource接口作为所有资源的抽象和访问接口.Resource接口可以根据资源的不同类型,或者资源所处的不同场合, ...
- spring中注解@Resource 与@Autowire 区别
① .@Resource 是根据名字进行自动装配:@Autowire是通过类型进行装配. ②. @Resource 注解是 jdk 的:@Autowire 是spring的.
- Spring中的Autowired注解和Resource注解的区别
1.所属jar包不同,Autowired是Spring中的Resource是JSR-250规范定义的注解
- Spring中资源的加载原来是这么一回事啊!
1. 简介 在JDK中 java.net.URL 适用于加载资源的类,但是 URL 的实现类都是访问网络资源的,并没有可以从类路径或者相对路径获取文件及 ServletContext , 虽然可以通过 ...
- Spring中@Autowired注解、@Resource注解的区别 (zz)
Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...
- Spring中Autowired注解,Resource注解和xml default-autowire工作方式异同
前面说到了关于在xml中有提供default-autowire的配置信息,从spring 2.5开始,spring又提供了一个Autowired以及javaEE中标准的Resource注释,都好像可以 ...
- Spring中@Autowired、@Resource和@Inject注解的使用和区别
在使用Spring进行项目开发的时候,会大量使用到自动装配,那自动装配是什么呢?简单来说:Spring 利用依赖注入(DI)功能,完成SpringIOC容器中各个组件之间的依赖关系赋值管理. 下面介绍 ...
- Spring中 @Autowired注解与J2EE@Resource注解的区别
在开发中经常使用到@Autowired和@Resource进行装配. 不禁好奇这两个注解的差异在何处??? 相同点: @Resource的作用相当于@Autowired,均可标注在字段或属性的sett ...
随机推荐
- 0<=i<iLen 在C++中
for( i=0;0<= i<2; i++)这样的话会出现什么错误呢? 一直循环下去, 因为i>=一直成立
- linux ubuntu ppa源
ubuntu10.04添加删除PPA源 增加ppa资源以后,今后的版本更新什么的都会从launchpad去下载,在国内访问launchpad速度比乌龟还慢 Ubuntu里,PPA代表一种非稳定版本 ...
- mysql之创建外键报150错误的处理方法
这几天由于在赶项目进度,也就没有及时记录下自己的学习情况 ,在完成项目的这段时间里,碰到了很多问题,在解决问题的过程中学习了不少技巧. 这里就主要介绍一下在mysql数据库中为表之间建立外键时报100 ...
- Catalyst揭秘 Day4 analyzer解析
Catalyst揭秘 Day4 analyzer解析 今天继续解析catalyst,主要讲一下analyzer,在sql语句的处理流程中,analyzer是在sqlparse的基础上,把unresol ...
- 在centos 6.4下安装opencv 2.3.1
系统环境介绍: centos 6.4 1.安装依赖包 yum install cmake gcc gcc-c++ gtk+-devel gimp-devel gimp-devel-tools gimp ...
- 免信用卡注册美国App Store账号
对于一些应用国内的App Store无法下载让人很郁闷,而自己又有点轻微的强迫症.于是开始尝试免信用卡注册iCloud账号. Apple的官方网站上的教程,见http://support.apple. ...
- lazy instructor
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- dom树的介绍,及原理分析
三.解析和DOM树的构建 1.解析: 由于解析渲染引擎是一个非常重要的过程,我们将会一步步的深入,现在让我们来介绍解析. 解析一个文档,意味着把它转换为一个有意义的结构——代码可以了解和使用的东西,解 ...
- 研读代码必须掌握的Eclipse快捷键
1. Ctrl+左键 和F3 这个是大多数人经常用到的,用来查看变量.方法.类的定义跳到光标所在标识符的定义代码.当按执行流程阅读时,F3实现了大部分导航动作. 2 Ctrl+Shift+G在工作空间 ...
- java和php实现RSA加密互通-b
java和PHP RSA加密实现互通 1:通过openssl 生成公钥和密钥文件(linux) (1) 生产私钥文件命令 openssl genrsa -out rsa_private_key.pe ...