您的位置:首页 > 编程语言 > Java开发

spring源码学习之路---IOC实现原理

2014-03-05 00:49 676 查看
上一章我们已经初步认识了BeanFactory和BeanDefinition,一个是IOC的核心工厂接口,一个是IOC的bean定义接口,上章提到说我们无法让BeanFactory持有一个Map<String,Object>来完成bean工厂的功能,是因为spring的初始化是可以控制的,可以到用的时候才将bean实例化供开发者使用,除非我们将bean的lazy-init属性设置为true,初始化bean工厂时采用延迟加载。
那么知道了上述两个接口,我相信不少人甚至不看源码都已经猜到spring是如何做的了。没错,就是让bean工厂持有一个Map<String,BeanDefinition>,这样就可以在任何时候我们想用哪个bean,取到它的bean定义,我们就可以创造出一个新鲜的实例。
接口当然不可能持有这样一个对象,那么这个对象一定是在BeanFactory的某个实现类或者抽象实现类当中所持有的,我经过跋山涉水,终于把它给找出来了,来看DefaultListableBeanFactory。

[java] view
plaincopy

/*

* Copyright 2002-2010 the original author or authors.

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0
*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package org.springframework.beans.factory.support;

import java.io.NotSerializableException;

import java.io.ObjectStreamException;

import java.io.Serializable;

import java.lang.annotation.Annotation;

import java.lang.ref.Reference;

import java.lang.ref.WeakReference;

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

import java.security.AccessController;

import java.security.PrivilegedAction;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collection;

import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.LinkedHashSet;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Provider;

import org.springframework.beans.BeansException;

import org.springframework.beans.FatalBeanException;

import org.springframework.beans.TypeConverter;

import org.springframework.beans.factory.BeanCreationException;

import org.springframework.beans.factory.BeanCurrentlyInCreationException;

import org.springframework.beans.factory.BeanDefinitionStoreException;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.BeanFactoryAware;

import org.springframework.beans.factory.BeanFactoryUtils;

import org.springframework.beans.factory.CannotLoadBeanClassException;

import org.springframework.beans.factory.FactoryBean;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;

import org.springframework.beans.factory.ObjectFactory;

import org.springframework.beans.factory.SmartFactoryBean;

import org.springframework.beans.factory.config.BeanDefinition;

import org.springframework.beans.factory.config.BeanDefinitionHolder;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import org.springframework.beans.factory.config.DependencyDescriptor;

import org.springframework.core.annotation.AnnotationUtils;

import org.springframework.util.Assert;

import org.springframework.util.ObjectUtils;

import org.springframework.util.StringUtils;

/**

* Default implementation of the

* {@link org.springframework.beans.factory.ListableBeanFactory} and

* {@link BeanDefinitionRegistry} interfaces: a full-fledged bean factory

* based on bean definition objects.

*

* <p>Typical usage is registering all bean definitions first (possibly read

* from a bean definition file), before accessing beans. Bean definition lookup

* is therefore an inexpensive operation in a local bean definition table,

* operating on pre-built bean definition metadata objects.

*

* <p>Can be used as a standalone bean factory, or as a superclass for custom

* bean factories. Note that readers for specific bean definition formats are

* typically implemented separately rather than as bean factory subclasses:

* see for example {@link PropertiesBeanDefinitionReader} and

* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.

*

* <p>For an alternative implementation of the

* {@link org.springframework.beans.factory.ListableBeanFactory} interface,

* have a look at {@link StaticListableBeanFactory}, which manages existing

* bean instances rather than creating new ones based on bean definitions.

*

* @author Rod Johnson

* @author Juergen Hoeller

* @author Sam Brannen

* @author Costin Leau

* @since 16 April 2001

* @see StaticListableBeanFactory

* @see PropertiesBeanDefinitionReader

* @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader

*/

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory

implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {

private static Class javaxInjectProviderClass = null;

static {

ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();

try {

javaxInjectProviderClass = cl.loadClass("javax.inject.Provider");

}

catch (ClassNotFoundException ex) {

// JSR-330 API not available - Provider interface simply not supported then.

}

}

/** Map from serialized id to factory instance */

private static final Map<String, Reference<DefaultListableBeanFactory>> serializableFactories =

new ConcurrentHashMap<String, Reference<DefaultListableBeanFactory>>();

/** Optional id for this factory, for serialization purposes */

private String serializationId;

/** Whether to allow re-registration of a different definition with the same name */

private boolean allowBeanDefinitionOverriding = true;

/** Whether to allow eager class loading even for lazy-init beans */

private boolean allowEagerClassLoading = true;

/** Resolver to use for checking if a bean definition is an autowire candidate */

private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver();

/** Map from dependency type to corresponding autowired value */

private final Map<Class, Object> resolvableDependencies = new HashMap<Class, Object>();

/** Map of bean definition objects, keyed by bean name */

private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();

/** List of bean definition names, in registration order */

private final List<String> beanDefinitionNames = new ArrayList<String>();

/** Whether bean definition metadata may be cached for all beans */

private boolean configurationFrozen = false;

/** Cached array of bean definition names in case of frozen configuration */

private String[] frozenBeanDefinitionNames;

}

注明下,这里我省略了下面N多行源码,源码太长,而且太多的话容易混乱,切勿认为此类就这么多了。
看它名字就知道,这是一个默认的bean工厂实现类,也就是说,如果你需要的功能非常单一,这个实现类已经足够可以满足你了,而以后如果你想要对spring的容器扩展,那么只需要扩展或者持有这个对象即可。

[java] view
plaincopy

/** Map of bean definition objects, keyed by bean name */

final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();

看到这一行,其实已经证明了我们的猜测,即使英文不太好,也能看懂它所注释的意思是bean定义的MAP对象,采用beanName作为key值。
走到这里,思路已经很明确了,bean工厂的初始化其实就是往这个Map里填充东西。只要把我们XML文件中定义的bean都填充到这里,其实这个工厂就已经可以工作了。
那么从现在来看,我们需要什么才能把Map填充呢?也就是初始化bean工厂呢,或者说建立IOC容器。我首先列出来以下几步。
1.需要一个File指向我们的XML文件(本文的配置文件都已XML为例,因为这是我们最熟悉的),专业点可以叫资源定位,简单点可以说我们需要一些工具来完成找到XML文件的所在位置。
2.需要一个Reader来读取我们的XML文件,专业点叫DOM解析,简单点说,就是把XML文件的各种定义都给拿出来。
3.需要将读出来的数据都设置到Map当中。
这三部总结起来就是定位、解析、注册。我们首先按照这个思路来试一下。
直接上代码,我们还使用原来的Person类作为一个Bean。

[java] view
plaincopy

package com.springframework.beans.test;

public class Person {

public void work(){

System.out.println("I am working");

}

}

我们还需要写一个简单的XML文件,beans.xml。

[html] view
plaincopy

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="person" class="com.springframework.beans.test.Person"></bean>

</beans>

下面是我们根据上述的思路写一段程序,来看看会发生什么情况。

[java] view
plaincopy

package com.springframework.beans.test;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

import org.springframework.core.io.ClassPathResource;

public class TestDefaultListableBeanFactory {

public static void main(String[] args) {

ClassPathResource classPathResource = new ClassPathResource("beans.xml");

DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();

XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory);

xmlBeanDefinitionReader.loadBeanDefinitions(classPathResource);

System.out.println("numbers: " + defaultListableBeanFactory.getBeanDefinitionCount());

((Person)defaultListableBeanFactory.getBean("person")).work();

}

}

以下是输出结果。



可以看到,结果与我们期望的是一样的,成功的解析了XML文件,并注册了一个bean定义,而且我们使用getBean方法也成功得到了Person的实例。
上述这段程序当中可以看出,bean工厂的初始化一共使用了四行程序。
第一行完成了我们的第一步,即资源定位,采用classpath定位,因为我的beans.xml文件是放在src下面的。
第二行创建了一个默认的bean工厂。
第三行创建了一个reader,从名字就不难看出,这个reader是用来读取XML文件的。这一步要多说一句,其中将我们创建的defaultListableBeanFactory作为参数传给了reader的构造函数,这里是为了第四步读取XML文件做准备。
第四行使用reader解析XML文件,并将读取的bean定义回调设置到defaultListableBeanFactory当中。其实回调这一步就相当于我们上述的注册这一步。
这个时候defaultListableBeanFactory已经被正确初始化了,我们已经可以使用它的一些方法了,比如上面所使用的获取bean个数,以及获得一个bean实例的方法。
但是我相信真正的开发当中,没有人会采用这样的方式去创造一个bean工厂,我们可以有更简单的方式。上面的四步,我们肯定希望一步就可以完成它。是的,这不是在做梦,就像下面这样。

[java] view
plaincopy

package com.springframework.beans.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestApplicationContext {

public static void main(String[] args) {

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:beans.xml");

System.out.println("numbers: " + applicationContext.getBeanDefinitionCount());

((Person)applicationContext.getBean("person")).work();

}

}

接下来就是见证奇迹的时刻了,输出结果如下图。



我们果然一步就完成了上面四步所做的事情。而且仔细看会发现日志信息当中,第二次采用FileSystemXmlApplicationContext时,日志信息多了许多,分别在上面的前面多了两行,后面多了两行,这说明别看我们是一步,但其实这里比上面做了更多的事情。
具体我们在new一个FileSystemXmlApplicationContext对象的时候,spring到底做了哪些事情呢,这个自然要去跟随源码去看个究竟。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: