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的更多相关文章

  1. 2:spring中的@resource

    @Resource 其实是spring里面的注解注入. @Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定, 如果没有指定name属性,当注解写在字段 ...

  2. Spring 中的 Resource和ResourceLoader

    Spring 内部框架使用org.springframework.core.io.Resource接口作为所有资源的抽象和访问接口.Resource接口可以根据资源的不同类型,或者资源所处的不同场合, ...

  3. spring中注解@Resource 与@Autowire 区别

    ① .@Resource 是根据名字进行自动装配:@Autowire是通过类型进行装配. ②. @Resource 注解是 jdk 的:@Autowire 是spring的.

  4. Spring中的Autowired注解和Resource注解的区别

    1.所属jar包不同,Autowired是Spring中的Resource是JSR-250规范定义的注解

  5. Spring中资源的加载原来是这么一回事啊!

    1. 简介 在JDK中 java.net.URL 适用于加载资源的类,但是 URL 的实现类都是访问网络资源的,并没有可以从类路径或者相对路径获取文件及 ServletContext , 虽然可以通过 ...

  6. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  7. Spring中Autowired注解,Resource注解和xml default-autowire工作方式异同

    前面说到了关于在xml中有提供default-autowire的配置信息,从spring 2.5开始,spring又提供了一个Autowired以及javaEE中标准的Resource注释,都好像可以 ...

  8. Spring中@Autowired、@Resource和@Inject注解的使用和区别

    在使用Spring进行项目开发的时候,会大量使用到自动装配,那自动装配是什么呢?简单来说:Spring 利用依赖注入(DI)功能,完成SpringIOC容器中各个组件之间的依赖关系赋值管理. 下面介绍 ...

  9. Spring中 @Autowired注解与J2EE@Resource注解的区别

    在开发中经常使用到@Autowired和@Resource进行装配. 不禁好奇这两个注解的差异在何处??? 相同点: @Resource的作用相当于@Autowired,均可标注在字段或属性的sett ...

随机推荐

  1. 【原】从/dev/null重新打开标准输出

    今天遇到一个程序,使用了printf输出中间的信息,我也懒得去改.由于此进程被其他进程fork之后,dup2 了标识输入输出到了/dev/null,再通过execvp装载进来.于是,为了看到输出的信息 ...

  2. Libcurl笔记二

    一: multi与easy接口的不同处The multi interface offers several abilities that the easy interface doesn't. The ...

  3. 仿360手机卫士界面效果android版源码

    仿360手机卫士界面效果android版,这个今天一大早在源码天堂的那个网站上看到了一个那个网站最新更新的一个源码,所以就分享给大学习一下吧,布局还挺不错的,而且也很简单的,我就不把我修改的那个分享出 ...

  4. L008-oldboy-mysql-dba-lesson08

    L008-oldboy-mysql-dba-lesson08 xtrabackup安装 [root@web01 installer]# wget https://www.percona.com/dow ...

  5. TweenMax动画库学习(一)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  6. 使用AE进行点的坐标投影变换

    private IPoint PRJtoGCS( double x, double y) { IPoint pPoint = new PointClass(); pPoint.PutCoords(x, ...

  7. Unity学习笔记(2):注册映射

    在上一篇文章中(认识Unity)中概要介绍了Unity和Ioc,本节主要介绍IoC中的注册映射,并使用代码和配置文件两种方式进行说明. 定义依赖注入相关信息 定义ILogger接口 public in ...

  8. Spark小课堂Week5 Scala初探

    Spark小课堂Week5 Scala初探 Scala是java威力加强版. 对Java的改进 这里会结合StreamingContext.scala这个代码说明下对Java的改进方面. 方便测试方式 ...

  9. [Git]代码管理工具简单使用

    1 Git简介 Git是分布式的版本控制系统,是Linux内核开发者林纳斯·托瓦兹(Linus Torvalds)为更好地管理Linux内核开发而设计.与CVS.Subversion一类的集中式版本控 ...

  10. 利用IDE编写C语言程序的一点注意事项

    前言:我是喜欢编程的一只菜鸟,在自学过程中,对遇到的一些问题和困惑,有时虽有一点体会感悟,但时间一长就会淡忘,很不利于知识的积累.因此,想通过博客园这个平台,一来记录自己的学习体会,二来便于向众多高手 ...