Spring实现默认标签解析流程

网友投稿 577 2022-11-08

Spring实现默认标签解析流程

Spring实现默认标签解析流程

本文实例为大家分享了Spring实现默认标签解析流程的具体代码,供大家参考,具体内容如下

承接上文,进入parseBeanDefinitions()方法

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);

}

}

-进入parseDefaultElement()方法,此方法在parseBeanDefinitions的下方

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(ele);

}

}

-进入到bean标签解析方法

protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {

//重点看这个方法 ,解析document,封装成BeanDefinition

BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);

if (bdHolder != null) {

bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);

try {

//完成document到BeanDefinition对象转换后,对BeanDefinition对象进行缓存注册

// 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));

}

}

进入

public BeanDefinitionHolder parseBeanDefinitionElement(Element ele) {

//思考一下这个的第二个参数为啥要传入null(BeanDefinition类型),进入方法

return parseBeanDefinitionElement(ele, null);

}

打开parseBeanDefinitionElement方法

public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) {

//获取id

String id = ele.getAttribute(ID_ATTRIBUTE);

//获取别名

String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);

//将别名添加到aliases内

List aliases = new ArrayList<>();

if (StringUtils.hasLength(nameAttr)) {

String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);

aliases.addAll(Arrays.asList(nameArr));

}

String beanName = id;

if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {

beanName = aliases.remove(0);

if (logger.isTraceEnabled()) {

logger.trace("No XML 'id' specified - using '" + beanName +

"' as bean name and " + aliases + " as aliases");

}

}

//检查beanName是否重复

if (containingBean == null) {

checkNameUniqueness(beanName, aliases, ele);

}

//标签解析的核心方法

AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);

if (beanDefinition != null) {

......不重要的就不要看了,浪费时间

}

return null;

}

看核心方法parseBeanDefinitionElement

public AbstractBeanDefinition parseBeanDefinitionElement(

Element ele, String beanName, @Nullable BeanDefinition containingBean) {

this.parseState.push(new BeanEntry(beanName));

String className = null;

if (ele.hasAttribute(CLASS_ATTRIBUTE)) {

className = ele.getAttribute(CLASS_ATTRIBUTE).trim();

}

String parent = null;

if (ele.hasAttribute(PARENT_ATTRIBUTE)) {

parent = ele.getAttribute(PARENT_ATTRIBUTE);

}

try {

//创建GenericBeanDefinition对象

AbstractBeanDefinition bd = createBeanDefinition(className, parent);

//解析bean标签的属性,并把解析出来的属性设置到BeanDefinition对象中

parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);

bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));

//解析bean中的meta标签

parseMetaElements(ele, bd);

//解析bean中的lookup-method标签 ,可看可不看

parseLookupOverrideSubElements(ele, bd.getMethodOverrides());

//解析bean中的replaced-method标签 ,可看可不看

parseReplacedMethodSubElements(ele, bd.getMethodOverrides());

//解析bean中的constructor-arg标签 ,可看可不看

parseConstructorArgElements(ele, bd);

//解析bean中的property标签 可看可不看

parsePropertyElements(ele, bd);

//可以不看,用不到

parseQualifierElements(ele, bd);

bd.setResource(this.readerContext.getResource());

bd.setSource(extractSource(ele));

return bd;

}

......有兴趣自己看

return null;

}

打开这个方法parseBeanDefinitionAttributes(),对BeanDefinition属性进行默认的封装,然后返回BeanDefinition对象

public AbstractBeanDefinition parseBeanDefinitionAttributes(Element ele, String beanName,

@Nullable BeanDefinition containingBean, AbstractBeanDefinition bd) {

//

if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {

error("Old 1.x 'singleton' attribute in use - upgrade to 'scope' declaration", ele);

}

else if (ele.hasAttribute(SCOPE_ATTRIBUTE)) {

bd.setScope(ele.getAttribute(SCOPE_ATTRIBUTE));

}

else if (containingBean != null) {

// Take default from containing bean in case of an inner bean definition.

bd.setScope(containingBean.getScope());

}

if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) {

bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE)));

}

String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);

if (isDefaultValue(lazyInit)) {

lazyInit = this.defaults.getLazyInit();

}

bd.setLazyInit(TRUE_VALUE.equals(lazyInit));

String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE);

bd.setAutowireMode(getAutowireMode(autowire));

if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {

String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE);

bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS));

}

String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE);

if (isDefaultValue(autowireCandidate)) {

String candidatePattern = this.defaults.getAutowireCandidates();

if (candidatePattern != null) {

String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern);

bd.setAutowireCandidate(PatternMatchUtils.simpleMatch(patterns, beanName));

}

}

else {

bd.setAutowireCandidate(TRUE_VALUE.equals(autowireCandidate));

}

if (ele.hasAttribute(PRIMARY_ATTRIBUTE)) {

bd.setPrimary(TRUE_VALUE.equals(ele.getAttribute(PRIMARY_ATTRIBUTE)));

}

if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) {

String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE);

bd.setInitMethodName(initMethodName);

}

else if (this.defaults.getInitMethod() != null) {

bd.setInitMethodName(this.defaults.getInitMethod());

bd.setEnforceInitMethod(false);

}

if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {

String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE);

bd.setDestroyMethodName(destroyMethodName);

}

else if (this.defaults.getDestroyMethod() != null) {

bd.setDestroyMethodName(this.defaults.getDestroyMethod());

bd.setEnforceDestroyMethod(false);

}

if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {

bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE));

}

if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) {

bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE));

}

return bd;

}

然后回到processBeanDefinition方法,将BeanDefinition注册到缓存中,此时看

protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {

//重点看这个方法,重要程度 5 ,解析document,封装成BeanDefinition

BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);

if (bdHolder != null) {

//该方法功能不重要,设计模式重点看一下,装饰者设计模式,加上SPI设计思想

bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);

try {

//完成document到BeanDefinition对象转换后,对BeanDefinition对象进行缓存注册

// 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().fireComponenthttp://Registered(new BeanComponentDefinition(bdHolder));

}

}

打开registerBeanDefinition方法

public static void registerBeanDefinition(

BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)

throws BeanDefinitionStoreException {

// Register bean definition under primary name.

String beanName = definitionHolder.getBeanName();

//完成BeanDefinition的注册,重点看

registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

//建立别名和 id的映射,这样就可以根据别名获取到id

// Register aliases for bean name, if any.

String[] aliases = definitionHolder.getAliases();

if (aliases != null) {

for (String alias : aliases) {

registry.registerAlias(beanName, alias);

}

}

}

打开registerBeanDefinition方法,将创建好的beanDefinition存入map。默认标签解析就此完毕

@Override

public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)

throws BeanDefinitionStoreException {

Assert.hasText(beanName, "'beanName' must not be empty");

Assert.notNull(beanDefinition, "BeanDefinition must not be null");

this.beanDefinitionMap.put(beanName, beanDefinition);

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:C++---string类的使用和模拟实现
下一篇:带你造轮子,自定义一个随意拖拽可吸边的View
相关文章

 发表评论

暂时没有评论,来抢沙发吧~