Spring源码解析—— ClassPathResource类

简介:   一,简单介绍Spring中资源处理相关类BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.


  一,简单介绍Spring中资源处理相关类


BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));






在Spring中,定义了接口InputStreamSource,这个类中只包含一个方法:



public interface InputStreamSource {

	/**
	 * Return an {@link InputStream}.
	 * <p>It is expected that each call creates a <i>fresh</i> stream.
	 * <p>This requirement is particularly important when you consider an API such
	 * as JavaMail, which needs to be able to read the stream multiple times when
	 * creating mail attachments. For such a use case, it is <i>required</i>
	 * that each {@code getInputStream()} call returns a fresh stream.
	 * @return the input stream for the underlying resource (must not be {@code null})
	 * @throws IOException if the stream could not be opened
	 * @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)
	 */
	InputStream getInputStream() throws IOException;

}

    用来返回一个基本的InputStream文件。


    之后,使用Resource接口来规定对文件的一些基本的操作。对于不同来源的件:classpath,file,url,byte,inputstream...进行处理。在我们的应用程序里面,比较常用的就是对我们classpath下的xml文件进行解析的ClassPathResource。


二,ClassPathResource的构造函数


         利用ClassPathResource读取xml配置的基本思路就是通过构造函数传入的文件路径,接着交给class或者classLoader,调用getResourceAsStream获取到InputStream。


       所以,我们的private成员变量是这样子定义的:


public class ClassPathResource extends AbstractFileResolvingResource {

	private final String path;

	private ClassLoader classLoader;

	private Class<?> clazz;


    主要构造函数(存在多个构造函数,但是都差不多啦):


public ClassPathResource(String path, ClassLoader classLoader) {
		Assert.notNull(path, "Path must not be null");
		String pathToUse = StringUtils.cleanPath(path);
		if (pathToUse.startsWith("/")) {
			pathToUse = pathToUse.substring(1);
		}
		this.path = pathToUse;
		this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
	}

    Assert类是spring的一个校验类,比如,如果path为空的话,则会抛出一个异常,异常信息为后面的"Path must not be null"。其实就是封装了一个判断是否为null的操作,查看Assert,发现还有很多类似函数,例如,判断为trueorfalse这种。


     之后处理你自己传入的路径。。。。因为这里默认使用ClassLoader来加载资源,所以,路径开头要去掉“/”。但是如果你不传入classLoader,则classLoader按照如下规则获取:


   

public static ClassLoader getDefaultClassLoader() {
		ClassLoader cl = null;
		try {
			cl = Thread.currentThread().getContextClassLoader();
		}
		catch (Throwable ex) {
			// Cannot access thread context ClassLoader - falling back to system class loader...
		}
		if (cl == null) {
			// No thread context class loader -> use class loader of this class.
			cl = ClassUtils.class.getClassLoader();
		}
		return cl;
	}


     首选我们安全的从当前线程获取类加载器的方法,但是这个方法在 某些情况下会返回null值(可以百度下Thread.currentThread().getContextClassLoader()获取classloader跟使用**Class.getClassLoader()方法有什么不同,如果懒的话,可以看看下面的代码),这个跟JVM的类加载器的层级关系有关。。。


 

/* 测试利用class或者classloader读取文件classpath下的文件 */
	public static void getResourceByClassOrClassLoader() {
		/*
		 * Classloader是从classpath中读取资源的一个类,
		 * 一般我们用classloader来加载class,实际上,但凡是处在classpath中的文件,
		 * 我们称之为资源,都可以用classloader来读取。
		 * 注意,使用classloader来加载资源的时候,目录前面不加“/”
		 */
		System.out.println(TestResource.class.getClassLoader().getResource(
				"applicationContext.xml"));

		/* 这个API的起始路径是当前类的路径,如果要正确的读到资源,目标资源必须和当前class在同一级,或子目录里,可以用相对路径读取到。 */
		System.out.println(TestResource.class
				.getResource("/applicationContext.xml"));

		/*安全用法---提倡使用  From:http://www.cnblogs.com/gaoxing/p/4703412.html*/
		System.out.println( Thread.currentThread().getContextClassLoader().getResource(
				"applicationContext.xml"));
		
		/*读取到InputStream*/
		InputStream inputStream = TestResource.class.getClassLoader()
				.getSystemResourceAsStream("applicationContext.xml");
		System.out.println(inputStream);

	}

   区分下使用class 还有classLoader 这两种方式getResource在写法跟实现上有什么不同。


   这之后,构造函数完成, XML文件加载完毕。



三,其他方法


   1,示例:exists

@Override
	public boolean exists() {
		URL url;
		if (this.clazz != null) {
			url = this.clazz.getResource(this.path);
		}
		else {
			url = this.classLoader.getResource(this.path);
		}
		return (url != null);
	}


       在对资源文件文件进行操作的时候,我们的首选还是使用Class类来进行操作,因为资源和我类文件的位置是相对固定的,可能这是一种潜规则+默认的写法吧。如果执行构造函数的时候没有传入class来进行初始化,则使用classLoader来判断文件。最 后return (url != null);写法挺简洁!

      这个类里面的getInputStream,getURL,getDescription都是类似的。


       希望没丢掉什么重点。求补充!






 


目录
相关文章
|
1天前
|
canal 缓存 关系型数据库
Spring Boot整合canal实现数据一致性解决方案解析-部署+实战
Spring Boot整合canal实现数据一致性解决方案解析-部署+实战
|
1天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
8天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
9天前
yolo-world 源码解析(六)(2)
yolo-world 源码解析(六)
18 0
|
9天前
yolo-world 源码解析(六)(1)
yolo-world 源码解析(六)
13 0
|
XML 前端开发 Java
Spring 最常用的 7 大类注解,史上最强整理!
随着技术的更新迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。
|
28天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
43 0
|
2月前
|
缓存 Java Maven
Spring Boot自动配置原理
Spring Boot自动配置原理
48 0
|
1月前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
111 0
|
1月前
|
前端开发 搜索推荐 Java
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革

推荐镜像

更多