**IoC初始化过程
首先spring IoC容器的初始化,要分成三大部分,BeanDefinition的
Resource定位、载入和注册三个基本过程。
今天我要说的就是资源文件的定位,IoC容器就像是一个大水桶,首先我要将水注入吧,我们要去哪找水呢,当然要从我们的给的配置文件中了,小编写了一段特别简单的代码,然后将spring的源码,导入,断点调试一步步跟进去,篇幅比较大,请谅解。**
BeanFactory fac = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory是基类接口,至于这个基类在哪,可以看下面这张图。
**上面这张图,就是IoC容器的主要继承关系。
断点进入classPathXmlAppliactionContext时,我们会发现,所有的操作都是在下面这段代码中开始的:**
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
- 就是refresh这段代码开始的资源定位,载入和注册的。看一下refresh的代码:
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}
个人理解,就像是一个目录,在这里可以看到整个IoC容器工作的目录,下面就开始真正的资源定位阶段了,首选要对BeanFactory进行判断,判断是否有水桶,或者水桶里面是否有东西等等,我要保证我的水桶是干净的,大不了我换一个新的。
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
后面则开始BeanDefinition的查找了(找水源),看下面代码:
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}
上面是两种不同的方式来找水,一是我们src下的配置文件,另一个则是资源文件。我们是通过配置文件来查找的。
public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
//这里取得ResourceLoader,使用的是DefaultResourceLoader
ResourceLoader resourceLoader = getResourceLoader();
if (resourceLoader == null) {
throw new BeanDefinitionStoreException(
"Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
}
//针对Resource的路径模式进行解析,可以使多个路径,可以使多个文件
if (resourceLoader instanceof ResourcePatternResolver) {
// Resource pattern matching available.
try {
//调用DefaultResourceLoader的getResource完成具体的Resource定位
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
int loadCount = loadBeanDefinitions(resources);
if (actualResources != null) {
for (Resource resource : resources) {
actualResources.add(resource);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
}
return loadCount;
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"Could not resolve bean definition resource pattern [" + location + "]", ex);
}
}
else {
// Can only load single resources by absolute URL.
Resource resource = resourceLoader.getResource(location);
int loadCount = loadBeanDefinitions(resource);
if (actualResources != null) {
actualResources.add(resource);
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
}
return loadCount;
}
}
最后来看看取得Resource的具体过程:
//对于取得Resource的具体过程,看看defaultResourceLoader
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
//这里处理带有classpath标志的resource
if (location.startsWith(CLASSPATH_URL_PREFIX)) {
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
}
else {
try {
// Try to parse the location as a URL…
//这里处理URL标识的Resource定位
URL url = new URL(location);
return new UrlResource(url);
}
catch (MalformedURLException ex) {
// No URL -> resolve as resource path.
//如果不是classpath又不是URL 把getResource的任务交给getResourceByPath,这个方法是protected方法,默认实现是得到一个classPathContextResource,这个方法常常用子类实现
return getResourceByPath(location);
}
}
}
代码跟到这,就开始返回了,返回到前面提到的refresh的目录中,开始进行下一个阶段的解析,BeanDefinition的载入和解析,下篇博客会继续讲。