Spring Bean生命周期-注册Bean(doRegisterBeanDefinitions)(四)
ApplicationContext在启动的时候会首先刷新beanFactory,刷新的时候如果有之前的Bean首先销毁之前存在的Bean,然后再从我们的配置文件中注册新的Bean。
Spring会使用BeanDefinitionDocumentReader来注册,默认使用DefaultBeanDefinitionDocumentReader来doRegisterBeanDefinitions。这里我们看看注册过程到底都做了那些事。
/**
* Register each bean definition within the given root {@code <beans/>} element.
* 从<beans />配置中注册每一个bean,如果有嵌套的beans,那么递归执行这个方法。
*
*/
protected void doRegisterBeanDefinitions(Element root) {
// Any nested <beans> elements will cause recursion in this method. In
// order to propagate and preserve <beans> default-* attributes correctly,
// keep track of the current (parent) delegate, which may be null. Create
// the new (child) delegate with a reference to the parent for fallback purposes,
// then ultimately reset this.delegate back to its original (parent) reference.
// this behavior emulates a stack of delegates without actually necessitating one.
// 在递归的时候,跟踪父级delegate,新的递归调用引用上个方法的delegate
BeanDefinitionParserDelegate parent = this.delegate;
this.delegate = createDelegate(getReaderContext(), root, parent);
if (this.delegate.isDefaultNamespace(root)) {
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
return;
}
}
}
preProcessXml(root);
parseBeanDefinitions(root, this.delegate);
postProcessXml(root);
this.delegate = parent;
}
代码分析
- 创建delegate,创建代理,然后代理首先初始化一些默认的属性,DocumentDefaultsDefinition是存储默认配置的对象
DEFAULT_LAZY_INIT_ATTRIBUTE = "default-lazy-init";
DEFAULT_MERGE_ATTRIBUTE = "default-merge";
DEFAULT_AUTOWIRE_ATTRIBUTE = "default-autowire";
DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE = "default-dependency-check";
DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE = "default-autowire-candidates";
DEFAULT_INIT_METHOD_ATTRIBUTE = "default-init-method";
DEFAULT_DESTROY_METHOD_ATTRIBUTE = "default-destroy-method"
protected BeanDefinitionParserDelegate createDelegate(
XmlReaderContext readerContext, Element root, BeanDefinitionParserDelegate parentDelegate) {
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
// 设置默认的属性
delegate.initDefaults(root, parentDelegate);
return delegate;
}
处理Bean的profile,是否支持,如果不接受的话直接返回,接受不做处理
解析beanDefinitions,解析之前的preProcessXml,postProcessXml方法都为空,方便我们自定义一些扩展。只看解析部分
// 解析Bean的配置,也可以解析自定义的元素标签。
// 一般情况下执行parseDefaultElement解析默认的元素
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
if (delegate.isDefaultNamespace(root)) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate);
}
else {
delegate.parseCustomElement(ele);
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
- 解析默认的元素标签,如<beans/>,<bean />,<import />,<alias />,每一种标签都有对应的方法。
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
// 标签为<import />
if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
importBeanDefinitionResource(ele);
}
// 标签为<alias />
else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
processAliasRegistration(ele);
}
// 标签为<bean />
else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
processBeanDefinition(ele, delegate);
}
// 标签为<beans />
else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
// recurse
// 递归仍然执行最初的doRegisterBeanDefinitions方法
doRegisterBeanDefinitions(ele);
}
}
- 其它标签的我们先暂不处理,只看如何解析<bean />标签
- 通过BeanDefinitionParserDelegate进行解析,返回beanHolder
- BeanDefinitionParserDelegate装饰beanHolder
- 注册Bean
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
if (bdHolder != null) {
bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
try {
// Register the final decorated instance.
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error("Failed to register bean definition with name '" +
bdHolder.getBeanName() + "'", ele, ex);
}
// Send registration event.
getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
}
}
- 代理解析Bean的流程,如果解析的过程发生错误,返回null.
public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, BeanDefinition containingBean) {
// 获取id
String id = ele.getAttribute(ID_ATTRIBUTE);
// 获取别名,name属性逗号分隔,
String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);
List<String> aliases = new ArrayList<String>();
if (StringUtils.hasLength(nameAttr)) {
String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);
aliases.addAll(Arrays.asList(nameArr));
}
//如果没有设置id,那么name属性作为id
String beanName = id;
if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {
beanName = aliases.remove(0);
if (logger.isDebugEnabled()) {
logger.debug("No XML 'id' specified - using '" + beanName +
"' as bean name and " + aliases + " as aliases");
}
}
//判断名字是否有重复,通过HashSet判断
if (containingBean == null) {
checkNameUniqueness(beanName, aliases, ele);
}
// 真正解析,内部处理<bean />的所有相关的配置"parent","class","abstract","scope","singleton","lazy-init",等,然后存储在BeanDifinition中,GenericBeanDefinition。
AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);
if (beanDefinition != null) {
if (!StringUtils.hasText(beanName)) {
try {
if (containingBean != null) {
// 生成Bean的名称
beanName = BeanDefinitionReaderUtils.generateBeanName(
beanDefinition, this.readerContext.getRegistry(), true);
}
else {
beanName = this.readerContext.generateBeanName(beanDefinition);
// Register an alias for the plain bean class name, if still possible,
// if the generator returned the class name plus a suffix.
// This is expected for Spring 1.2/2.0 backwards compatibility.
String beanClassName = beanDefinition.getBeanClassName();
if (beanClassName != null &&
beanName.startsWith(beanClassName) && beanName.length() > beanClassName.length() &&
!this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) {
aliases.add(beanClassName);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Neither XML 'id' nor 'name' specified - " +
"using generated bean name [" + beanName + "]");
}
}
catch (Exception ex) {
error(ex.getMessage(), ele);
return null;
}
}
String[] aliasesArray = StringUtils.toStringArray(aliases);
// 创建BeanDefinitionHolder
return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray);
}
return null;
}
- delegate装饰BeanDefinitionHolder,然后注册BeanDfinition
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
// 记得ReaderContext的registry是最初创建的DefaultListableBeanFactory
public static void registerBeanDefinition(
BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {
// Register bean definition under primary name.
// 注册到registry中,beanDefinitionMap
String beanName = definitionHolder.getBeanName();
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
// Register aliases for bean name, if any.
//如果别名不为null,也注册下别名
String[] aliases = definitionHolder.getAliases();
if (aliases != null) {
for (String alias : aliases) {
registry.registerAlias(beanName, alias);
}
}
}
- 发布注册完成事件
回顾
注册过程,主要是解析配置中的各种属性,最终存放在BeanFactory中的beanDifnitionMap中。大过程描述起来简单,但细节还有很多。
TODO
- BeanDefinitionParserDelegate
结束
希望对大家有帮助。