首先,先上两张图:
下面,就根据这四种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的创建方式了。