赞
踩
首先看看我们是如何使用Spring的
public static void main(String[] args) {
BeanFactory bf=new XmlBeanFactory(new ClassPathResource("bean_test.xml"));
HelloWorldService hw=(HelloWorldService) bf.getBean("helloWorldService");
hw.hello();
}
查看源码(简化版):
首先看看类的一些关系,
public class ClassPathResource extends AbstractFileResolvingResource
public abstract class AbstractResource implements Resource
public class ClassPathResource extends AbstractFileResolvingResource {
private final String path;
private ClassLoader classLoader;
private Class<?> clazz;
/**
* 构造函数,只有一个参数path="xxx.xml"
*/
public ClassPathResource(String path) {
this(path, (ClassLoader) null);
}
/**
* 构造函数,
* 创建实例对象,并初始化参数path,classLoader
*/
public ClassPathResource(String path, ClassLoader classLoader) {
this.path = pathToUse;//pathToUse="bean_test.xml";
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}
对于获取classLoader:ClassUtils.getDefaultClassLoader()
/**
* 返回类加载器
*/
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
//获得类加载器
cl = Thread.currentThread().getContextClassLoader();
return cl;
}
至此我们创建了一个ClassPathResource实例对象,并初始化了path=”bean_test.xml”,classLoader=Thread.currentThread().getContextClassLoader()参数
原版源码:
public class ClassPathResource extends AbstractFileResolvingResource {
private final String path;
private ClassLoader classLoader;
private Class<?> clazz;
/**
* 构造函数,只有一个参数path="xxx.xml"
* Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
* A leading slash will be removed, as the ClassLoader resource access
* methods will not accept it.
* <p>The thread context class loader will be used for
* loading the resource.
* @param path the absolute path within the class path
* @see java.lang.ClassLoader#getResourceAsStream(String)
* @see org.springframework.util.ClassUtils#getDefaultClassLoader()
*/
public ClassPathResource(String path) {
this(path, (ClassLoader) null);
}
/**
* 构造函数,
* 创建实例对象,并初始化参数path,classLoader
* Create a new {@code ClassPathResource} for {@code ClassLoader} usage.
* A leading slash will be removed, as the ClassLoader resource access
* methods will not accept it.
* @param path the absolute path within the classpath
* @param classLoader the class loader to load the resource with,
* or {@code null} for the thread context class loader
* @see ClassLoader#getResourceAsStream(String)
*/
public ClassPathResource(String path, ClassLoader classLoader) {
//Assert,StringUtils暂时不管
Assert.notNull(path, "Path must not be null");
String pathToUse = StringUtils.cleanPath(path);
if (pathToUse.startsWith("/")) {
pathToUse = pathToUse.substring(1);
}
this.path = pathToUse;//pathToUse="bean_test.xml";
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}
/**
* 返回类加载器
* Return the default ClassLoader to use: typically the thread context
* ClassLoader, if available; the ClassLoader that loaded the ClassUtils
* class will be used as fallback.
* <p>Call this method if you intend to use the thread context ClassLoader
* in a scenario where you clearly prefer a non-null ClassLoader reference:
* for example, for class path resource loading (but not necessarily for
* {@code Class.forName}, which accepts a {@code null} ClassLoader
* reference as well).
* @return the default ClassLoader (only {@code null} if even the system
* ClassLoader isn't accessible)
* @see Thread#getContextClassLoader()
* @see ClassLoader#getSystemClassLoader()
*/
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
//获得类加载器
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
if (cl == null) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
try {
cl = ClassLoader.getSystemClassLoader();
}
catch (Throwable ex) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
}
return cl;
}
至此我们创建了一个ClassPathResource实例对象,并初始化了path=”bean_test.xml”,classLoader=Thread.currentThread().getContextClassLoader()参数
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。