Example usage for org.springframework.beans.factory.support AbstractBeanDefinition setResource

List of usage examples for org.springframework.beans.factory.support AbstractBeanDefinition setResource

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support AbstractBeanDefinition setResource.

Prototype

public void setResource(@Nullable Resource resource) 

Source Link

Document

Set the resource that this bean definition came from (for the purpose of showing context in case of errors).

Usage

From source file:com.griddynamics.banshun.config.xml.ImportBeanDefinitionParser.java

@Override
protected void doParse(Element el, ParserContext parserContext, BeanDefinitionBuilder builder) {

    Resource resource = parserContext.getReaderContext().getResource();

    String rootName = defaultIfBlank(el.getAttribute(ROOT_ATTR), DEFAULT_ROOT_FACTORY_NAME);
    String serviceIfaceName = el.getAttribute(INTERFACE_ATTR);
    String serviceName = el.getAttribute(ID_ATTR);

    Class<?> serviceIface = ParserUtils.findClassByName(serviceIfaceName, el.getAttribute(ID_ATTR),
            parserContext);//from www  .j av  a  2 s .c  o m

    AbstractBeanDefinition beanDef = builder.getRawBeanDefinition();
    beanDef.setFactoryBeanName(rootName);
    beanDef.setFactoryMethodName(Registry.LOOKUP_METHOD_NAME);
    beanDef.setConstructorArgumentValues(defineLookupMethodArgs(serviceName, serviceIface));
    beanDef.setLazyInit(true);
    beanDef.setScope(SCOPE_SINGLETON);
    beanDef.setResource(resource);

    beanDef.setAttribute(IMPORT_BEAN_DEF_ATTR_NAME,
            new BeanReferenceInfo(serviceName, serviceIface, extractResourcePath(resource)));
}

From source file:com.griddynamics.banshun.config.xml.ExportBeanDefinitionParser.java

/**
 * Creates a factory method bean definition that just invokes
 * {@link Registry#export(ExportRef) export()} method on the specified registry bean.
 *
 * @param registryName//from   w ww .j ava2  s. c o m
 * @param exportRef The definition of the {@link ExportRef} bean.
 * @param resource The resource that this bean definition came from.
 */
private BeanDefinition defineExportFactoryBean(String registryName, BeanDefinition exportRef,
        Resource resource) {

    AbstractBeanDefinition beanDef = rootBeanDefinition(Void.class)
            .setFactoryMethod(Registry.EXPORT_METHOD_NAME).setScope(SCOPE_SINGLETON).setLazyInit(false)
            //.addDependsOn(exportBeanRef) TODO ?
            .getRawBeanDefinition();
    beanDef.setFactoryBeanName(registryName);
    beanDef.setConstructorArgumentValues(defineExportMethodArgs(exportRef));
    beanDef.setResource(resource);

    return beanDef;
}

From source file:com.seovic.validation.config.ValidationBeanDefinitionParser.java

@SuppressWarnings("unchecked")
private BeanDefinition parseValidator(Element element, ParserContext parserContext) {
    String parent = element.getAttribute("parent");
    if ("".equals(parent)) {
        parent = null;//  w  ww  . j ava 2s  .c  o  m
    }
    String test = element.getAttribute("test");
    String when = element.getAttribute("when");
    String validateAll = element.getAttribute("validate-all");
    String context = element.getAttribute("context");
    String includeElementsErrors = element.getAttribute("include-element-errors");

    MutablePropertyValues properties = new MutablePropertyValues();
    if (StringUtils.hasText(test)) {
        properties.addPropertyValue("test", test);
    }
    if (StringUtils.hasText(when)) {
        properties.addPropertyValue("when", when);
    }
    if (StringUtils.hasText(validateAll)) {
        properties.addPropertyValue("validateAll", validateAll);
    }
    if (StringUtils.hasText(context)) {
        properties.addPropertyValue("context", context);
    }
    if (StringUtils.hasText(includeElementsErrors)) {
        properties.addPropertyValue("includeElementsErrors", includeElementsErrors);
    }
    ManagedList nestedValidators = new ManagedList();
    ManagedList actions = new ManagedList();
    for (int i = 0; i < element.getChildNodes().getLength(); i++) {
        Node child = element.getChildNodes().item(i);
        if (child != null && child instanceof Element) {
            Element childElement = (Element) child;
            if ("message".equals(childElement.getLocalName())) {
                actions.add(parseErrorMessageAction(childElement, parserContext));
            } else {
                nestedValidators.add(parseAndRegisterValidator2(childElement, parserContext));
            }
        }
    }
    if (nestedValidators.size() > 0) {
        properties.addPropertyValue("validators", nestedValidators);
    }
    if (actions.size() > 0) {
        properties.addPropertyValue("actions", actions);
    }
    String className;
    if ("required".equals(element.getLocalName())) {
        className = RequiredValidator.class.getName();
    } else if ("condition".equals(element.getLocalName())) {
        className = ConditionValidator.class.getName();
    } else if ("validator".equals(element.getLocalName())) {
        className = element.getAttribute("type");
    } else {
        className = ValidatorGroup.class.getName();
    }
    AbstractBeanDefinition validatorDefinition;
    try {
        validatorDefinition = BeanDefinitionReaderUtils.createBeanDefinition(parent, className,
                parserContext.getReaderContext().getBeanClassLoader());
    } catch (ClassNotFoundException e) {
        throw new BeanCreationException("Error occured during creation of bean definition", e);
    }
    validatorDefinition.setResource(parserContext.getReaderContext().getResource());
    validatorDefinition.setSource(parserContext.extractSource(element));
    validatorDefinition.setPropertyValues(properties);
    validatorDefinition.setLazyInit(true);
    validatorDefinition.setScope("singleton");
    return validatorDefinition;
}

From source file:org.eclipse.gemini.blueprint.blueprint.config.internal.BlueprintParser.java

/**
 * Parse the bean definition itself, without regard to name or aliases. May return <code>null</code> if problems
 * occurred during the parse of the bean definition.
 *///from   w w  w  . j  a  v a2  s  .co  m
private AbstractBeanDefinition parseBeanDefinitionElement(Element ele, String beanName,
        BeanDefinition containingBean) {

    this.parseState.push(new BeanEntry(beanName));

    String className = null;
    if (ele.hasAttribute(BeanDefinitionParserDelegate.CLASS_ATTRIBUTE)) {
        className = ele.getAttribute(BeanDefinitionParserDelegate.CLASS_ATTRIBUTE).trim();
    }

    try {
        AbstractBeanDefinition beanDefinition = BeanDefinitionReaderUtils.createBeanDefinition(null, className,
                parserContext.getReaderContext().getBeanClassLoader());

        // some early validation
        String activation = ele.getAttribute(LAZY_INIT_ATTR);
        String scope = ele.getAttribute(BeanDefinitionParserDelegate.SCOPE_ATTRIBUTE);

        if (EAGER_INIT_VALUE.equals(activation) && BeanDefinition.SCOPE_PROTOTYPE.equals(scope)) {
            error("Prototype beans cannot be eagerly activated", ele);
        }

        // add marker to indicate that the scope was present
        if (StringUtils.hasText(scope)) {
            beanDefinition.setAttribute(DECLARED_SCOPE, Boolean.TRUE);
        }

        // parse attributes
        parseAttributes(ele, beanName, beanDefinition);

        // inner beans get a predefined scope in RFC 124
        if (containingBean != null) {
            beanDefinition.setLazyInit(true);
            beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
        }

        // parse description
        beanDefinition.setDescription(
                DomUtils.getChildElementValueByTagName(ele, BeanDefinitionParserDelegate.DESCRIPTION_ELEMENT));

        parseConstructorArgElements(ele, beanDefinition);
        parsePropertyElements(ele, beanDefinition);

        beanDefinition.setResource(parserContext.getReaderContext().getResource());
        beanDefinition.setSource(extractSource(ele));

        return beanDefinition;
    } 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;
}

From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java

/**
 * Parse the bean definition itself, without regard to name or aliases. May return
 * {@code null} if problems occurred during the parsing of the bean definition.
 */// w w w.  j ava  2s.  c o  m
@Nullable
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 {
        AbstractBeanDefinition bd = createBeanDefinition(className, parent);

        parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
        bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));

        parseMetaElements(ele, bd);
        parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
        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;
}