Spring:resource的相关配置

本文通过四个示例探讨Spring中resource的加载方式,包括classpath、file及不加前缀的加载。实验结果显示,不加前缀时,默认依赖于ApplicationContext创建方式,即采用classpath。文章深入分析了这一现象的原因,涉及UnitTestBase源码中ApplicationContext的创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,先上两张图:

下面,就根据这四种load方式来做实验

1.classpath实现load

//1.类
package com.imooc.resource;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

//因为上面提到了所有的application contexts都实现了resourceLoader接口,所以这里实现该Aware(为啥?)
public class MoocResource implements ApplicationContextAware  {
	
	private ApplicationContext applicationContext;
	//重写方法,实现接口必须的操作,set方法构造实例
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	public void resource() throws IOException {
        //获取resource路径,classpath方式
		Resource resource = applicationContext.getResource("classpath:config.txt");
        //两个resource方法,获取文件名和内容大小
		System.out.println(resource.getFilename());
		System.out.println(resource.contentLength());
	}

}

//2.测试类
package com.imooc.test.resource;

import java.io.IOException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.resource.MoocResource;
import com.imooc.test.base.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestResource extends UnitTestBase {
	
	public TestResource() {
		super("classpath:spring-resource.xml");
	}
	
	@Test
	public void testResource() {
		MoocResource resource = super.getBean("moocResource");
		try {
			resource.resource();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

//3.xml文件除了基本的id和class配置外,没有其他特殊配置,省略

输出结果(classpath模式):

8月 02, 2018 3:17:11 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 15:17:11 CST 2018]; root of context hierarchy
8月 02, 2018 3:17:11 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-resource.xml]
config.txt
0
8月 02, 2018 3:17:11 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 15:17:11 CST 2018]; root of context hierarchy

输出结果(file方式):

//load部分代码改为相应的文件路径
Resource resource = applicationContext.getResource("file:E:\\eclipse\\workspace\\Spring\\src\\main\\resources\\config.txt");

//结果
8月 02, 2018 3:22:57 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 15:22:57 CST 2018]; root of context hierarchy
8月 02, 2018 3:22:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-resource.xml]
config.txt
0
8月 02, 2018 3:22:57 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 15:22:57 CST 2018]; root of context hierarchy

输出结果(什么都不加的方式)

//1.load方式
Resource resource = applicationContext.getResource("config.txt");

//2.测试结果
8月 02, 2018 3:25:37 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 15:25:37 CST 2018]; root of context hierarchy
8月 02, 2018 3:25:37 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-resource.xml]
config.txt
0
8月 02, 2018 3:25:37 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 15:25:37 CST 2018]; root of context hierarchy

可以看到输出了同样的结果,原因是什么,上面讲到取决于ApplicationContext,所以打开UnitTestBase源码看下ApplicationContext是如何创建的,

@Before
	public void before() {
		if (StringUtils.isEmpty(springXmlpath)) {
			springXmlpath = "classpath*:spring-*.xml";
		}
		try {
			context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
			context.start();
		} catch (BeansException e) {
			e.printStackTrace();
		}
	}

可以看到,在before方法里面,通过classpath来创建的,

所以如果load前缀什么都不写,就是默认依赖于applicationContext的创建方式,也就是classpath的创建方式了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值