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 ...
随机推荐
- Spring零碎知识复习
自学了Spring也有一段时间了,多多少少掌握了一些Spring的知识,现在手上也没有很多的项目练手,就将就着把这些学到的东西先收集起来,方便日后用到的时候没地方找. 1.spring的国际化 主要是 ...
- ubuntu配置多个DNS
Ubuntu设置了静态IP地址,设置DNS,打开/etc/resolv.conf cat /etc/resolv.conf# Dynamic resolv.conf(5) file for glibc ...
- Cookie和Seesion的区别
一.Cookie对象: 1.Cookie是由网络服务器发送出来,存在在浏览器上,它是个存储在浏览器目录中的文本文件.当浏览该cookie对应的站点时,cookie作为http头部文件的一部分在浏览器和 ...
- Kubernetes Architecture
reference:https://www.symantec.com/connect/blogs/google-kubernetes-analytical-evaluation
- javascript的setTimeout以及setInterval休眠问题。
前端码农们在做项目中时候,必定不可少的需要做到轮播效果.但是有些特殊的需求,比如: 需要做到第一个容器内容轮播滚动之后,第二个容器内部再轮播滚动,再第三个容器内容轮播滚动. 这时候我的一开始的思路是: ...
- PHP SQL注入的防范
说到网站安全就不得不提到SQL注入(SQL Injection),如果你用过ASP,对SQL注入一定有比较深的理解,PHP的安全性相对较高,这是因为MYSQL4以下的版本不支持子语句,而且当php.i ...
- Linux下通过shell脚本创建账户
当我们在linux平台上开发一些项目时,或者有一些项目是需要部署到linux系统上时,有时候会涉及到linux上的特定的账户,例如有一些项目需要运行在某些特定的账户下,或者有时候需要在全新的环境上搭建 ...
- windows 与fedora时间差
windows 默认BIOS时间当前时间UTC+时区, 按北京时间时区,就是要加8个小时. Linux默认BIOS时间是UTC时间,所以同一机子上装WINDOWS与LINUX时间上会差8个小时.这问题 ...
- splObjectStroge的作用,实例化一个数组
PHP SPL SplObjectStorage是用来存储一组对象的,特别是当你需要唯一标识对象的时候. PHP SPL SplObjectStorage类实现了Countable,Iterator, ...
- CPU使用率
CPU使用率 事故回放 当时的情况是那个样子的: 1,正值饭点,客户电话说系统慢,几乎无法完成订单调度,有时还显示内存不足.当时心里的第一个声音就是,服务器配置太低了,远程一看,2核4G内存,cpu平 ...