Example usage for org.springframework.beans.factory.config RuntimeBeanNameReference getBeanName

List of usage examples for org.springframework.beans.factory.config RuntimeBeanNameReference getBeanName

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config RuntimeBeanNameReference getBeanName.

Prototype

@Override
    public String getBeanName() 

Source Link

Usage

From source file:org.smf4j.spring.RegistryNodeTemplateDefinitionParser.java

protected String parseCustom(ParserContext context, Element element, BeanDefinition containingBean) {
    String name = getName(context, element);
    String ref = element.getAttribute(REF_ATTR);

    String customBeanId = null;//www.j  a  v a2 s .c  o  m
    if (StringUtils.hasLength(ref)) {
        customBeanId = ref;
    } else {
        // Grab the single child element, that should define or point
        // to the custom Accumulator or Calcuation bean definition.
        NodeList childList = element.getChildNodes();
        Element child = null;
        for (int i = 0; i < childList.getLength(); i++) {
            Node childNode = childList.item(i);
            if (!(childNode instanceof Element)) {
                continue;
            }

            if (child != null) {
                context.getReaderContext()
                        .error("'custom' elements without a 'ref' attribute must "
                                + "have exactly one 'bean', 'ref', or 'idref' child" + " element.",
                                context.extractSource(element));
            }
            child = (Element) childNode;
        }

        if (child == null) {
            context.getReaderContext()
                    .error("'custom' elements must specify a 'ref' attribute or a "
                            + "single 'bean', 'ref', or 'idref' child element.",
                            context.extractSource(element));
        }

        // Parse the contents of the custom bean
        Object o = context.getDelegate().parsePropertySubElement(child, containingBean);

        if (o instanceof BeanDefinitionHolder) {
            BeanDefinitionHolder bdh = (BeanDefinitionHolder) o;
            customBeanId = bdh.getBeanName();
            if (!StringUtils.hasLength(customBeanId)) {
                // They didn't give their bean an id, so we'll need to
                // generate one for it now.
                customBeanId = context.getReaderContext().generateBeanName(bdh.getBeanDefinition());
            }

            // Register this bean
            context.getRegistry().registerBeanDefinition(customBeanId, bdh.getBeanDefinition());
        } else if (o instanceof RuntimeBeanReference) {
            RuntimeBeanReference rbr = (RuntimeBeanReference) o;
            customBeanId = rbr.getBeanName();
        } else if (o instanceof RuntimeBeanNameReference) {
            RuntimeBeanNameReference rbnr = (RuntimeBeanNameReference) o;
            customBeanId = rbnr.getBeanName();
        }
    }

    // Create proxy that associates the given name with this child
    BeanDefinitionBuilder accProxyBdb = getBdb(RegistryNodeChildProxy.class);
    accProxyBdb.addPropertyValue(NAME_ATTR, name);
    accProxyBdb.addPropertyValue(CHILD_ATTR, customBeanId);
    accProxyBdb.getRawBeanDefinition().setSource(element);

    return context.getReaderContext().registerWithGeneratedName(accProxyBdb.getBeanDefinition());
}