In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The content of this article mainly focuses on how to achieve the default tag parsing of Spring. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Enter the parseBeanDefinitions () method
Protected void parseBeanDefinitions (Element root, BeanDefinitionParserDelegate delegate) {if (delegate.isDefaultNamespace (root)) {NodeList nl = root.getChildNodes (); for (int I = 0; I < nl.getLength (); iTunes +) {Node node = nl.item (I); if (node instanceof Element) {Element ele = (Element) node If (delegate.isDefaultNamespace (ele)) {/ / default tag parsing, this time focuses on the analysis of this method parseDefaultElement (ele, delegate) } else {/ / Custom tag parsing delegate.parseCustomElement (ele);} else {delegate.parseCustomElement (root);}}
-enter the parseDefaultElement () method, which is below the parseBeanDefinitions
Private void parseDefaultElement (Element ele, BeanDefinitionParserDelegate delegate) {/ / import tag resolution, can not see if (delegate.nodeNameEquals (ele, IMPORT_ELEMENT)) {importBeanDefinitionResource (ele);} / / alias tag resolution alias tag can see else if (delegate.nodeNameEquals (ele, ALIAS_ELEMENT)) {processAliasRegistration (ele) } / / bean tag, important (see this method) else if (delegate.nodeNameEquals (ele, BEAN_ELEMENT)) {processBeanDefinition (ele, delegate);} / / beans tag, unimportant else if (delegate.nodeNameEquals (ele, NESTED_BEANS_ELEMENT)) {/ / recurse doRegisterBeanDefinitions (ele);}}
-enter the bean tag parsing method
Protected void processBeanDefinition (Element ele, BeanDefinitionParserDelegate delegate) {/ / focus on this method. Parse document and encapsulate it into BeanDefinition BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement (ele); if (bdHolder! = null) {bdHolder = delegate.decorateBeanDefinitionIfRequired (ele, bdHolder) Try {/ / after completing the conversion from document to BeanDefinition object, register the BeanDefinition object with cache / / 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));}
Enter
Public BeanDefinitionHolder parseBeanDefinitionElement (Element ele) {/ / think about why the second parameter of this is passed in null (BeanDefinition type), into the method return parseBeanDefinitionElement (ele, null);}
Open the parseBeanDefinitionElement method
Public BeanDefinitionHolder parseBeanDefinitionElement (Element ele, @ Nullable BeanDefinition containingBean) {/ / get id String id = ele.getAttribute (ID_ATTRIBUTE); / / get alias String nameAttr = ele.getAttribute (NAME_ATTRIBUTE); / / add alias to 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");}} / / check whether beanName repeats if (containingBean = = null) {checkNameUniqueness (beanName, aliases, ele) } / / the core method of tag parsing AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement (ele, beanName, containingBean); if (beanDefinition! = null) {. Don't look at what is not important, waste time} return null;}
Look at the core method 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 {/ / create a GenericBeanDefinition object AbstractBeanDefinition bd = createBeanDefinition (className, parent); / / parse the properties of the bean tag and set the parsed properties to parseBeanDefinitionAttributes (ele, beanName, containingBean, bd) in the BeanDefinition object Bd.setDescription (DomUtils.getChildElementValueByTagName (ele, DESCRIPTION_ELEMENT)); / parse the meta tag parseMetaElements (ele, bd) in bean; / / parse the lookup-method tag in bean, but not parseLookupOverrideSubElements (ele, bd.getMethodOverrides ()) / / parse the replaced-method tag in bean, but not parseReplacedMethodSubElements (ele, bd.getMethodOverrides ()); / / parse the constructor-arg tag in bean, but not parseConstructorArgElements (ele, bd); / / parse the property tag in bean, or parsePropertyElements (ele, bd) / / No parseQualifierElements (ele, bd); bd.setResource (this.readerContext.getResource ()); bd.setSource (extractSource (ele)); return bd;}. If you are interested in watching return null; for yourself.
Open the method parseBeanDefinitionAttributes (), encapsulate the BeanDefinition property by default, and then return the BeanDefinition object
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;}
Then go back to the processBeanDefinition method, register the BeanDefinition in the cache, and see
Protected void processBeanDefinition (Element ele, BeanDefinitionParserDelegate delegate) {/ / focus on this method, degree of importance 5, parse document and encapsulate it into BeanDefinition BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement (ele); if (bdHolder! = null) {/ / the function of this method is not important, focus on design patterns, decorator design patterns, plus SPI design ideas bdHolder = delegate.decorateBeanDefinitionIfRequired (ele, bdHolder) Try {/ / after completing the conversion from document to BeanDefinition object, register the BeanDefinition object with cache / / 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));}
Open the registerBeanDefinition method
Public static void registerBeanDefinition (BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) throws BeanDefinitionStoreException {/ / Register bean definition under primary name. String beanName = definitionHolder.getBeanName (); / / complete the registration of BeanDefinition, focusing on registry.registerBeanDefinition (beanName, definitionHolder.getBeanDefinition ()); / / establish the mapping between aliases and id, so that you can get id / / Register aliases for bean name, if any according to aliases. String [] aliases = definitionHolder.getAliases (); if (aliases! = null) {for (String alias: aliases) {registry.registerAlias (beanName, alias);}
Open the registerBeanDefinition method and save the created beanDefinition into map. Default tag resolution is over.
@ 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) } Thank you for your reading. I believe you have some understanding of "how to implement default tag parsing in Spring". Go to practice quickly. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.