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

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

Introduction

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

Prototype

@Override
public void setFactoryMethodName(@Nullable String factoryMethodName) 

Source Link

Document

Specify a factory method, if any.

Usage

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

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

    String exportInterface = element.getAttribute(INTERFACE_ATTR);
    if (isBlank(exportInterface)) {
        return;/* ww w .  j  av  a2s. c o m*/
    }

    String externalName = element.getAttribute(ID_ATTR);
    if (isBlank(externalName)) {
        return;
    }

    String rootName = element.getAttribute(ROOT_ATTR);
    if (isBlank(rootName)) {
        rootName = DEFAULT_ROOT_FACTORY_NAME;
    }

    ConstructorArgumentValues constructorArgValues = new ConstructorArgumentValues();
    constructorArgValues.addGenericArgumentValue(externalName);
    constructorArgValues.addGenericArgumentValue(findClass(exportInterface, element.getAttribute(ID_ATTR),
            parserContext.getReaderContext().getResource().getDescription()));

    AbstractBeanDefinition beanDef = builder.getRawBeanDefinition();
    beanDef.setFactoryBeanName(rootName);
    beanDef.setFactoryMethodName("lookup");
    beanDef.setConstructorArgumentValues(constructorArgValues);
    beanDef.setLazyInit(true);
    beanDef.setScope(SCOPE_SINGLETON);
}

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 a v 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.xml.ExportBeanDefinitionParser.java

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();

    String exportInterface = element.getAttribute(INTERFACE_ATTR);
    if (isBlank(exportInterface)) {
        return;/*from www.ja  v a 2  s.  co m*/
    }

    String exportBeanRef = element.getAttribute(REF_ATTR);
    if (isBlank(exportBeanRef)) {
        return;
    }

    String rootName = element.getAttribute(ROOT_ATTR);
    if (isBlank(rootName)) {
        rootName = DEFAULT_ROOT_FACTORY_NAME;
    }

    String exportName = element.getAttribute(NAME_ATTR);
    if (isBlank(exportName)) {
        exportName = exportBeanRef;
    }

    String exportRefName = exportName + BEAN_NAME_SUFFIX;
    if (registry.containsBeanDefinition(exportRefName)) {
        throw new BeanCreationException("Registry already contains bean with name: " + exportRefName);
    }

    ConstructorArgumentValues exportBeanConstructorArgValues = new ConstructorArgumentValues();
    exportBeanConstructorArgValues.addGenericArgumentValue(exportName);
    exportBeanConstructorArgValues.addGenericArgumentValue(findClass(exportInterface, exportBeanRef,
            parserContext.getReaderContext().getResource().getDescription()));

    AbstractBeanDefinition exportBeanDef = rootBeanDefinition(ExportRef.class).getRawBeanDefinition();
    exportBeanDef.setConstructorArgumentValues(exportBeanConstructorArgValues);

    ConstructorArgumentValues voidBeanConstructorArgValues = new ConstructorArgumentValues();
    voidBeanConstructorArgValues.addGenericArgumentValue(exportBeanDef, ExportRef.class.getName());

    AbstractBeanDefinition voidBeanDef = rootBeanDefinition(Void.class).getRawBeanDefinition();
    voidBeanDef.setFactoryBeanName(rootName);
    voidBeanDef.setFactoryMethodName("export");
    voidBeanDef.setLazyInit(false);
    voidBeanDef.setScope(SCOPE_SINGLETON);
    voidBeanDef.setConstructorArgumentValues(voidBeanConstructorArgValues);
    //        voidBeanDefinition.setDependsOn(new String[] { exportBeanRef }); TODO ?

    registry.registerBeanDefinition(exportRefName, voidBeanDef);
}

From source file:hudson.util.spring.DefaultBeanConfiguration.java

public void setProperty(String property, Object newValue) {
    if (PARENT.equals(property)) {
        setParent(newValue);/*w  ww.  ja v  a  2  s  .com*/
    } else {
        AbstractBeanDefinition bd = getBeanDefinition();
        if (AUTOWIRE.equals(property)) {
            if (BY_NAME.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
            } else if (BY_TYPE.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
            } else if (Boolean.TRUE.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
            } else if (BY_CONSTRUCTOR.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
            }
        }
        // constructorArgs
        else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
            ConstructorArgumentValues cav = new ConstructorArgumentValues();
            List args = (List) newValue;
            for (Object e : args) {
                cav.addGenericArgumentValue(e);
            }
            bd.setConstructorArgumentValues(cav);
        }
        // destroyMethod
        else if (DESTROY_METHOD.equals(property)) {
            if (newValue != null)
                bd.setDestroyMethodName(newValue.toString());
        }
        // factoryBean
        else if (FACTORY_BEAN.equals(property)) {
            if (newValue != null)
                bd.setFactoryBeanName(newValue.toString());
        }
        // factoryMethod
        else if (FACTORY_METHOD.equals(property)) {
            if (newValue != null)
                bd.setFactoryMethodName(newValue.toString());
        }
        // initMethod
        else if (INIT_METHOD.equals(property)) {
            if (newValue != null)
                bd.setInitMethodName(newValue.toString());
        } else if (wrapper.isWritableProperty(property)) {

            wrapper.setPropertyValue(property, newValue);
        }
        // autowire
        else {
            super.setProperty(property, newValue);
        }
    }
}

From source file:org.blocks4j.feature.toggle.spring.scheme.FeatureToggleDefinitionParser.java

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) {
    AbstractBeanDefinition result = null;
    if (DomUtils.nodeNameEquals(element, TAG_TOGGLE_NAME)) {
        BeanDefinitionHolder holder = context.getDelegate().parseBeanDefinitionElement(element);
        result = (AbstractBeanDefinition) holder.getBeanDefinition();

        String feature = element.getAttribute(TAG_FEATURE_NAME);
        String commonInterfaceName = element.getAttribute(TAG_COMMON_INTERFACE_NAME);
        String onRefName = element.getAttribute(TAG_ON_REF_NAME);
        String offRefName = element.getAttribute(TAG_OFF_REF_NAME);

        if (StringUtils.isEmpty(onRefName)) {
            throw new FeatureToggleDefinitionParsingException("You must specify on-ref");
        }/*from   www  . j  av  a 2s. c  om*/
        if (StringUtils.isEmpty(offRefName)) {
            throw new FeatureToggleDefinitionParsingException("You must specify on-ref");
        }
        Class<?> commonInterface;
        try {
            commonInterface = Class.forName(commonInterfaceName);
        } catch (Exception e) {
            LOGGER.error("It was not possible to find interface of type '{}'", commonInterfaceName);
            throw new FeatureToggleDefinitionParsingException(
                    String.format("It was not possible to find interface of type '%s'", commonInterfaceName),
                    e);
        }
        String fbeanName = feature.concat("_" + this.randomUUID() + "_factory");
        this.buildNewFactoryBean(fbeanName, context, onRefName, offRefName, commonInterface, feature);
        result.setFactoryBeanName(fbeanName);
        result.setFactoryMethodName(TAG_TOGGLE_NAME);
        context.getRegistry()
                .registerBeanDefinition(TAG_TOGGLE_NAME.concat(Integer.toHexString(this.hashCode())), result);
    }
    return result;
}

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

/**
 * Apply the attributes of the given bean element to the given bean * definition.
 * @param ele bean declaration element//w  ww. j  ava2  s  . c  o  m
 * @param beanName bean name
 * @param containingBean containing bean definition
 * @return a bean definition initialized according to the bean element attributes
 */
public AbstractBeanDefinition parseBeanDefinitionAttributes(Element ele, String beanName,
        @Nullable BeanDefinition containingBean, AbstractBeanDefinition bd) {

    if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {
        error("Old 1.x 'singleton' attribute in use - upgrade to 'scope' declaration", ele);
    } else if (ele.hasAttribute(SCOPE_ATTRIBUTE)) {
        bd.setScope(ele.getAttribute(SCOPE_ATTRIBUTE));
    } else if (containingBean != null) {
        // Take default from containing bean in case of an inner bean definition.
        bd.setScope(containingBean.getScope());
    }

    if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) {
        bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE)));
    }

    String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
    if (DEFAULT_VALUE.equals(lazyInit)) {
        lazyInit = this.defaults.getLazyInit();
    }
    bd.setLazyInit(TRUE_VALUE.equals(lazyInit));

    String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE);
    bd.setAutowireMode(getAutowireMode(autowire));

    if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
        String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE);
        bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS));
    }

    String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE);
    if ("".equals(autowireCandidate) || DEFAULT_VALUE.equals(autowireCandidate)) {
        String candidatePattern = this.defaults.getAutowireCandidates();
        if (candidatePattern != null) {
            String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern);
            bd.setAutowireCandidate(PatternMatchUtils.simpleMatch(patterns, beanName));
        }
    } else {
        bd.setAutowireCandidate(TRUE_VALUE.equals(autowireCandidate));
    }

    if (ele.hasAttribute(PRIMARY_ATTRIBUTE)) {
        bd.setPrimary(TRUE_VALUE.equals(ele.getAttribute(PRIMARY_ATTRIBUTE)));
    }

    if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
        String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE);
        bd.setInitMethodName(initMethodName);
    } else if (this.defaults.getInitMethod() != null) {
        bd.setInitMethodName(this.defaults.getInitMethod());
        bd.setEnforceInitMethod(false);
    }

    if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
        String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE);
        bd.setDestroyMethodName(destroyMethodName);
    } else if (this.defaults.getDestroyMethod() != null) {
        bd.setDestroyMethodName(this.defaults.getDestroyMethod());
        bd.setEnforceDestroyMethod(false);
    }

    if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
        bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE));
    }
    if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) {
        bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE));
    }

    return bd;
}

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

/**
 * Parse the BeanDefinition itself, without regard to name or aliases.
 *//*from   www.j a v a2s  . c  om*/
protected BeanDefinition parseBeanDefinitionElement(Element ele, String beanName)
        throws BeanDefinitionStoreException {

    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 {
        ConstructorArgumentValues cargs = parseConstructorArgElements(ele, beanName);
        MutablePropertyValues pvs = parsePropertyElements(ele, beanName);

        AbstractBeanDefinition bd = BeanDefinitionReaderUtils.createBeanDefinition(className, parent, cargs,
                pvs, getBeanDefinitionReader().getBeanClassLoader());

        if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
            String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE);
            bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, BEAN_NAME_DELIMITERS));
        }

        if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
            bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE));
        }
        if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) {
            bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE));
        }

        String dependencyCheck = ele.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE);
        if (DEFAULT_VALUE.equals(dependencyCheck)) {
            dependencyCheck = getDefaultDependencyCheck();
        }
        bd.setDependencyCheck(getDependencyCheck(dependencyCheck));

        String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE);
        if (DEFAULT_VALUE.equals(autowire)) {
            autowire = getDefaultAutowire();
        }
        bd.setAutowireMode(getAutowireMode(autowire));

        if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
            String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE);
            if (!"".equals(initMethodName)) {
                bd.setInitMethodName(initMethodName);
            }
        } else {
            if (getDefaultInitMethod() != null) {
                bd.setInitMethodName(getDefaultInitMethod());
                bd.setEnforceInitMethod(false);
            }
        }

        if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
            String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE);
            if (!"".equals(destroyMethodName)) {
                bd.setDestroyMethodName(destroyMethodName);
            }
        } else {
            if (getDefaultDestroyMethod() != null) {
                bd.setDestroyMethodName(getDefaultDestroyMethod());
                bd.setEnforceDestroyMethod(false);
            }
        }

        parseLookupOverrideSubElements(ele, beanName, bd.getMethodOverrides());
        parseReplacedMethodSubElements(ele, beanName, bd.getMethodOverrides());

        bd.setResourceDescription(getResource().getDescription());

        if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) {
            bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE)));
        }

        if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {
            bd.setSingleton(TRUE_VALUE.equals(ele.getAttribute(SINGLETON_ATTRIBUTE)));
        }

        String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
        if (DEFAULT_VALUE.equals(lazyInit) && bd.isSingleton()) {
            // Just apply default to singletons, as lazy-init has no meaning for prototypes.
            lazyInit = getDefaultLazyInit();
        }
        bd.setLazyInit(TRUE_VALUE.equals(lazyInit));

        return bd;
    }

    catch (BeanDefinitionStoreException ex) {
        throw ex;
    } catch (ClassNotFoundException ex) {
        throw new BeanDefinitionStoreException(getResource(), beanName,
                "Bean class [" + className + "] not found", ex);
    } catch (NoClassDefFoundError err) {
        throw new BeanDefinitionStoreException(getResource(), beanName,
                "Class that bean class [" + className + "] depends on not found", err);
    } catch (Throwable ex) {
        throw new BeanDefinitionStoreException(getResource(), beanName,
                "Unexpected failure during bean definition parsing", ex);
    }
}