Example usage for org.springframework.beans.factory BeanFactoryUtils isFactoryDereference

List of usage examples for org.springframework.beans.factory BeanFactoryUtils isFactoryDereference

Introduction

In this page you can find the example usage for org.springframework.beans.factory BeanFactoryUtils isFactoryDereference.

Prototype

public static boolean isFactoryDereference(@Nullable String name) 

Source Link

Document

Return whether the given name is a factory dereference (beginning with the factory dereference prefix).

Usage

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   w  w w.ja  va2 s . c  o  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.xaloon.wicket.component.inject.spring.CdiProxyFieldValueFactory.java

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

    // filter out beans that are not candidates for autowiring
    Iterator<String> it = names.iterator();
    while (it.hasNext()) {
        final String possibility = it.next();
        if (ctx instanceof AbstractApplicationContext) {
            BeanDefinition beanDef = getBeanDefinition(((AbstractApplicationContext) ctx).getBeanFactory(),
                    possibility);//w  w  w .ja  va 2s  . c  om
            if (BeanFactoryUtils.isFactoryDereference(possibility) || possibility.startsWith("scopedTarget.")
                    || !beanDef.isAutowireCandidate()) {
                it.remove();
            }
        }
    }

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

From source file:org.springframework.beans.factory.support.AbstractBeanFactory.java

/**
 * Return whether the given name is a factory dereference
 * (beginning with the factory dereference prefix).
 * @see BeanFactory#FACTORY_BEAN_PREFIX//w ww . java  2s .  c  om
 * @see org.springframework.beans.factory.BeanFactoryUtils#isFactoryDereference
 */
protected boolean isFactoryDereference(String name) {
    return BeanFactoryUtils.isFactoryDereference(name);
}