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

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

Introduction

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

Prototype

void setAutowireCandidate(boolean autowireCandidate);

Source Link

Document

Set whether this bean is a candidate for getting autowired into some other bean.

Usage

From source file:org.constretto.spring.EnvironmentAnnotationConfigurer.java

@SuppressWarnings("unchecked")
private void removeNonAnnotatedBeansFromAutowireForType(Class lookupClass,
        ConfigurableListableBeanFactory configurableListableBeanFactory) throws ClassNotFoundException {
    List<String> beanNames = new ArrayList<String>();
    Class[] interfaces = lookupClass.getInterfaces();
    for (Class anInterface : interfaces) {
        beanNames.addAll(asList(BeanFactoryUtils
                .beanNamesForTypeIncludingAncestors(configurableListableBeanFactory, anInterface)));
    }//from   w ww  .ja  v  a 2 s.  co  m
    List<BeanDefinition> potentialMatches = new ArrayList<BeanDefinition>();
    for (String beanName : beanNames) {
        BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition(beanName);
        Class beanClass = Class.forName(beanDefinition.getBeanClassName());
        beanDefinition.setAttribute(INCLUDE_IN_COLLECTIONS, beanClass.getInterfaces());
        Environment environmentAnnotation = findEnvironmentAnnotation(beanClass);
        if (environmentAnnotation == null) {
            beanDefinition.setAutowireCandidate(false);
        } else {
            potentialMatches.add(beanDefinition);
        }
    }
    if (potentialMatches.size() == 1) {
        potentialMatches.get(0).setAutowireCandidate(true);
    } else {
        List<BeanDefinition> highestPriorityBeans = new ArrayList<BeanDefinition>();
        for (BeanDefinition potentialMatch : potentialMatches) {
            if (potentialMatch.isAutowireCandidate()) {
                potentialMatch.setAutowireCandidate(false);
                highestPriorityBeans = prioritizeBeans(potentialMatch, highestPriorityBeans);
            }
        }
        if (highestPriorityBeans.size() == 1) {
            highestPriorityBeans.get(0).setAutowireCandidate(true);
        } else {
            List<String> equalPriorityBeans = new ArrayList<String>();
            for (BeanDefinition highestPriorityBean : highestPriorityBeans) {
                equalPriorityBeans.add(highestPriorityBean.getBeanClassName());
            }
            throw new ConstrettoException("More than one bean with the class or interface + ["
                    + lookupClass.getSimpleName()
                    + "] registered with same tag. Could not resolve priority. To fix this, remove one of the following beans "
                    + equalPriorityBeans.toString());
        }
    }
}

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 {/* w w w. j  ava 2  s.  co m*/
        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:org.constretto.spring.EnvironmentAnnotationConfigurer.java

@SuppressWarnings("unchecked")
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory)
        throws BeansException {

    if (!(configurableListableBeanFactory instanceof DefaultListableBeanFactory)) {
        throw new IllegalStateException(
                "EnvironmentAnnotationConfigurer needs to operate on a DefaultListableBeanFactory");
    }/*from w w  w. ja  v  a  2s .co m*/
    DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory;
    defaultListableBeanFactory.setAutowireCandidateResolver(new ConstrettoAutowireCandidateResolver());
    String[] beanNames = configurableListableBeanFactory.getBeanDefinitionNames();
    int lowestDiscoveredPriority = Integer.MAX_VALUE;
    for (String beanName : beanNames) {
        BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition(beanName);
        if (beanDefinition.getBeanClassName() != null) {
            try {
                Class beanClass = Class.forName(beanDefinition.getBeanClassName());
                Environment environmentAnnotation = findEnvironmentAnnotation(beanClass);
                if (environmentAnnotation != null) {
                    if (!assemblyContextResolver.getAssemblyContext().isEmpty()) {
                        boolean autowireCandidate = decideIfAutowireCandiate(beanName, environmentAnnotation);
                        beanDefinition.setAutowireCandidate(autowireCandidate);
                        if (autowireCandidate) {
                            removeNonAnnotatedBeansFromAutowireForType(beanClass,
                                    configurableListableBeanFactory);
                        }
                    } else {
                        beanDefinition.setAutowireCandidate(false);
                    }
                }
            } catch (ClassNotFoundException e) {
                beanDefinition.setAutowireCandidate(false);
            }
        }
    }
}