public class BeanDefinitionParserDelegate { public static final String BEANS_NAMESPACE_URI = "http://www.springframework.org/schema/beans"; public static final String MULTI_VALUE_ATTRIBUTE_DELIMITERS = ",; "; /** @deprecated as of Spring 3.1 in favor of {@link #MULTI_VALUE_ATTRIBUTE_DELIMITERS} */ public static final String BEAN_NAME_DELIMITERS = MULTI_VALUE_ATTRIBUTE_DELIMITERS; public static final String TRUE_VALUE = "true"; public static final String FALSE_VALUE = "false"; public static final String DEFAULT_VALUE = "default"; public static final String DESCRIPTION_ELEMENT = "description"; public static final String AUTOWIRE_NO_VALUE = "no"; public static final String AUTOWIRE_BY_NAME_VALUE = "byName"; public static final String AUTOWIRE_BY_TYPE_VALUE = "byType"; public static final String AUTOWIRE_CONSTRUCTOR_VALUE = "constructor"; public static final String AUTOWIRE_AUTODETECT_VALUE = "autodetect"; public static final String DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE = "all"; public static final String DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE = "simple"; public static final String DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE = "objects"; public static final String NAME_ATTRIBUTE = "name"; public static final String BEAN_ELEMENT = "bean"; public static final String META_ELEMENT = "meta"; public static final String ID_ATTRIBUTE = "id"; public static final String PARENT_ATTRIBUTE = "parent"; public static final String CLASS_ATTRIBUTE = "class"; public static final String ABSTRACT_ATTRIBUTE = "abstract"; public static final String SCOPE_ATTRIBUTE = "scope"; public static final String SINGLETON_ATTRIBUTE = "singleton"; public static final String LAZY_INIT_ATTRIBUTE = "lazy-init"; public static final String AUTOWIRE_ATTRIBUTE = "autowire"; public static final String AUTOWIRE_CANDIDATE_ATTRIBUTE = "autowire-candidate"; public static final String PRIMARY_ATTRIBUTE = "primary"; public static final String DEPENDENCY_CHECK_ATTRIBUTE = "dependency-check"; public static final String DEPENDS_ON_ATTRIBUTE = "depends-on"; public static final String INIT_METHOD_ATTRIBUTE = "init-method"; public static final String DESTROY_METHOD_ATTRIBUTE = "destroy-method"; public static final String FACTORY_METHOD_ATTRIBUTE = "factory-method"; public static final String FACTORY_BEAN_ATTRIBUTE = "factory-bean"; public static final String CONSTRUCTOR_ARG_ELEMENT = "constructor-arg"; public static final String INDEX_ATTRIBUTE = "index"; public static final String TYPE_ATTRIBUTE = "type"; public static final String VALUE_TYPE_ATTRIBUTE = "value-type"; public static final String KEY_TYPE_ATTRIBUTE = "key-type"; public static final String PROPERTY_ELEMENT = "property"; public static final String REF_ATTRIBUTE = "ref"; public static final String VALUE_ATTRIBUTE = "value"; public static final String LOOKUP_METHOD_ELEMENT = "lookup-method"; public static final String REPLACED_METHOD_ELEMENT = "replaced-method"; public static final String REPLACER_ATTRIBUTE = "replacer"; public static final String ARG_TYPE_ELEMENT = "arg-type"; public static final String ARG_TYPE_MATCH_ATTRIBUTE = "match"; public static final String REF_ELEMENT = "ref"; public static final String IDREF_ELEMENT = "idref"; public static final String BEAN_REF_ATTRIBUTE = "bean"; public static final String LOCAL_REF_ATTRIBUTE = "local"; public static final String PARENT_REF_ATTRIBUTE = "parent"; public static final String VALUE_ELEMENT = "value"; public static final String NULL_ELEMENT = "null"; public static final String ARRAY_ELEMENT = "array"; public static final String LIST_ELEMENT = "list"; public static final String SET_ELEMENT = "set"; public static final String MAP_ELEMENT = "map"; public static final String ENTRY_ELEMENT = "entry"; public static final String KEY_ELEMENT = "key"; public static final String KEY_ATTRIBUTE = "key"; public static final String KEY_REF_ATTRIBUTE = "key-ref"; public static final String VALUE_REF_ATTRIBUTE = "value-ref"; public static final String PROPS_ELEMENT = "props"; public static final String PROP_ELEMENT = "prop"; public static final String MERGE_ATTRIBUTE = "merge"; public static final String QUALIFIER_ELEMENT = "qualifier"; public static final String QUALIFIER_ATTRIBUTE_ELEMENT = "attribute"; public static final String DEFAULT_LAZY_INIT_ATTRIBUTE = "default-lazy-init"; public static final String DEFAULT_MERGE_ATTRIBUTE = "default-merge"; public static final String DEFAULT_AUTOWIRE_ATTRIBUTE = "default-autowire"; public static final String DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE = "default-dependency-check"; public static final String DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE = "default-autowire-candidates"; public static final String DEFAULT_INIT_METHOD_ATTRIBUTE = "default-init-method"; public static final String DEFAULT_DESTROY_METHOD_ATTRIBUTE = "default-destroy-method"; protected final Log logger = LogFactory.getLog(getClass()); private final XmlReaderContext readerContext; private final DocumentDefaultsDefinition defaults = new DocumentDefaultsDefinition(); private final ParseState parseState = new ParseState(); private Environment environment; /** *缓存所有已经解析的bean的id以及别名 */ private final Set<String> usedNames = new HashSet<String>(); /** * 使用给定的XmlReaderContext和Environment创建一个BeanDefinitionParserDelegate实例 */ public BeanDefinitionParserDelegate(XmlReaderContext readerContext, Environment environment) { Assert.notNull(readerContext, "XmlReaderContext must not be null"); Assert.notNull(readerContext, "Environment must not be null"); this.readerContext = readerContext; this.environment = environment; } @Deprecated public BeanDefinitionParserDelegate(XmlReaderContext readerContext) { this(readerContext, new StandardEnvironment()); } public final XmlReaderContext getReaderContext() { return this.readerContext; } public final Environment getEnvironment() { return this.environment; } protected Object extractSource(Element ele) { return this.readerContext.extractSource(ele); } protected void error(String message, Node source) { this.readerContext.error(message, source, this.parseState.snapshot()); } protected void error(String message, Element source) { this.readerContext.error(message, source, this.parseState.snapshot()); } protected void error(String message, Element source, Throwable cause) { this.readerContext.error(message, source, this.parseState.snapshot(), cause); } /** *初始化默认值 */ public void initDefaults(Element root, BeanDefinitionParserDelegate parent) { populateDefaults(this.defaults, (parent != null ? parent.defaults : null), root); this.readerContext.fireDefaultsRegistered(this.defaults); } /** * 初始化默认值 */ public void initDefaults(Element root) { initDefaults(root, null); } /** * 设置defualt数据, */ protected void populateDefaults(DocumentDefaultsDefinition defaults, DocumentDefaultsDefinition parentDefaults, Element root) { //从根节点即<beans>节点中获取default-lazy-init参数 String lazyInit = root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE); //如果lazyInit的值为default,如果parentDefaults不为null则侧其中获取lazyInit,否则设置为false if (DEFAULT_VALUE.equals(lazyInit)) { lazyInit = parentDefaults != null ? parentDefaults.getLazyInit() : FALSE_VALUE; } defaults.setLazyInit(lazyInit); //获取default-merge属性 String merge = root.getAttribute(DEFAULT_MERGE_ATTRIBUTE); if (DEFAULT_VALUE.equals(merge)) { merge = parentDefaults != null ? parentDefaults.getMerge() : FALSE_VALUE; } defaults.setMerge(merge); //获取default-autowire属性 String autowire = root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE); if (DEFAULT_VALUE.equals(autowire)) { autowire = parentDefaults != null ? parentDefaults.getAutowire() : AUTOWIRE_NO_VALUE; } defaults.setAutowire(autowire); //设置defult-dependecny-check defaults.setDependencyCheck(root.getAttribute(DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE)); if (root.hasAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE)) { defaults.setAutowireCandidates(root.getAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE)); } else if (parentDefaults != null) { defaults.setAutowireCandidates(parentDefaults.getAutowireCandidates()); } if (root.hasAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)) { defaults.setInitMethod(root.getAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)); } else if (parentDefaults != null) { defaults.setInitMethod(parentDefaults.getInitMethod()); } if (root.hasAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)) { defaults.setDestroyMethod(root.getAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)); } else if (parentDefaults != null) { defaults.setDestroyMethod(parentDefaults.getDestroyMethod()); } defaults.setSource(this.readerContext.extractSource(root)); } /** * 获取默认值 */ public DocumentDefaultsDefinition getDefaults() { return this.defaults; } public BeanDefinitionDefaults getBeanDefinitionDefaults() { BeanDefinitionDefaults bdd = new BeanDefinitionDefaults(); bdd.setLazyInit("TRUE".equalsIgnoreCase(this.defaults.getLazyInit())); bdd.setDependencyCheck(this.getDependencyCheck(DEFAULT_VALUE)); bdd.setAutowireMode(this.getAutowireMode(DEFAULT_VALUE)); bdd.setInitMethodName(this.defaults.getInitMethod()); bdd.setDestroyMethodName(this.defaults.getDestroyMethod()); return bdd; } public String[] getAutowireCandidatePatterns() { String candidatePattern = this.defaults.getAutowireCandidates(); return (candidatePattern != null ? StringUtils.commaDelimitedListToStringArray(candidatePattern) : null); } /** * 解析<bean>节点,如果在解析过程中发生异常可能返回null */ public BeanDefinitionHolder parseBeanDefinitionElement(Element ele) { return parseBeanDefinitionElement(ele, null); } /** */ public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, BeanDefinition containingBean) { //获取<bean>节点的id属性,如<bean id="man" ... />,则id=man String id = ele.getAttribute(ID_ATTRIBUTE); //获取name属性值 String nameAttr = ele.getAttribute(NAME_ATTRIBUTE); List<String> aliases = new ArrayList<String>(); //如果name属性值不为空,拆分多个name,并保存到list中 if (StringUtils.hasLength(nameAttr)) { String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS); aliases.addAll(Arrays.asList(nameArr)); } //将id赋值给beanName String beanName = id; //如果beanName不为空且别名的列表不为空则将别名集合中的第一个值赋值为beanName 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"); } } //beanName的唯一性检查,beanName和别名都不能和已有的bean id或别名重复 if (containingBean == null) { checkNameUniqueness(beanName, aliases, ele); } // 如果当前bean 的id和别名都没有重复值则解析bean节点 AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean); if (beanDefinition != null) { if (!StringUtils.hasText(beanName)) { try { if (containingBean != null) { 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); return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray); } return null; } /** * 校验制定的bean的名称和别名在当前级别的beans嵌套内没有使用过 */ protected void checkNameUniqueness(String beanName, List<String> aliases, Element beanElement) { //定义一个找到的beanName,初始化值为null String foundName = null; //beanName不为空且userNames中包含这个beanName则将beanName赋值为foundName if (StringUtils.hasText(beanName) && this.usedNames.contains(beanName)) { foundName = beanName; } //如果foundName不为null,则判断usedNames中是否包含别名,将包含的第一个别名赋值给foundName if (foundName == null) { foundName = (String) CollectionUtils.findFirstMatch(this.usedNames, aliases); } //如果foundName不为null则进行错误处理 if (foundName != null) { error("Bean name '" + foundName + "' is already used in this <beans> element", beanElement); } //向usedNames中添加beanName this.usedNames.add(beanName); //将aliases添加到usedNames this.usedNames.addAll(aliases); } /** * 解析Bean */ public AbstractBeanDefinition parseBeanDefinitionElement( Element ele, String beanName, BeanDefinition containingBean) { //使用beanName创建一个BeanEntriy对象并放入parseState中 this.parseState.push(new BeanEntry(beanName)); String className = null; //如果bean节点中含有class属性,则将该获取该属性值 if (ele.hasAttribute(CLASS_ATTRIBUTE)) { className = ele.getAttribute(CLASS_ATTRIBUTE).trim(); } try { // 如果bean节点含有parent属性则获取该属性 String parent = null; if (ele.hasAttribute(PARENT_ATTRIBUTE)) { parent = ele.getAttribute(PARENT_ATTRIBUTE); } //使用className和parent创建一个beanDefinition AbstractBeanDefinition bd = createBeanDefinition(className, parent); parseBeanDefinitionAttributes(ele, beanName, containingBean, bd); //设置description bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT)); parseMetaElements(ele, bd); //解析lookup-method元素 parseLookupOverrideSubElements(ele, bd.getMethodOverrides()); //解析 replace-method元素 parseReplacedMethodSubElements(ele, bd.getMethodOverrides()); //解析构造参数元素 parseConstructorArgElements(ele, bd); //解析属性 parsePropertyElements(ele, bd); parseQualifierElements(ele, bd); bd.setResource(this.readerContext.getResource()); bd.setSource(extractSource(ele)); return bd; } catch (ClassNotFoundException ex) { error("Bean class [" + className + "] not found", ele, ex); } catch (NoClassDefFoundError err) { error("Class that bean class [" + className + "] depends on not found", ele, err); } catch (Throwable ex) { error("Unexpected failure during bean definition parsing", ele, ex); } finally { this.parseState.pop(); } return null; } /** * 解析给定的节点的属性值 * */ public AbstractBeanDefinition parseBeanDefinitionAttributes(Element ele, String beanName, BeanDefinition containingBean, AbstractBeanDefinition bd) { //如果bean节点含有scope属性则设置BeanDefinition的scope属性 if (ele.hasAttribute(SCOPE_ATTRIBUTE)) { bd.setScope(ele.getAttribute(SCOPE_ATTRIBUTE)); //如果设置了scope属性同时设置了singleton,则进行错误处理!不允许同时设置scope和singleton if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) { error("Specify either 'scope' or 'singleton', not both", ele); } } else if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) { //如果bean节点中包含singletone属性且值为true,则设置scope属性为singleton否则设置为prototype bd.setScope(TRUE_VALUE.equals(ele.getAttribute(SINGLETON_ATTRIBUTE)) ? BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE); } else if (containingBean != null) { // 如果含有内部bean则作用域以内部bean为准. bd.setScope(containingBean.getScope()); } //如果bean节点包含abstract值且值为true则设置beanDefinition的值为true否则设置为false if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) { bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE))); } //获取lazy-init属性 String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE); //如果lazyInit的值等于default则使用beans根节点中设置的default-lazy-init值 if (DEFAULT_VALUE.equals(lazyInit)) { lazyInit = this.defaults.getLazyInit(); } bd.setLazyInit(TRUE_VALUE.equals(lazyInit)); //获取bean节点autowire属性值 String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE); //设置beanDefinition的AutowireMode bd.setAutowireMode(getAutowireMode(autowire)); //获取dependency-check属性 String dependencyCheck = ele.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE); bd.setDependencyCheck(getDependencyCheck(dependencyCheck)); //如果bean节点中包含depends-on属性值 if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) { //获取depends-on节点值 String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE); //设置beandefinition的dependsOn属性 bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS)); } //获取autowire-candidate属性值 String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE); //如果autowireCandidate的值是空获取是default if ("".equals(autowireCandidate) || DEFAULT_VALUE.equals(autowireCandidate)) { //获取beans节点中设置的default-autowire-candidates属性值 String candidatePattern = this.defaults.getAutowireCandidates(); //如果candidatePattern不为null if (candidatePattern != null) { //将candidatePattern使用,分割为字符串数组 String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern); //如果正则匹配beanName则设置为true否则设置为false bd.setAutowireCandidate(PatternMatchUtils.simpleMatch(patterns, beanName)); } } else { //如果没有设置autowire-candidate或者值不是false,如果其值是true则设置为true否则设置为false bd.setAutowireCandidate(TRUE_VALUE.equals(autowireCandidate)); } // 如果bean节点包含primary属性,则设置beanDefinition的primary属性 if (ele.hasAttribute(PRIMARY_ATTRIBUTE)) { bd.setPrimary(TRUE_VALUE.equals(ele.getAttribute(PRIMARY_ATTRIBUTE))); } //如果bean节点中包含init-method属性 if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) { //获取init-method属性值 String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE); //如果initMethodName不是空字符串则设置initMethodName if (!"".equals(initMethodName)) { bd.setInitMethodName(initMethodName); } } else { //如果bean节点没有设置init-method,但是beans节点中设置了default-init-method if (this.defaults.getInitMethod() != null) { bd.setInitMethodName(this.defaults.getInitMethod()); bd.setEnforceInitMethod(false); } } //如果bean节点中包含destory-method或者beans节点中设置了defult-destory-method属性则设置beanDefinition的destroyMethod if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) { String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE); if (!"".equals(destroyMethodName)) { bd.setDestroyMethodName(destroyMethodName); } } else { if (this.defaults.getDestroyMethod() != null) { bd.setDestroyMethodName(this.defaults.getDestroyMethod()); bd.setEnforceDestroyMethod(false); } } //如果bean节点中包含了factory-method,则设置beanDefinition的factoryMethod if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) { bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE)); } //如果bean节点设置了factory-bean则设置beanDefinition的factoryBean if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) { bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE)); } return bd; } /** * 使用给定的类名称和父类名称创建一个BeanDifinition */ protected AbstractBeanDefinition createBeanDefinition(String className, String parentName) throws ClassNotFoundException { return BeanDefinitionReaderUtils.createBeanDefinition( parentName, className, this.readerContext.getBeanClassLoader()); } //解析Bean的元节点 public void parseMetaElements(Element ele, BeanMetadataAttributeAccessor attributeAccessor) { //获取bean所有子节点 NodeList nl = ele.getChildNodes(); //遍历所有元节点 for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); / if (isCandidateElement(node) && nodeNameEquals(node, META_ELEMENT)) { // 强制类型转换 Element metaElement = (Element) node; //获取key String key = metaElement.getAttribute(KEY_ATTRIBUTE); //获取value String value = metaElement.getAttribute(VALUE_ATTRIBUTE); //使用key,value构成一个元属性 BeanMetadataAttribute attribute = new BeanMetadataAttribute(key, value); attribute.setSource(extractSource(metaElement)); attributeAccessor.addMetadataAttribute(attribute); } } } public int getAutowireMode(String attValue) { String att = attValue; if (DEFAULT_VALUE.equals(att)) { att = this.defaults.getAutowire(); } int autowire = AbstractBeanDefinition.AUTOWIRE_NO; if (AUTOWIRE_BY_NAME_VALUE.equals(att)) { autowire = AbstractBeanDefinition.AUTOWIRE_BY_NAME; } else if (AUTOWIRE_BY_TYPE_VALUE.equals(att)) { autowire = AbstractBeanDefinition.AUTOWIRE_BY_TYPE; } else if (AUTOWIRE_CONSTRUCTOR_VALUE.equals(att)) { autowire = AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR; } else if (AUTOWIRE_AUTODETECT_VALUE.equals(att)) { autowire = AbstractBeanDefinition.AUTOWIRE_AUTODETECT; } // Else leave default value. return autowire; } public int getDependencyCheck(String attValue) { String att = attValue; if (DEFAULT_VALUE.equals(att)) { att = this.defaults.getDependencyCheck(); } if (DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE.equals(att)) { return AbstractBeanDefinition.DEPENDENCY_CHECK_ALL; } else if (DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE.equals(att)) { return AbstractBeanDefinition.DEPENDENCY_CHECK_OBJECTS; } else if (DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE.equals(att)) { return AbstractBeanDefinition.DEPENDENCY_CHECK_SIMPLE; } else { return AbstractBeanDefinition.DEPENDENCY_CHECK_NONE; } } /** * 解析构造参数元素 */ public void parseConstructorArgElements(Element beanEle, BeanDefinition bd) { NodeList nl = beanEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (isCandidateElement(node) && nodeNameEquals(node, CONSTRUCTOR_ARG_ELEMENT)) { parseConstructorArgElement((Element) node, bd); } } } /** * 解析给定bean节点的属性元素 */ public void parsePropertyElements(Element beanEle, BeanDefinition bd) { NodeList nl = beanEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (isCandidateElement(node) && nodeNameEquals(node, PROPERTY_ELEMENT)) { parsePropertyElement((Element) node, bd); } } } /** * 解析给定元素的 qualifier 子节点 */ public void parseQualifierElements(Element beanEle, AbstractBeanDefinition bd) { NodeList nl = beanEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (isCandidateElement(node) && nodeNameEquals(node, QUALIFIER_ELEMENT)) { parseQualifierElement((Element) node, bd); } } } /** *解析给定节点的lookup-method子节点 */ public void parseLookupOverrideSubElements(Element beanEle, MethodOverrides overrides) { //获取bean节点的所有子节点 NodeList nl = beanEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); //如果节点是lookup-method if (isCandidateElement(node) && nodeNameEquals(node, LOOKUP_METHOD_ELEMENT)) { Element ele = (Element) node; String methodName = ele.getAttribute(NAME_ATTRIBUTE); String beanRef = ele.getAttribute(BEAN_ELEMENT); LookupOverride override = new LookupOverride(methodName, beanRef); override.setSource(extractSource(ele)); overrides.addOverride(override); } } } /** * */ public void parseReplacedMethodSubElements(Element beanEle, MethodOverrides overrides) { NodeList nl = beanEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (isCandidateElement(node) && nodeNameEquals(node, REPLACED_METHOD_ELEMENT)) { Element replacedMethodEle = (Element) node; String name = replacedMethodEle.getAttribute(NAME_ATTRIBUTE); String callback = replacedMethodEle.getAttribute(REPLACER_ATTRIBUTE); ReplaceOverride replaceOverride = new ReplaceOverride(name, callback); // Look for arg-type match elements. List<Element> argTypeEles = DomUtils.getChildElementsByTagName(replacedMethodEle, ARG_TYPE_ELEMENT); for (Element argTypeEle : argTypeEles) { String match = argTypeEle.getAttribute(ARG_TYPE_MATCH_ATTRIBUTE); match = (StringUtils.hasText(match) ? match : DomUtils.getTextValue(argTypeEle)); if (StringUtils.hasText(match)) { replaceOverride.addTypeIdentifier(match); } } replaceOverride.setSource(extractSource(replacedMethodEle)); overrides.addOverride(replaceOverride); } } } /** * */ public void parseConstructorArgElement(Element ele, BeanDefinition bd) { String indexAttr = ele.getAttribute(INDEX_ATTRIBUTE); String typeAttr = ele.getAttribute(TYPE_ATTRIBUTE); String nameAttr = ele.getAttribute(NAME_ATTRIBUTE); if (StringUtils.hasLength(indexAttr)) { try { int index = Integer.parseInt(indexAttr); if (index < 0) { error("'index' cannot be lower than 0", ele); } else { try { this.parseState.push(new ConstructorArgumentEntry(index)); Object value = parsePropertyValue(ele, bd, null); ConstructorArgumentValues.ValueHolder valueHolder = new ConstructorArgumentValues.ValueHolder(value); if (StringUtils.hasLength(typeAttr)) { valueHolder.setType(typeAttr); } if (StringUtils.hasLength(nameAttr)) { valueHolder.setName(nameAttr); } valueHolder.setSource(extractSource(ele)); if (bd.getConstructorArgumentValues().hasIndexedArgumentValue(index)) { error("Ambiguous constructor-arg entries for index " + index, ele); } else { bd.getConstructorArgumentValues().addIndexedArgumentValue(index, valueHolder); } } finally { this.parseState.pop(); } } } catch (NumberFormatException ex) { error("Attribute 'index' of tag 'constructor-arg' must be an integer", ele); } } else { try { this.parseState.push(new ConstructorArgumentEntry()); Object value = parsePropertyValue(ele, bd, null); ConstructorArgumentValues.ValueHolder valueHolder = new ConstructorArgumentValues.ValueHolder(value); if (StringUtils.hasLength(typeAttr)) { valueHolder.setType(typeAttr); } if (StringUtils.hasLength(nameAttr)) { valueHolder.setName(nameAttr); } valueHolder.setSource(extractSource(ele)); bd.getConstructorArgumentValues().addGenericArgumentValue(valueHolder); } finally { this.parseState.pop(); } } } /** * */ public void parsePropertyElement(Element ele, BeanDefinition bd) { String propertyName = ele.getAttribute(NAME_ATTRIBUTE); if (!StringUtils.hasLength(propertyName)) { error("Tag 'property' must have a 'name' attribute", ele); return; } this.parseState.push(new PropertyEntry(propertyName)); try { if (bd.getPropertyValues().contains(propertyName)) { error("Multiple 'property' definitions for property '" + propertyName + "'", ele); return; } Object val = parsePropertyValue(ele, bd, propertyName); PropertyValue pv = new PropertyValue(propertyName, val); parseMetaElements(ele, pv); pv.setSource(extractSource(ele)); bd.getPropertyValues().addPropertyValue(pv); } finally { this.parseState.pop(); } } /** * */ public void parseQualifierElement(Element ele, AbstractBeanDefinition bd) { String typeName = ele.getAttribute(TYPE_ATTRIBUTE); if (!StringUtils.hasLength(typeName)) { error("Tag 'qualifier' must have a 'type' attribute", ele); return; } this.parseState.push(new QualifierEntry(typeName)); try { AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(typeName); qualifier.setSource(extractSource(ele)); String value = ele.getAttribute(VALUE_ATTRIBUTE); if (StringUtils.hasLength(value)) { qualifier.setAttribute(AutowireCandidateQualifier.VALUE_KEY, value); } NodeList nl = ele.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (isCandidateElement(node) && nodeNameEquals(node, QUALIFIER_ATTRIBUTE_ELEMENT)) { Element attributeEle = (Element) node; String attributeName = attributeEle.getAttribute(KEY_ATTRIBUTE); String attributeValue = attributeEle.getAttribute(VALUE_ATTRIBUTE); if (StringUtils.hasLength(attributeName) && StringUtils.hasLength(attributeValue)) { BeanMetadataAttribute attribute = new BeanMetadataAttribute(attributeName, attributeValue); attribute.setSource(extractSource(attributeEle)); qualifier.addMetadataAttribute(attribute); } else { error("Qualifier 'attribute' tag must have a 'name' and 'value'", attributeEle); return; } } } bd.addQualifier(qualifier); } finally { this.parseState.pop(); } } /** * 获取指定属性的值 */ public Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) { String elementName = (propertyName != null) ? "<property> element for property '" + propertyName + "'" : "<constructor-arg> element"; // Should only have one child element: ref, value, list, etc. NodeList nl = ele.getChildNodes(); Element subElement = null; for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element && !nodeNameEquals(node, DESCRIPTION_ELEMENT) && !nodeNameEquals(node, META_ELEMENT)) { // Child element is what we're looking for. if (subElement != null) { error(elementName + " must not contain more than one sub-element", ele); } else { subElement = (Element) node; } } } boolean hasRefAttribute = ele.hasAttribute(REF_ATTRIBUTE); boolean hasValueAttribute = ele.hasAttribute(VALUE_ATTRIBUTE); if ((hasRefAttribute && hasValueAttribute) || ((hasRefAttribute || hasValueAttribute) && subElement != null)) { error(elementName + " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element", ele); } if (hasRefAttribute) { String refName = ele.getAttribute(REF_ATTRIBUTE); if (!StringUtils.hasText(refName)) { error(elementName + " contains empty 'ref' attribute", ele); } RuntimeBeanReference ref = new RuntimeBeanReference(refName); ref.setSource(extractSource(ele)); return ref; } else if (hasValueAttribute) { TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE)); valueHolder.setSource(extractSource(ele)); return valueHolder; } else if (subElement != null) { return parsePropertySubElement(subElement, bd); } else { // Neither child element nor "ref" or "value" attribute found. error(elementName + " must specify a ref or value", ele); return null; } } public Object parsePropertySubElement(Element ele, BeanDefinition bd) { return parsePropertySubElement(ele, bd, null); } /** *解析一个属性的子节点 */ public Object parsePropertySubElement(Element ele, BeanDefinition bd, String defaultValueType) { //如果子节点的命名空间是默认命名空间的 if (!isDefaultNamespace(ele)) { return parseNestedCustomElement(ele, bd); } else if (nodeNameEquals(ele, BEAN_ELEMENT)) { //如果子节点是bean则解析该bean配置 BeanDefinitionHolder nestedBd = parseBeanDefinitionElement(ele, bd); if (nestedBd != null) { nestedBd = decorateBeanDefinitionIfRequired(ele, nestedBd, bd); } //返回嵌套bean return nestedBd; } else if (nodeNameEquals(ele, REF_ELEMENT)) { //如果子节是ref,获取引用bean 名称 String refName = ele.getAttribute(BEAN_REF_ATTRIBUTE); boolean toParent = false; //如果引用bean名称为空 if (!StringUtils.hasLength(refName)) { //获取local值 refName = ele.getAttribute(LOCAL_REF_ATTRIBUTE); //如果local值为空 if (!StringUtils.hasLength(refName)) { // 获取parent值 refName = ele.getAttribute(PARENT_REF_ATTRIBUTE); toParent = true; //如果parent值也为空则进行错误处理 if (!StringUtils.hasLength(refName)) { error("'bean', 'local' or 'parent' is required for <ref> element", ele); return null; } } } // 如果refName中不包含文字则进行错误处理 if (!StringUtils.hasText(refName)) { error("<ref> element contains empty target attribute", ele); return null; } //创建一个RuntimeBeanReference对象 RuntimeBeanReference ref = new RuntimeBeanReference(refName, toParent); ref.setSource(extractSource(ele)); return ref; } else if (nodeNameEquals(ele, IDREF_ELEMENT)) { //如果节点是idref return parseIdRefElement(ele); } else if (nodeNameEquals(ele, VALUE_ELEMENT)) { // 如果节点是value return parseValueElement(ele, defaultValueType); } else if (nodeNameEquals(ele, NULL_ELEMENT)) { //如果节点是null TypedStringValue nullHolder = new TypedStringValue(null); nullHolder.setSource(extractSource(ele)); return nullHolder; } else if (nodeNameEquals(ele, ARRAY_ELEMENT)) { //如果节点是array return parseArrayElement(ele, bd); } else if (nodeNameEquals(ele, LIST_ELEMENT)) { //如果节点是list return parseListElement(ele, bd); } else if (nodeNameEquals(ele, SET_ELEMENT)) { //如果节点是set return parseSetElement(ele, bd); } else if (nodeNameEquals(ele, MAP_ELEMENT)) { //如果节点是map return parseMapElement(ele, bd); } else if (nodeNameEquals(ele, PROPS_ELEMENT)) { //如果节点是props return parsePropsElement(ele); } else { error("Unknown property sub-element: [" + ele.getNodeName() + "]", ele); return null; } } /** * */ public Object parseIdRefElement(Element ele) { // A generic reference to any name of any bean. String refName = ele.getAttribute(BEAN_REF_ATTRIBUTE); if (!StringUtils.hasLength(refName)) { // A reference to the id of another bean in the same XML file. refName = ele.getAttribute(LOCAL_REF_ATTRIBUTE); if (!StringUtils.hasLength(refName)) { error("Either 'bean' or 'local' is required for <idref> element", ele); return null; } } if (!StringUtils.hasText(refName)) { error("<idref> element contains empty target attribute", ele); return null; } RuntimeBeanNameReference ref = new RuntimeBeanNameReference(refName); ref.setSource(extractSource(ele)); return ref; } /** * Return a typed String value Object for the given value element. */ public Object parseValueElement(Element ele, String defaultTypeName) { // It's a literal value. String value = DomUtils.getTextValue(ele); String specifiedTypeName = ele.getAttribute(TYPE_ATTRIBUTE); String typeName = specifiedTypeName; if (!StringUtils.hasText(typeName)) { typeName = defaultTypeName; } try { TypedStringValue typedValue = buildTypedStringValue(value, typeName); typedValue.setSource(extractSource(ele)); typedValue.setSpecifiedTypeName(specifiedTypeName); return typedValue; } catch (ClassNotFoundException ex) { error("Type class [" + typeName + "] not found for <value> element", ele, ex); return value; } } /** * 根据给定的值,已经类型创建一个TypedStringValue */ protected TypedStringValue buildTypedStringValue(String value, String targetTypeName) throws ClassNotFoundException { ClassLoader classLoader = this.readerContext.getBeanClassLoader(); TypedStringValue typedValue; if (!StringUtils.hasText(targetTypeName)) { typedValue = new TypedStringValue(value); } else if (classLoader != null) { Class<?> targetType = ClassUtils.forName(targetTypeName, classLoader); typedValue = new TypedStringValue(value, targetType); } else { typedValue = new TypedStringValue(value, targetTypeName); } return typedValue; } /** * 解析array节点 */ public Object parseArrayElement(Element arrayEle, BeanDefinition bd) { //获取value-type String elementType = arrayEle.getAttribute(VALUE_TYPE_ATTRIBUTE); //获取子节点 NodeList nl = arrayEle.getChildNodes(); //根据value-type和子节点长度创建一个ManagedArray ManagedArray target = new ManagedArray(elementType, nl.getLength()); target.setSource(extractSource(arrayEle)); target.setElementTypeName(elementType); target.setMergeEnabled(parseMergeAttribute(arrayEle)); parseCollectionElements(nl, target, bd, elementType); return target; } /** * Parse a list element. */ public List parseListElement(Element collectionEle, BeanDefinition bd) { String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = collectionEle.getChildNodes(); ManagedList<Object> target = new ManagedList<Object>(nl.getLength()); target.setSource(extractSource(collectionEle)); target.setElementTypeName(defaultElementType); target.setMergeEnabled(parseMergeAttribute(collectionEle)); parseCollectionElements(nl, target, bd, defaultElementType); return target; } /** * Parse a set element. */ public Set parseSetElement(Element collectionEle, BeanDefinition bd) { String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = collectionEle.getChildNodes(); ManagedSet<Object> target = new ManagedSet<Object>(nl.getLength()); target.setSource(extractSource(collectionEle)); target.setElementTypeName(defaultElementType); target.setMergeEnabled(parseMergeAttribute(collectionEle)); parseCollectionElements(nl, target, bd, defaultElementType); return target; } protected void parseCollectionElements( NodeList elementNodes, Collection<Object> target, BeanDefinition bd, String defaultElementType) { //遍历子节点 for (int i = 0; i < elementNodes.getLength(); i++) { Node node = elementNodes.item(i); //如果节点是element且不是description则将其添加到target中 if (node instanceof Element && !nodeNameEquals(node, DESCRIPTION_ELEMENT)) { target.add(parsePropertySubElement((Element) node, bd, defaultElementType)); } } } /** * 解析map节点 */ public Map parseMapElement(Element mapEle, BeanDefinition bd) { //获取key-type String defaultKeyType = mapEle.getAttribute(KEY_TYPE_ATTRIBUTE); //获取value-type String defaultValueType = mapEle.getAttribute(VALUE_TYPE_ATTRIBUTE); //获取map节点的所有entry子节点 List<Element> entryEles = DomUtils.getChildElementsByTagName(mapEle, ENTRY_ELEMENT); //根据entry子节点的数量创建一个ManagedMap ManagedMap<Object, Object> map = new ManagedMap<Object, Object>(entryEles.size()); map.setSource(extractSource(mapEle)); //设置KeyType map.setKeyTypeName(defaultKeyType); //设置valueType map.setValueTypeName(defaultValueType); map.setMergeEnabled(parseMergeAttribute(mapEle)); //遍历Entry for (Element entryEle : entryEles) { //获取entry的子节点 NodeList entrySubNodes = entryEle.getChildNodes(); Element keyEle = null; Element valueEle = null; //遍历entry子节点 for (int j = 0; j < entrySubNodes.getLength(); j++) { Node node = entrySubNodes.item(j); if (node instanceof Element) { Element candidateEle = (Element) node; if (nodeNameEquals(candidateEle, KEY_ELEMENT)) { if (keyEle != null) { error("<entry> element is only allowed to contain one <key> sub-element", entryEle); } else { keyEle = candidateEle; } } else { // Child element is what we're looking for. if (nodeNameEquals(candidateEle, DESCRIPTION_ELEMENT)) { // the element is a <description> -> ignore it } else if (valueEle != null) { error("<entry> element must not contain more than one value sub-element", entryEle); } else { valueEle = candidateEle; } } } } Object key = null; //entry节点是否包含key属性 boolean hasKeyAttribute = entryEle.hasAttribute(KEY_ATTRIBUTE); //entry节点是否包含key-ref属性 boolean hasKeyRefAttribute = entryEle.hasAttribute(KEY_REF_ATTRIBUTE); //key,key-ref,或者key的子节点不能同时配置, if ((hasKeyAttribute && hasKeyRefAttribute) || ((hasKeyAttribute || hasKeyRefAttribute)) && keyEle != null) { error("<entry> element is only allowed to contain either " + "a 'key' attribute OR a 'key-ref' attribute OR a <key> sub-element", entryEle); } if (hasKeyAttribute) { //如果含有key属性则更具key属性值创建一个TypedStringValue key = buildTypedStringValueForMap(entryEle.getAttribute(KEY_ATTRIBUTE), defaultKeyType, entryEle); } else if (hasKeyRefAttribute) { //如果含有key-ref且值不为空则创建一个RuntimeBaseReference String refName = entryEle.getAttribute(KEY_REF_ATTRIBUTE); if (!StringUtils.hasText(refName)) { error("<entry> element contains empty 'key-ref' attribute", entryEle); } RuntimeBeanReference ref = new RuntimeBeanReference(refName); ref.setSource(extractSource(entryEle)); key = ref; } else if (keyEle != null) { key = parseKeyElement(keyEle, bd, defaultKeyType); } else { error("<entry> element must specify a key", entryEle); } Object value = null; boolean hasValueAttribute = entryEle.hasAttribute(VALUE_ATTRIBUTE); boolean hasValueRefAttribute = entryEle.hasAttribute(VALUE_REF_ATTRIBUTE); if ((hasValueAttribute && hasValueRefAttribute) || ((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) { error("<entry> element is only allowed to contain either " + "'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle); } if (hasValueAttribute) { //如果有value属性则根据value属性的值创建一个TypeStringValue value = buildTypedStringValueForMap(entryEle.getAttribute(VALUE_ATTRIBUTE), defaultValueType, entryEle); } else if (hasValueRefAttribute) { String refName = entryEle.getAttribute(VALUE_REF_ATTRIBUTE); if (!StringUtils.hasText(refName)) { error("<entry> element contains empty 'value-ref' attribute", entryEle); } RuntimeBeanReference ref = new RuntimeBeanReference(refName); ref.setSource(extractSource(entryEle)); value = ref; } else if (valueEle != null) { value = parsePropertySubElement(valueEle, bd, defaultValueType); } else { error("<entry> element must specify a value", entryEle); } //向Map中添加数据 map.put(key, value); } return map; } /** *创建一个TypedStringValue */ protected final Object buildTypedStringValueForMap(String value, String defaultTypeName, Element entryEle) { try { //创建一个TypeStringValue TypedStringValue typedValue = buildTypedStringValue(value, defaultTypeName); typedValue.setSource(extractSource(entryEle)); return typedValue; } catch (ClassNotFoundException ex) { error("Type class [" + defaultTypeName + "] not found for Map key/value type", entryEle, ex); return value; } } /** * 解析key的子节点 */ protected Object parseKeyElement(Element keyEle, BeanDefinition bd, String defaultKeyTypeName) { //获取key的所有子节点 NodeList nl = keyEle.getChildNodes(); Element subElement = null; for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { // Child element is what we're looking for. if (subElement != null) { error("<key> element must not contain more than one value sub-element", keyEle); } else { subElement = (Element) node; } } } return parsePropertySubElement(subElement, bd, defaultKeyTypeName); } /** * Parse a props element. */ public Properties parsePropsElement(Element propsEle) { ManagedProperties props = new ManagedProperties(); props.setSource(extractSource(propsEle)); props.setMergeEnabled(parseMergeAttribute(propsEle)); List<Element> propEles = DomUtils.getChildElementsByTagName(propsEle, PROP_ELEMENT); for (Element propEle : propEles) { String key = propEle.getAttribute(KEY_ATTRIBUTE); // Trim the text value to avoid unwanted whitespace // caused by typical XML formatting. String value = DomUtils.getTextValue(propEle).trim(); TypedStringValue keyHolder = new TypedStringValue(key); keyHolder.setSource(extractSource(propEle)); TypedStringValue valueHolder = new TypedStringValue(value); valueHolder.setSource(extractSource(propEle)); props.put(keyHolder, valueHolder); } return props; } /** * Parse the merge attribute of a collection element, if any. */ public boolean parseMergeAttribute(Element collectionElement) { String value = collectionElement.getAttribute(MERGE_ATTRIBUTE); if (DEFAULT_VALUE.equals(value)) { value = this.defaults.getMerge(); } return TRUE_VALUE.equals(value); } public BeanDefinition parseCustomElement(Element ele) { return parseCustomElement(ele, null); } public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) { String namespaceUri = getNamespaceURI(ele); NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri); if (handler == null) { error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele); return null; } return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd)); } /** * 装饰BeanDefinition **/ public BeanDefinitionHolder decorateBeanDefinitionIfRequired(Element ele, BeanDefinitionHolder definitionHolder) { return decorateBeanDefinitionIfRequired(ele, definitionHolder, null); } public BeanDefinitionHolder decorateBeanDefinitionIfRequired( Element ele, BeanDefinitionHolder definitionHolder, BeanDefinition containingBd) { BeanDefinitionHolder finalDefinition = definitionHolder; // 获取节点的所有属性,[autowire,autowire-candidate,class,id,lazy-init] NamedNodeMap attributes = ele.getAttributes(); //遍历属性 for (int i = 0; i < attributes.getLength(); i++) { // autowire Node node = attributes.item(i); finalDefinition = decorateIfRequired(node, finalDefinition, containingBd); } NodeList children = ele.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { finalDefinition = decorateIfRequired(node, finalDefinition, containingBd); } } return finalDefinition; } private BeanDefinitionHolder decorateIfRequired( Node node, BeanDefinitionHolder originalDef, BeanDefinition containingBd) { //获取节点的命名空间,null String namespaceUri = getNamespaceURI(node); //如果命名空间不是默认的命名空间 if (!isDefaultNamespace(namespaceUri)) { //获取命名空间处理器 NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri); if (handler != null) { return handler.decorate(node, originalDef, new ParserContext(this.readerContext, this, containingBd)); } else if (namespaceUri != null && namespaceUri.startsWith("http://www.springframework.org/")) { error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", node); } else { // A custom namespace, not to be handled by Spring - maybe "xml:...". if (logger.isDebugEnabled()) { logger.debug("No Spring NamespaceHandler found for XML schema namespace [" + namespaceUri + "]"); } } } return originalDef; } private BeanDefinitionHolder parseNestedCustomElement(Element ele, BeanDefinition containingBd) { BeanDefinition innerDefinition = parseCustomElement(ele, containingBd); if (innerDefinition == null) { error("Incorrect usage of element '" + ele.getNodeName() + "' in a nested manner. " + "This tag cannot be used nested inside <property>.", ele); return null; } String id = ele.getNodeName() + BeanDefinitionReaderUtils.GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(innerDefinition); if (logger.isDebugEnabled()) { logger.debug("Using generated bean name [" + id + "] for nested custom element '" + ele.getNodeName() + "'"); } return new BeanDefinitionHolder(innerDefinition, id); } /** *获取指定节点的NamespaceURI */ public String getNamespaceURI(Node node) { return node.getNamespaceURI(); } public String getLocalName(Node node) { return node.getLocalName(); } public boolean nodeNameEquals(Node node, String desiredName) { return desiredName.equals(node.getNodeName()) || desiredName.equals(getLocalName(node)); } /** * 如果给定的namespaceUri不为空且等于http://www.springframework.org/schema/beans 则是默认的命名空间 **/ public boolean isDefaultNamespace(String namespaceUri) { return (!StringUtils.hasLength(namespaceUri) || BEANS_NAMESPACE_URI.equals(namespaceUri)); } /** * 判断给定的节点的namespaceUri是不是默认的命名空间值 **/ public boolean isDefaultNamespace(Node node) { return isDefaultNamespace(getNamespaceURI(node)); } private boolean isCandidateElement(Node node) { return (node instanceof Element && (isDefaultNamespace(node) || !isDefaultNamespace(node.getParentNode()))); } }
相关推荐
`BeanDefinitionParser`通常是一个实现了`BeanDefinitionParserDelegate`接口的类,这个接口定义了一系列方法,如`parseBeanDefinitionElement()`和`parseCustomElement()`,用于解析bean定义和自定义标签。...
本篇文章主要分析了Spring如何通过`ClassPathXmlApplicationContext`来启动和初始化Bean的过程。 首先,`ClassPathXmlApplicationContext`是Spring的一种ApplicationContext实现,用于从类路径下的XML配置文件加载...
在`processBeanDefinition`方法中,Spring使用`BeanDefinitionParserDelegate`的`parseBeanDefinitionElement`方法来解析`<bean>`标签。这个方法会根据`<bean>`标签的属性(如id、class等)创建一个`BeanDefinition`...
5. **源码分析** Spring框架中,`BeanDefinitionParserDelegate`类是处理bean定义解析的核心,它会根据XML标签的namespace和localName找到对应的`BeanDefinitionParser`进行解析。`BeanDefinitionBuilder`类则提供...
本文将通过源码分析,结合个人理解和相关博客资料,深入探讨`spring-beans`模块的核心概念和实现机制。 1. **Bean的定义** 在Spring中,Bean的定义通常存储在XML配置文件或Java配置类中。`...
在 BeanDefinitionDocumentReader 对象的 processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) 方法中,我们将 Bean 元素的解析委托给了 BeanDefinitionParserDelegate 对象来完成。...
本文将深入探讨Spring如何通过读取配置文件实现依赖注入,并讲解相关源码,帮助理解其工作原理。 在Spring中,配置文件通常为XML格式,如`applicationContext.xml`,它定义了bean的实例化、属性设置、装配关系等。...
- `BeanDefinitionParserDelegate`是解析bean定义属性的关键类,它解释XML配置中bean的属性,如scope、lazy-init、autowire模式等,并将这些信息记录到`BeanDefinition`中。 6. **刷新工厂** - 在`...
然而,当遇到非默认命名空间的元素时,Spring会调用`BeanDefinitionParserDelegate`的`parseCustomElement`方法进行处理。这个方法会寻找匹配的`NamespaceHandler`来解析自定义标签。`NamespaceHandler`是一个接口,...
5. Spring的autowiring机制自动管理Bean之间的依赖,通过分析Bean名称或类型来匹配依赖,并利用`DefaultSingletonBeanRegistry`进行依赖注册。 总结: Spring的IOC设计原理涉及Bean的管理、依赖注入以及高级特性。...
Bean 定义解析 `BeanDefinitionParserDelegate` 是解析 `<bean>` 元素的关键,它会处理各种属性,如 class(Bean 的实现类)、scope(作用域)、property(属性)等,将这些信息存储为 `BeanDefinition` 对象。...
它首先获取了InputStream,然后将其传递给DocumentLoader,DocumentLoader将InputStream转换为Document对象,最后将Document对象传递给BeanDefinitionParserDelegate,BeanDefinitionParserDelegate将Document对象...
在`BeanDefinitionParserDelegate`类中,`parseBeanDefinitionElement`方法扮演着关键角色。它解析`<bean>`标签,从中提取出`class`属性(定义bean的实现类)、`parent`属性(定义父bean)、以及其他的属性,如`...
这通常通过实现`org.springframework.beans.factory.config.BeanDefinitionParserDelegate`接口并注册到Spring的XML解析器中来完成。例如,创建一个名为`myTag`的自定义标签: ```xml ``` 为了处理这个自定义标签...
在`spring-beans-4.3.6.RELEASE.jar`中,包含了解析XML配置文件的相关类,如`BeanDefinitionParserDelegate`和`DefaultListableBeanFactory`等。 - XML配置文件中,除了定义bean外,还可以设置bean的作用域、初始化...
- **BeanDefinitionParserDelegate**:提供了解析Bean定义的方法。 - **BeanDefinitionHolder**:持有BeanDefinition及其别名。 **容器解析相关类:** - **BeanDefinitionMap**:以Map的形式存储BeanDefinition,...
入口是在org.springframework.beans.factory.xml.XmlBeanDefinitionReader这个类,但是实际干活的是在org.springframework.beans.factory.xml.BeanDefinitionParserDelegate。代码很多,但实际逻辑很简单,就是解析...
BeanDefinitionParserDelegate BeanDefinitionParsingException BeanDefinitionReader BeanDefinitionReaderUtils BeanDefinitionRegistry BeanDefinitionStoreException BeanDefinitionValidationException ...