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

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

Introduction

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

Prototype

@Override
public boolean isSingleton() 

Source Link

Document

Return whether this a Singleton, with a single shared instance returned from all calls.

Usage

From source file:uk.org.ponder.rsac.support.BeanDefUtil.java

static RSACBeanInfo convertBeanDef(BeanDefinition origdef, String beanname,
        ConfigurableListableBeanFactory factory, MethodAnalyser abdAnalyser, BeanDefConverter converter) {
    RSACBeanInfo rbi = new RSACBeanInfo();
    AbstractBeanDefinition def = getMergedBeanDefinition(factory, beanname, origdef);
    MutablePropertyValues pvs = def.getPropertyValues();
    PropertyValue[] values = pvs.getPropertyValues();
    for (int j = 0; j < values.length; ++j) {
        PropertyValue thispv = values[j];
        Object beannames = BeanDefUtil.propertyValueToBeanName(thispv.getValue(), converter);
        boolean skip = false;
        // skip recording the dependency if it was unresolvable (some
        // unrecognised
        // type) or was a single-valued type referring to a static dependency.
        // NB - we now record ALL dependencies - bean-copying strategy
        // discontinued.
        if (beannames == null
        // || beannames instanceof String
        // && !blankcontext.containsBean((String) beannames)
        ) {/*  www . j  a  v a2s .c o m*/
            skip = true;
        }
        if (!skip) {
            rbi.recordDependency(thispv.getName(), beannames);
        }
    }
    // NB - illegal cast here is unavoidable.
    // Bit of a problem here with Spring flow - apparently the bean class
    // will NOT be set for a "factory-method" bean UNTIL it has been
    // instantiated
    // via the logic in AbstractAutowireCapableBeanFactory l.376:
    // protected BeanWrapper instantiateUsingFactoryMethod(
    AbstractBeanDefinition abd = def;
    rbi.factorybean = abd.getFactoryBeanName();
    rbi.factorymethod = abd.getFactoryMethodName();
    rbi.initmethod = abd.getInitMethodName();
    rbi.destroymethod = abd.getDestroyMethodName();
    rbi.islazyinit = abd.isLazyInit();
    rbi.dependson = abd.getDependsOn();
    rbi.issingleton = abd.isSingleton();
    rbi.isabstract = abd.isAbstract();
    rbi.aliases = factory.containsBeanDefinition(beanname) ? factory.getAliases(beanname)
            : StringArrayParser.EMPTY_STRINGL;
    if (abd.hasConstructorArgumentValues()) {
        ConstructorArgumentValues cav = abd.getConstructorArgumentValues();
        boolean hasgeneric = !cav.getGenericArgumentValues().isEmpty();
        boolean hasindexed = !cav.getIndexedArgumentValues().isEmpty();
        if (hasgeneric && hasindexed) {
            throw new UnsupportedOperationException("RSAC Bean " + beanname
                    + " has both indexed and generic constructor arguments, which is not supported");
        }
        if (hasgeneric) {
            List cvalues = cav.getGenericArgumentValues();
            rbi.constructorargvals = new ConstructorArgumentValues.ValueHolder[cvalues.size()];
            for (int i = 0; i < cvalues.size(); ++i) {
                rbi.constructorargvals[i] = (ConstructorArgumentValues.ValueHolder) cvalues.get(i);
            }
        } else if (hasindexed) {
            Map cvalues = cav.getIndexedArgumentValues();
            rbi.constructorargvals = new ConstructorArgumentValues.ValueHolder[cvalues.size()];
            for (int i = 0; i < cvalues.size(); ++i) {
                rbi.constructorargvals[i] = (ConstructorArgumentValues.ValueHolder) cvalues.get(new Integer(i));
            }
        }
    }
    if (rbi.factorymethod == null) {
        // Core Spring change at 2.0M5 - ALL bean classes are now irrevocably
        // lazy!!
        // Package org.springframework.beans
        // introduced lazy loading (and lazy validation) of bean classes in
        // standard bean factories and bean definition readers
        AccessMethod bcnaccess = abdAnalyser.getAccessMethod("beanClassName");
        if (bcnaccess != null) {
            String bcn = (String) bcnaccess.getChildObject(abd);
            if (bcn != null) {
                rbi.beanclass = ClassGetter.forName(bcn);
                if (rbi.beanclass == null) {
                    throw new IllegalArgumentException("Class name " + bcn + " for bean definition with name "
                            + beanname + " cannot be resolved");
                }
            }
        } else {
            // all right then BE like that! We'll work out the class later.
            // NB - beandef.getBeanClass() was eliminated around 1.2, we must
            // use the downcast even earlier now.
            rbi.beanclass = abd.getBeanClass();
        }
    }
    return rbi;
}

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

/**
 * Parse the BeanDefinition itself, without regard to name or aliases.
 *///from   w  w  w  .ja  v a  2  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);
    }
}