BeanFactory基础容器的设计原理(四)

本文探讨BeanFactory接口作为Spring IOC容器的基础,以XmlBeanFactory为例,解释简单IOC容器的设计原理。通过构造方法和Resource对象加载BeanDefinition,初始化过程涉及XmlBeanDefinitionReader的使用。同时,文章分析了自动装配接口忽略的机制。
摘要由CSDN通过智能技术生成

BeanFactory接口提供了使用IOC容器的规范,在这个基础上,Spring还提供了符合这个IOC容器接口的设计原理。下面我们就XmlBeanFactory的实现为例说明简单IOC容器的设计原理。
XmlBeanFactory
下面我们就使用XmlBeanFactory的代码实现讲解:
XmlBeanFactory的功能是建立在 DefaultListableBeanFactory这个基本容器基础上的,并在这个基本容器的基础上实现了其他诸如XML读取的附加功能,对于这些功能的实现原理我们看一看XmlBeanFactory的代码实现就很容易理解了。

@Deprecated
@SuppressWarnings({"serial", "all"})
public class XmlBeanFactory extends DefaultListableBeanFactory {

	private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);


	/**
	 * Create a new XmlBeanFactory with the given resource,
	 * which must be parsable using DOM.
	 * @param resource the XML resource to load bean definitions from
	 * @throws BeansException in case of loading or parsing errors
	 */
	public XmlBeanFactory(Resource resource) throws BeansException {
		this(resource, null);
	}

	/**
	 * Create a new XmlBeanFactory with the given input stream,
	 * which must be parsable using DOM.
	 * @param resource the XML resource to load bean definitions from
	 * @param parentBeanFactory parent bean factory
	 * @throws BeansException in case of loading or parsing errors
	 */
	public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
		super(parentBeanFactory);
		this.reader.loadBeanDefinitions(resource);
	}

}

XmlBeanFactory从注解上看已经标记为一个过时的类,但这里不影响我们对基础容器的设计分析。XmlBeanFactory构造方法中需要得到Resource对象,通过XmlBeanDefinitionReader对象的loadBeanDefinitions的方法调用载入BenDefinition,在XML配置文件模式下,这是IOC初始化的一个重要组成部分。当我们执行以下代码的时候,一个基本的XmlBeanFactory的IOC容器就被建立了。

XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));

以上代码实现的时序图为:
时序图
这里我们可以看到,首先我们在定义一个XMLBeanFactory时,这边需要传入一个Resource资源对象,具体的资源对象的处理方式,我们这里不做讨论详情原理介绍可参照Spring 统一资源加载策略。Resource相关的类完成了对配置文件进行封装后配置文件的读取工作就交给了XmlBeanDefinitionReader来处理了。XmlBeanFactory的初始化方法有很多,Spring中提供了很多的构造函数,这里我们分析的是使用Resource实例作为构造参数的方法,代码如下:

	/**
	 * Create a new XmlBeanFactory with the given resource,
	 * which must be parsable using DOM.
	 * @param resource the XML resource to load bean definitions from
	 * @throws BeansException in case of loading or parsing errors
	 */
	public XmlBeanFactory(Resource resource) throws BeansException {
		this(resource, null);
	}

构造函数在内部再次调用内部构造函数:

	/**
	 * Create a new XmlBeanFactory with the given input stream,
	 * which must be parsable using DOM.
	 * @param resource the XML resource to load bean definitions from
	 * @param parentBeanFactory parent bean factory
	 * @throws BeansException in case of loading or parsing errors
	 */
	public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
		super(parentBeanFactory);
		this.reader.loadBeanDefinitions(resource);
	}

上面代码中执行this.reader.loadBeanDefinitions(resource)是资源解析的真正实现。在时序图中我们可以看到XmlBeanDefinitionreader
加载数据就是在这里完成的,但在XmlBeanDefinitionReader加载数据前还有一个父类构造器的调用过程:super(parentBeanFactory),跟踪代码到父类AbstractAutowireCapableBeanFactory中调用了父类的其中一个构造器:

	/**
	 * Create a new AbstractAutowireCapableBeanFactory.
	 */
	public AbstractAutowireCapableBeanFactory() {
		super();
		ignoreDependencyInterface(BeanNameAware.class);
		ignoreDependencyInterface(BeanFactoryAware.class);
		ignoreDependencyInterface(BeanClassLoaderAware.class);
	}

方法ignoreDependencyInterface(Class<?> ifc)的主要功能是忽略给定接口的自动装配功能,Spring中是这样阐述的:

ignore the given dependency interface for autowiring.This will typically be used by application contexts to register dependencies that are resolved in other ways, like BeanFactory through BeanFactoryAware or ApplicationContext through ApplicationContextAware.By default, only the BeanFactoryAware interface is ignored. For further types to ignore, invoke this method for each type.

在自动装配的时候忽略给定的依赖接口,这些指定忽略的自动装配的接口,将通过其他的方式依赖注入,像ApplicationContext通过ApplicationContextAware进行注入或者BeanFactory通过BeanFactoryAware进行注入。
微信公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值