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

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

Introduction

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

Prototype

@Override
public void setDependsOn(@Nullable String... dependsOn) 

Source Link

Document

Set the names of the beans that this bean depends on being initialized.

Usage

From source file:org.xeustechnologies.jcl.spring.JclBeanDefinitionDecorator.java

@SuppressWarnings("unchecked")
private void createDependencyOnJcl(Node node, BeanDefinitionHolder holder, ParserContext parserContext) {
    AbstractBeanDefinition definition = ((AbstractBeanDefinition) holder.getBeanDefinition());
    String jclRef = node.getAttributes().getNamedItem(JCL_REF).getNodeValue();

    if (!parserContext.getRegistry().containsBeanDefinition(JCL_FACTORY)) {
        BeanDefinitionBuilder initializer = BeanDefinitionBuilder.rootBeanDefinition(JclObjectFactory.class,
                JCL_FACTORY_CONSTRUCTOR);
        parserContext.getRegistry().registerBeanDefinition(JCL_FACTORY, initializer.getBeanDefinition());
    }//from w  w  w .  j  a v  a  2 s  .c o  m

    if (parserContext.getRegistry().containsBeanDefinition(jclRef)) {
        String[] dependsOn = definition.getDependsOn();
        if (dependsOn == null) {
            dependsOn = new String[] { jclRef, JCL_FACTORY };
        } else {
            List dependencies = new ArrayList(Arrays.asList(dependsOn));
            dependencies.add(jclRef);
            dependencies.add(JCL_FACTORY);
            dependsOn = (String[]) dependencies.toArray(new String[0]);
        }
        definition.setDependsOn(dependsOn);
    } else
        throw new JclException("JCL Bean definition " + jclRef + "not found");
}

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//from  ww w . j  a  v  a2  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.
 *///  w  w  w  . ja  v  a2  s  . com
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);
    }
}