spring 资源访问
spring 资源访问
Resource resource=null;
//访问网络资源
resource=new UrlResource("file:bool.xml");
//访问类加载路径下的资源
resource=new ClassPathResource("book.xml");
//访问文件系统资源
resource=new FileSystemResource("book.xml");
//访问字节数组资源
String str="";
byte[] fileBytes=str.getBytes();
resource=new ByteArrayResource(fileBytes);
//resource=new ServletContextResource()
//resource=new InputStreamResource()
InputStream inputStream= resource.getInputStream();
boolean exists=resource.exists();
boolean isOpen=resource.isOpen();
String description=resource.getDescription();
File file=resource.getFile();
URL url= resource.getURL(); ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
resource=ctx.getResource("book.xml"); ctx=new FileSystemXmlApplicationContext("beans.xml");
ctx=new FileSystemXmlApplicationContext("/beans.xml");
//加载相对路径
ctx=new FileSystemXmlApplicationContext("file:beans.xml");
//加载绝对路径
ctx=new FileSystemXmlApplicationContext("file:/beans.xml");
ctx=new FileSystemXmlApplicationContext("classpath:beans.xml");
//加载多个配置文件
ctx=new FileSystemXmlApplicationContext("classpath*:beans.xml");
ctx=new ClassPathXmlApplicationContext("beans*.xml");
ctx=new FileSystemXmlApplicationContext("classpath*:bean*.xml");
配置管理
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; //修饰一个Java配置类
@Configuration
//导入XML配置
@ImportResource("classpath:/beans.xml")
public class AppConfig {
//修饰一个方法,将该方法的返回值定义成容器中的一个Bean
@Bean
public Person person(){ }
@Bean(name = "stoneAxe")
public Axe stoneAxe(){ }
@Bean(name = "steelAxe")
public Axe steelAxe(){ }
}
spring 资源访问的更多相关文章
- spring资源访问接口和资源加载接口
spring 资源访问接口 JDK提供的资源访问类,如java.net.URL.File等,不能很好地满足各种资源的访问需求,比如缺少从类路径或者Web容器的上下文中获取资源的操作类. 鉴于此,spr ...
- Spring资源访问
资源访问 1.Resource Jdk提供的访问资源的类并不能很好地满足各种底层的资源访问需求, 比如缺少从类路径或者web容器的上下文中获取资源的操作类. 鉴于此, Spring设计了一个Resou ...
- 攻城狮在路上(贰) Spring(三)--- Spring 资源访问利器Resource接口
Spring为了更好的满足各种底层资源的访问需求.设计了一个Resource接口,提供了更强的访问底层资源的能力.Spring框架使用Resource装载各种资源,包括配置文件资源.国际化属性文件资源 ...
- Spring资源访问接口Resource
该接口拥有对不同资源类型的实现类 boolean exists() 资源是否存在 boolean isOpen() 资源是否打开 URL getURL() 如果底层资源可以表示成URL,则该方法返回对 ...
- 05.Spring 资源加载 - Resource
基本概念 Spring 把所有能记录信息的载体,如各种类型的文件.二进制流等都称为资源. 对 Spring 开发者来说,最常用的资源就是 Spring 配置文件(通常是一份 XML 格式的文件). S ...
- Spring源码分析——资源访问利器Resource之实现类分析
今天来分析Spring的资源接口Resource的各个实现类.关于它的接口和抽象类,参见上一篇博文——Spring源码分析——资源访问利器Resource之接口和抽象类分析 一.文件系统资源 File ...
- spring mvc官网下最新jar搭建框架-静态资源访问处理-注解-自动扫描
1.从官网下载spring相关jar http://spring.io/projects 点击SPRING FRAMEWORK
- Spring MVC 使用介绍(十一)—— 跨域与静态资源访问
一.跨域 服务端须在响应中添加相应响应头,从而允许跨域,具体可通过 public class CorsFilter extends OncePerRequestFilter { @Override p ...
- Spring Boot 静态资源访问原理解析
一.前言 springboot配置静态资源方式是多种多样,接下来我会介绍其中几种方式,并解析一下其中的原理. 二.使用properties属性进行配置 应该说 spring.mvc.static-pa ...
随机推荐
- jquery实现图片上传前本地预览功能
HTML <img id="pic" src="" > <input id="upload" name="fil ...
- PHP continue break 区别 用法
<?php //continue 跳过当前循环,进行下一个 //break 终止当前循环 $db=new PDO("mysql:host=localhost;dbname=root&q ...
- Page页面生命周期——微信小程序
onLoad:function (options) { //页面初始化 console.log('index Load') }, onShow:function () { // ...
- A Magic Lamp -- hdu -- 3183
http://acm.hdu.edu.cn/showproblem.php?pid=3183 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) ...
- java类与继承(转载)
关于java的类与继承面链接是一个网友总结的,还有列子我个人觉得很详细 固拿来收藏,需要的朋友可从这里访问: http://www.cnblogs.com/dolphin0520/p/3803432. ...
- vue.js入门学习
可以用淘宝npm镜像 然后安装 然后初始化项目: Watch就是一个监听 v-if是如果为false就根本不在页面存在这个元素 v-show是通过display:none来控制这个元素的显示和隐藏 r ...
- security.php
<?php /** * */ class Security { public function csrf_verify() { if(count($_POST) == 0) { return ' ...
- Amoeba常见问题
1.1.1 JAVA_HOME不认 jdk安装后测试无问题java –version,但启动amoeba就是报错JAVA_HOME找不到.就修改/amoeba/bin/amoeba文件,在文件最开头直 ...
- Hibernate在PostgreSQL上执行sum函数导致数据失真的问题
有一段通过Hibernate从PostgreSQL上进行sum统计的简单代码,但统计结果却导致数据失真,不知原因何在,求指教! Java代码片段如下: public List<Object> ...
- 记Asp.Net Core Swagger 使用 并带域接口处理
引用作者原话:Asp.Net的WebApi中使用Swagger作为说明和测试的页面是非常不错的,比起WebApiTestClient来至少在界面上的很大的提升.但是使用Swagger时如果只是一般的控 ...