BeanFactory
定义了IoC容器的基本功能规范
package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
public interface BeanFactory {
//转义字符,用于获取BeanFactory本身
String FACTORY_BEAN_PREFIX = "&";
//根据名字获取bean
Object getBean(String name) throws BeansException;
//根据名字获取bean(带筛选,指定返回类型)
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
//根据指定类型获取bean
<T> T getBean(Class<T> requiredType) throws BeansException;
//根据名字获取bean,带多个参数
Object getBean(String name, Object... args) throws BeansException;
//根据指定类型获取bean(带筛选,多个参数)
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
//判断容器是否包含指定名字的bean
boolean containsBean(String name);
//判断指定名字的bean是否为单例的
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
//查询指定名字的bean是否是Prototype类型的
boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
//判断指定名字的bean是否是特定类型的
boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
//判断指定名字的bean是否是特定类型的
boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
//获得指定名字的bean的class类型
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
//获取指定名字的bean的所有别名
String[] getAliases(String name);
}
注意:getAliases,isPrototype,isSingleton方法都是对bean的属性进行判断或获取,bean的这些属性都是用户在BeanDefinition中定义的。
XmlBeanFactory
以XML格式定义了BeanDefinition,提供了最基本的IoC容器的功能。它本身未实现具体的IoC容器的功能,而是由它的继承体系实现的。它本身定义了XmlBeanDefinitionReader 对象通过Xml文件定义BeanDefinition。
继承体系
package org.springframework.beans.factory.xml;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.Resource;
@Deprecated
@SuppressWarnings({"serial", "all"})
public class XmlBeanFactory extends DefaultListableBeanFactory {
private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
//构造器
public XmlBeanFactory(Resource resource) throws BeansException {
this(resource, null);
}
//构造器,加载Definitions
public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
super(parentBeanFactory);
this.reader.loadBeanDefinitions(resource);
}
}
BeanDefinition
BeanDefinition的Resource定位