Example usage for org.springframework.beans.factory.config BeanDefinition isPrimary

List of usage examples for org.springframework.beans.factory.config BeanDefinition isPrimary

Introduction

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

Prototype

boolean isPrimary();

Source Link

Document

Return whether this bean is a primary autowire candidate.

Usage

From source file:ch.nydi.spring.context.support.PrimaryResolverListableBeanFactory.java

@Override
public <T> T getBean(Class<T> requiredType) throws BeansException {
    Assert.notNull(requiredType, "Required type must not be null");
    String[] beanNames = getBeanNamesForType(requiredType);
    String primaryCandidate = null;
    if (beanNames.length > 1) {
        final ArrayList<String> autowireCandidates = new ArrayList<String>();

        for (final String beanName : beanNames) {
            final BeanDefinition beanDefinition = getBeanDefinition(beanName);
            if (beanDefinition.isAutowireCandidate()) {
                autowireCandidates.add(beanName);
                if (beanDefinition.isPrimary()) {
                    primaryCandidate = beanName;
                }/*from   w  w  w.j a v a  2 s.c  o m*/
            }
        }
        if (autowireCandidates.size() > 0) {
            beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
        }
    }
    if (beanNames.length == 1) {
        return getBean(beanNames[0], requiredType);
    } else if (beanNames.length > 1) { // more than one bean defined, lookup primary candidate
        if (primaryCandidate != null) {
            return getBean(primaryCandidate, requiredType);
        }
        throw new NoSuchBeanDefinitionException(requiredType, "expected single bean but found "
                + beanNames.length + ": " + StringUtils.arrayToCommaDelimitedString(beanNames));
    } else if (beanNames.length == 0 && getParentBeanFactory() != null) {
        return getParentBeanFactory().getBean(requiredType);
    } else {
        throw new NoSuchBeanDefinitionException(requiredType, "expected single bean but found "
                + beanNames.length + ": " + StringUtils.arrayToCommaDelimitedString(beanNames));
    }
}

From source file:net.easysmarthouse.service.context.ProxiedResolverGenericXmlApplicationContext.java

@Override
public <T extends Object> T getBean(Class<T> requiredType) throws BeansException {
    Assert.notNull(requiredType, "Required type must not be null");
    String[] beanNames = getBeanNamesForType(requiredType);
    String primaryCandidate = null;

    if (beanNames.length > 1) {
        ArrayList<String> autowireCandidates = new ArrayList<String>();

        for (String beanName : beanNames) {
            BeanDefinition beanDefinition = getBeanDefinition(beanName);
            if (beanDefinition.isAutowireCandidate()) {
                autowireCandidates.add(beanName);
                if (beanDefinition.isPrimary()) {
                    primaryCandidate = beanName;
                }//from   w w w.ja v  a  2 s  .  co  m
            }
        }

        for (String autowireCandidate : autowireCandidates) {
            if (autowireCandidates.contains(autowireCandidate + "Proxied")) {
                primaryCandidate = autowireCandidate;
            }
        }

        if (autowireCandidates.size() > 0) {
            beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
        }
    }

    if (beanNames.length == 1) {
        return getBean(beanNames[0], requiredType);

    } else if (beanNames.length > 1) {
        // more than one bean defined, lookup primary candidate
        if (primaryCandidate != null) {
            return getBean(primaryCandidate, requiredType);
        }
        throw new NoSuchBeanDefinitionException(requiredType, "expected single bean but found "
                + beanNames.length + ": " + StringUtils.arrayToCommaDelimitedString(beanNames));

    } else if (beanNames.length == 0 && getParentBeanFactory() != null) {
        return getParentBeanFactory().getBean(requiredType);

    } else {
        throw new NoSuchBeanDefinitionException(requiredType, "expected single bean but found "
                + beanNames.length + ": " + StringUtils.arrayToCommaDelimitedString(beanNames));

    }
}

From source file:org.jdal.aop.SerializableProxyUtils.java

public static BeanDefinitionHolder createSerializableProxy(BeanDefinitionHolder definition,
        BeanDefinitionRegistry registry, boolean proxyTargetClass) {

    String originalBeanName = definition.getBeanName();
    BeanDefinition targetDefinition = definition.getBeanDefinition();

    // Create a scoped proxy definition for the original bean name,
    // "hiding" the target bean in an internal target definition.
    RootBeanDefinition proxyDefinition = new RootBeanDefinition(SerializableProxyFactoryBean.class);
    proxyDefinition.setOriginatingBeanDefinition(definition.getBeanDefinition());
    proxyDefinition.setSource(definition.getSource());
    proxyDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

    String targetBeanName = getTargetBeanName(originalBeanName);
    proxyDefinition.getPropertyValues().add("targetBeanName", targetBeanName);

    if (proxyTargetClass) {
        targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
    } else {//from www  . j  ava 2 s.c  om
        proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE);
    }

    // Copy autowire settings from original bean definition.
    proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
    proxyDefinition.setPrimary(targetDefinition.isPrimary());
    if (targetDefinition instanceof AbstractBeanDefinition) {
        proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
    }

    // Set singleton property of FactoryBean
    proxyDefinition.getPropertyValues().add("singleton", !targetDefinition.isPrototype());

    // The target bean should be ignored in favor of the scoped proxy.
    targetDefinition.setAutowireCandidate(false);
    targetDefinition.setPrimary(false);

    // Register the target bean as separate bean in the factory.
    registry.registerBeanDefinition(targetBeanName, targetDefinition);

    // Return the scoped proxy definition as primary bean definition
    // (potentially an inner bean).
    return new BeanDefinitionHolder(proxyDefinition, originalBeanName, definition.getAliases());
}

From source file:lodsve.core.condition.OnBeanCondition.java

private List<String> getPrimaryBeans(ConfigurableListableBeanFactory beanFactory, List<String> beanNames) {
    List<String> primaryBeans = new ArrayList<String>();
    for (String beanName : beanNames) {
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
        if (beanDefinition != null && beanDefinition.isPrimary()) {
            primaryBeans.add(beanName);/*  ww  w  . j  a va 2 s.  c o  m*/
        }
    }
    return primaryBeans;
}

From source file:ro.pippo.spring.AnnotationFieldValueProvider.java

private String getBeanNameOfClass(Class<?> clazz) {
    // get the list of all possible matching beans
    List<String> names = Arrays
            .asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, clazz));

    // filter out beans that are not candidates for auto wiring
    if (applicationContext instanceof AbstractApplicationContext) {
        Iterator<String> iterator = names.iterator();
        while (iterator.hasNext()) {
            String possibility = iterator.next();
            BeanDefinition beanDef = getBeanDefinition(
                    ((AbstractApplicationContext) applicationContext).getBeanFactory(), possibility);
            if (BeanFactoryUtils.isFactoryDereference(possibility) || possibility.startsWith("scopedTarget.")
                    || (beanDef != null && !beanDef.isAutowireCandidate())) {
                iterator.remove();/*from  ww  w .j av  a2  s .  co  m*/
            }
        }
    }

    if (names.isEmpty()) {
        return null;
    }

    if (names.size() > 1) {
        if (applicationContext instanceof AbstractApplicationContext) {
            List<String> primaries = new ArrayList<>();
            for (String name : names) {
                BeanDefinition beanDef = getBeanDefinition(
                        ((AbstractApplicationContext) applicationContext).getBeanFactory(), name);
                if (beanDef instanceof AbstractBeanDefinition) {
                    if (beanDef.isPrimary()) {
                        primaries.add(name);
                    }
                }
            }
            if (primaries.size() == 1) {
                return primaries.get(0);
            }
        }
        StringBuilder message = new StringBuilder();
        message.append("More than one bean of type [");
        message.append(clazz.getName());
        message.append("] found, you have to specify the name of the bean ");
        message.append("(@Named(\"foo\") if using @javax.inject classes) in order to resolve this conflict. ");
        message.append("Matched beans: ");
        message.append(names);
        throw new PippoRuntimeException(message.toString());
    }

    return names.get(0);
}

From source file:org.artifactory.spring.ArtifactoryApplicationContext.java

@Override
@SuppressWarnings("unchecked")
public <T> T beanForType(Class<T> type) {
    //No sync needed. Sync is done on write, so in the worst case we might end up with
    //a bean with the same value, which is fine
    T bean = (T) beansForType.get(type);
    if (bean == null) {
        Map<String, T> beans = getBeansOfType(type);
        if (beans.isEmpty()) {
            throw new RuntimeException("Could not find bean of type '" + type.getName() + "'.");
        }//from w ww . ja v  a2 s.  c om

        bean = beans.values().iterator().next(); // default to the first bean encountered
        if (beans.size() > 1) {
            // prefer beans marked as primary
            for (Map.Entry<String, T> beanEntry : beans.entrySet()) {
                BeanDefinition beanDefinition = getBeanFactory().getBeanDefinition(beanEntry.getKey());
                if (beanDefinition != null && beanDefinition.isPrimary()) {
                    bean = beanEntry.getValue();
                }
            }
        }
    }
    beansForType.put(type, bean);
    return bean;
}