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 ...
随机推荐
- hibernate之三种时态之间的转换
判断状态的标准 oid 和 session相关性 public class HibernateUtils { private static SessionFactory sessionFact ...
- 最基本的CentOS 网络配置
一般CentOS 网络配置是根据自己的需求来设定的.但是,对于一些不经常用CentOS的用户来说,不知道基本的CentOS 网络配置.如果你没有特别的要考虑的设置,那么就可以考虑下我推荐的这种Cent ...
- javascript 模块化编程
The module pattern is a common JavaScript coding pattern. It’s generally well understood, but there ...
- Eclipse tomcat配置 未在Eclipse中添加.jar包出错
JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bui ...
- js-倒计时应用
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- bootstrap2.1
<html> <head> <meta charset="utf-8" /> <title></title> ...
- AngularJS $scope 继承性 作用 生命周期
一.基本概念 作用域是一个指向应用模型的对象,相当于MVVM中的ViewModel,能绑定数据(属性)和行为(方法),能监控表达式和传递事件,是实现双向绑定的基础,是应用在 HTML (视图) 和 J ...
- cocos游戏网址
http://www.cocos.com/doc/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framewor ...
- G 最水的一道
G - Here Be Dragons The Triwizard Tournament's third task is to negotiate a corridor of many segment ...
- 【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)
有任何疑问请去我的新博客提出 https://blog.clso.fun/posts/2019-03-03/vb-net-zxing-net-qr-maker.html ZXing .NET 的项目主 ...