1 16 17 package org.springframework.beans.factory.xml; 18 19 import org.w3c.dom.Element ; 20 21 import org.springframework.beans.factory.BeanDefinitionStoreException; 22 import org.springframework.beans.factory.config.BeanDefinition; 23 import org.springframework.beans.factory.config.BeanDefinitionHolder; 24 import org.springframework.beans.factory.parsing.BeanComponentDefinition; 25 import org.springframework.beans.factory.support.AbstractBeanDefinition; 26 import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; 27 import org.springframework.beans.factory.support.BeanDefinitionRegistry; 28 import org.springframework.util.StringUtils; 29 30 49 public abstract class AbstractBeanDefinitionParser implements BeanDefinitionParser { 50 51 52 public static final String ID_ATTRIBUTE = "id"; 53 54 55 public final BeanDefinition parse(Element element, ParserContext parserContext) { 56 AbstractBeanDefinition definition = parseInternal(element, parserContext); 57 if (!parserContext.isNested()) { 58 try { 59 String id = resolveId(element, definition, parserContext); 60 if (!StringUtils.hasText(id)) { 61 parserContext.getReaderContext().error( 62 "Id is required for element '" + element.getLocalName() + "' when used as a top-level tag", element); 63 } 64 BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, id); 65 registerBeanDefinition(holder, parserContext.getRegistry()); 66 if (shouldFireEvents()) { 67 BeanComponentDefinition componentDefinition = new BeanComponentDefinition(holder); 68 postProcessComponentDefinition(componentDefinition); 69 parserContext.registerComponent(componentDefinition); 70 } 71 } 72 catch (BeanDefinitionStoreException ex) { 73 parserContext.getReaderContext().error(ex.getMessage(), element); 74 return null; 75 } 76 } 77 return definition; 78 } 79 80 93 protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) 94 throws BeanDefinitionStoreException { 95 96 if (shouldGenerateId()) { 97 return parserContext.getReaderContext().generateBeanName(definition); 98 } 99 else { 100 String id = element.getAttribute(ID_ATTRIBUTE); 101 if (!StringUtils.hasText(id) && shouldGenerateIdAsFallback()) { 102 id = parserContext.getReaderContext().generateBeanName(definition); 103 } 104 return id; 105 } 106 } 107 108 122 protected void registerBeanDefinition(BeanDefinitionHolder definition, BeanDefinitionRegistry registry) { 123 BeanDefinitionReaderUtils.registerBeanDefinition(definition, registry); 124 } 125 126 127 137 protected abstract AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext); 138 139 146 protected boolean shouldGenerateId() { 147 return false; 148 } 149 150 158 protected boolean shouldGenerateIdAsFallback() { 159 return false; 160 } 161 162 174 protected boolean shouldFireEvents() { 175 return true; 176 } 177 178 188 protected void postProcessComponentDefinition(BeanComponentDefinition componentDefinition) { 189 } 190 191 } 192 | Popular Tags |