List of usage examples for org.springframework.beans.factory.support BeanDefinitionReaderUtils generateBeanName
public static String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry, boolean isInnerBean) throws BeanDefinitionStoreException
From source file:com.mtgi.analytics.aop.config.v11.BtJdbcPersisterBeanDefinitionParser.java
@Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { //configure ID generator settings NodeList children = element.getElementsByTagNameNS("*", ELT_ID_SQL); if (children.getLength() == 1) { BeanDefinition def = builder.getRawBeanDefinition(); MutablePropertyValues props = def.getPropertyValues(); Element child = (Element) children.item(0); String sql = child.getTextContent(); if (StringUtils.hasText(sql)) props.addPropertyValue("idSql", sql); if (child.hasAttribute(ATT_INCREMENT)) props.addPropertyValue("idIncrement", child.getAttribute(ATT_INCREMENT)); }//w ww.j a v a 2 s. c om //configure nested dataSource NodeList nodes = element.getElementsByTagNameNS("*", "data-source"); if (nodes.getLength() == 1) { Element ds = (Element) nodes.item(0); ds.setAttribute("name", "dataSource"); parserContext.getDelegate().parsePropertyElement(ds, builder.getRawBeanDefinition()); } //push persister into parent manager bean, if applicable if (parserContext.isNested()) { AbstractBeanDefinition def = builder.getBeanDefinition(); String id = element.hasAttribute("id") ? element.getAttribute("id") : BeanDefinitionReaderUtils.generateBeanName(def, parserContext.getReaderContext().getRegistry(), true); BeanDefinitionHolder holder = new BeanDefinitionHolder(def, id); BtManagerBeanDefinitionParser.registerNestedBean(holder, "persister", parserContext); } }
From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java
/** * Parses the supplied {@code <bean>} element. May return {@code null} * if there were errors during parse. Errors are reported to the * {@link org.springframework.beans.factory.parsing.ProblemReporter}. *///from www.j av a2 s . c om @Nullable public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, @Nullable BeanDefinition containingBean) { String id = ele.getAttribute(ID_ATTRIBUTE); String nameAttr = ele.getAttribute(NAME_ATTRIBUTE); List<String> 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.isDebugEnabled()) { logger.debug("No XML 'id' specified - using '" + beanName + "' as bean name and " + aliases + " as aliases"); } } if (containingBean == null) { checkNameUniqueness(beanName, aliases, ele); } 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; }
From source file:org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.java
/** * Parse a standard bean definition into a BeanDefinitionHolder, * including bean name and aliases.//from w ww. ja v a2s . c o m * <p>Bean elements specify their canonical name as "id" attribute * and their aliases as a delimited "name" attribute. * <p>If no "id" specified, uses the first name in the "name" attribute * as canonical name, registering all others as aliases. * <p>Callers should specify whether this element represents an inner bean * definition or not by setting the <code>isInnerBean</code> argument appropriately */ protected BeanDefinitionHolder parseBeanDefinitionElement(Element ele, boolean isInnerBean) throws BeanDefinitionStoreException { String id = ele.getAttribute(ID_ATTRIBUTE); String nameAttr = ele.getAttribute(NAME_ATTRIBUTE); List aliases = new ArrayList(); if (StringUtils.hasLength(nameAttr)) { String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, BEAN_NAME_DELIMITERS); aliases.addAll(Arrays.asList(nameArr)); } String beanName = id; if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) { beanName = (String) aliases.remove(0); if (logger.isDebugEnabled()) { logger.debug("No XML 'id' specified - using '" + beanName + "' as bean name and " + aliases + " as aliases"); } } BeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName); if (!StringUtils.hasText(beanName) && beanDefinition instanceof AbstractBeanDefinition) { beanName = BeanDefinitionReaderUtils.generateBeanName((AbstractBeanDefinition) beanDefinition, this.beanDefinitionReader.getBeanFactory(), isInnerBean); if (logger.isDebugEnabled()) { logger.debug("Neither XML 'id' nor 'name' specified - " + "using generated bean name [" + beanName + "]"); } } String[] aliasesArray = StringUtils.toStringArray(aliases); return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray); }