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 ...
随机推荐
- 关于arcgis发布wfs问题
博客地址http://www.cnblogs.com/shizhongtao/p/3453594.html 官方文档中有这么一段描述: 从地图创建 WFS 服务 您 可以从 ArcMap 地图文档 ( ...
- HOWTO re
\w 字母数字字符 [a-z A-Z 0-9_] \W 非字母数组字符 [^a-z-A-Z 0-9_] \d 十进制数字 [0-9] \D 非数字字符 [^0-9] \s 空白字符 [\t\n\r\f ...
- NSS_02 日志配置
采用log4net,使用系统推荐的最新版本:log4net-1.2.11-bin-newkey.zip(网址:http://logging.apache.org/log4net/download_lo ...
- Eclipse中将项目导出jar包,以及转化成exe的方法
Eclipse中将项目导出jar包,以及转化成exe的方法 http://wenku.baidu.com/view/385597f07c1cfad6195fa7c6.html
- SQL行列转换:报表_公司采购表_每个公司各采购了些什么产品
有同学问了个比较典型行列转换的问题,想想,解答如下:数据库有一张表: 是个公司采购表,想转化成如下报表,显示每个公司各采购了些什么产品: 哪些公司采购哪些产品是不确定的,所以报表的列有哪几项是不确定的 ...
- bootstrap响应式网页设计的9条基本原则!
bootstrap响应式网页设计是针对多屏幕问题的一个很好的解决方案,但从印刷的视角来看有点困难.没有固定的页面尺寸,没有毫米或英寸,没有任何的物理限制,无从下手. 为了desktop和mobile单 ...
- xml学习总结(四)
命名空间 (1)产生 问题:在不同的约束文档中,有不同好安逸的相同标记名称 解决办法 每个约束模式人当被赋予一个唯一的名称空间,每个名称空间可用一个唯一的URI表示 在XML实例中为来自不同模式文档的 ...
- LeetCode 解题报告--202Happy Number
1 题目描述 Write an algorithm to determine if a number is "happy". A happy number is a number ...
- Asp.Net MVC过滤器小试牛刀
在上学期间学习的Asp.Net MVC,基本只是大概马马虎虎的了解,基本处于知其然而不知其所以然.现在到上班,接触到真实的项目,才发现还不够用,于是从最简单的过滤器开始学习.不得不说MVC的过滤器真是 ...
- mvc简单execl导出
直接上代码: public static byte[] GetExecl(DataTable dt, List<string> list) { var sbHtml = new Strin ...