Spring IOC容器的设计

IOC相关接口类实现和继承关系图:

1.从接口BeanFactory到HierarchicalBeanFactory,再到ConfigurableBeanFactory,是一条主要的BeanFactory设计途径。在这条设计途径中,Beantory接口定义了基本的IoC容器的规范。在这个接口定义中,包括了getBean()这样的IoC容器的基本方法(通过这个方法可以从容器中取得bean)。而HierarchicalBeanFactory接口继承了Beantory的基本接口之后,增加了

	/**
	 * Return the parent bean factory, or {@code null} if there is none.
     *  返回父类工厂,或者如果没有返回null
	 */
	BeanFactory getParentBeanFactory();

此接口功能,使得BeanFactory具备了双亲IoC容器的管理功能。在接下来的ConfigurableBeanFactory接口中,主要定义了一些对BeanFactory的配置功能,比如可以通过

	/**
	 * Set the parent of this bean factory.
     * 设置父类的bean 工厂
	 * <p>Note that the parent cannot be changed: It should only be set outside
	 * a constructor if it isn't available at the time of factory instantiation.
     * 请注意的是,父类不能改变,如果工厂不可以进行实例化,则它只能在构造函数外部进行设置
	 * @param parentBeanFactory the parent BeanFactory
	 * @throws IllegalStateException if this factory is already associated with
	 * a parent BeanFactory
	 * @see #getParentBeanFactory()
	 */
	void setParentBeanFactory(BeanFactory parentBeanFactory) throws IllegalStateException;

此方法设置双亲IoC容器, 

通过此方法配置Bean的后续处理器,等。

	/**
	 * Add a new BeanPostProcessor that will get applied to beans created
	 * by this factory. To be invoked during factory configuration.
	 * <p>Note: Post-processors submitted here will be applied in the order of
	 * registration; any ordering semantics expressed through implementing the
	 * {@link org.springframework.core.Ordered} interface will be ignored. Note
	 * that autodetected post-processors (e.g. as beans in an ApplicationContext)
	 * will always be applied after programmatically registered ones.
*添加一个新的BeanPostProcessor,它将应用于该工厂创建的。在工厂配置期间调用。注意:此处提交的后
*处理器将按照注册的顺序应用;通过实现* {@link org.springframework.core.Ordered}接口表示的任何排
*序语义都将被忽略。请注意自动检测到的后处理器(例如,作为ApplicationContext中的bean)将始终以编程
*方式注册后的处理器。
	 * @param beanPostProcessor the post-processor to register
	 */
	void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);

通过这些接口设计得叠加,定义了BeanFactory就是简单IoC容器的基本功能。

2.接口设计主线是,以applicationContext应用上下文接口为核心的接口设计,这里涉及的主要接口设计有,BeanFactory到

ListableBeanFactory再到ApplicationContext,在到WebApplicationContext或者ConfigurableApplicationContext接口,我们常用的应用上下文基本上都是ConfigurableApplicationContext接口或者WebApplicationContext的实现。在这个接口体系中,ListableBeanFactory和HierarchicalBeanFactory两个接口,连接BeanFactory接口定义和ApplicationContext应用上下文的接口定义。在ListableBeanFactory接口中,细化了许多BeanFactory的接口功能等,eg:
// 获取所有bean名称的方法
String[] getBeanDefinitionNames();

对于HierarchicalBeanFactory接口,对于ApplicationContext接口,它通过集成MessageSource、ResourceLoader、ApplicationEventPublisher接口。在BeanFactory简单IoC容器的基础上添加了许多对高级容器的特性的支持。

3.涉及的是主要接口关系,而具体的IoC容器都是在这个接口体系之前进行实现的,eg:DefaultListableBeanFactory,这个基本IoC容器就是实现了ConfigurableBeanFactory,从而成为了一个简单的IoC容器的实现。像其他IoC容器,eg:xmlBeanFactory,都是在DefaultListableBeanFactory的基础上进行的扩展。

// 实现和继承关系
public interface HierarchicalBeanFactory extends BeanFactory {}

public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {}

public interface ConfigurableListableBeanFactory
		extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {}

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
		implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {}

4.这个接口系统是以BeanFactory和ApplicationContext为核心的。而BeanFactory又是IoC的最基本接口,在ApplicationContext的设计中,一方面,可以看到它继承了BeanFactory接口体系同的ListableBeanFactory、AutoWireCapableBeanFactory、HierarchicalBeanFactory等BeanFactory的接口,具备了BeanFactory IoC容器的基本功能;另一方面,通过继承MessageSource、ResourceLoader、ApplicationEventPublisher这些接口,BeanFactory为ApplicationContext赋予了更高级的IoC容器特性。对于ApplicationContext而言,为了在web环境中使用它,还设计了WebApplicationContext接口,而这个接口通过ThemeSource接口来扩充功能。

猜你喜欢

转载自blog.csdn.net/crossroads10/article/details/104741205