Example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory getParentBeanFactory

List of usage examples for org.springframework.beans.factory.config ConfigurableListableBeanFactory getParentBeanFactory

Introduction

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

Prototype

@Nullable
BeanFactory getParentBeanFactory();

Source Link

Document

Return the parent bean factory, or null if there is none.

Usage

From source file:org.nuunframework.spring.SpringModule.java

@SuppressWarnings("unchecked")
private void bindFromApplicationContext() {
    boolean debugEnabled = logger.isDebugEnabled();

    ConfigurableListableBeanFactory currentBeanFactory = this.beanFactory;
    do {//w w w  . ja v  a 2  s . co  m
        for (String beanName : currentBeanFactory.getBeanDefinitionNames()) {
            BeanDefinition beanDefinition = currentBeanFactory.getMergedBeanDefinition(beanName);
            if (!beanDefinition.isAbstract()) {
                Class<?> beanClass = classFromString(beanDefinition.getBeanClassName());
                if (beanClass == null) {
                    logger.warn("Cannot bind spring bean " + beanName + " because its class "
                            + beanDefinition.getBeanClassName() + " failed to load");
                    return;
                }

                SpringBeanDefinition springBeanDefinition = new SpringBeanDefinition(beanName,
                        currentBeanFactory);

                // Adding bean with its base type
                addBeanDefinition(beanClass, springBeanDefinition);

                // Adding bean with its parent type if enabled
                Class<?> parentClass = beanClass.getSuperclass();
                if (parentClass != null && parentClass != Object.class)
                    addBeanDefinition(parentClass, springBeanDefinition);

                // Adding bean with its immediate interfaces if enabled
                for (Class<?> i : beanClass.getInterfaces())
                    addBeanDefinition(i, springBeanDefinition);
            }
        }
        BeanFactory factory = currentBeanFactory.getParentBeanFactory();
        if (factory != null) {
            if (factory instanceof ConfigurableListableBeanFactory)
                currentBeanFactory = (ConfigurableListableBeanFactory) factory;
            else {
                logger.info(
                        "Cannot go up further in the bean factory hierarchy, parent bean factory doesn't implement ConfigurableListableBeanFactory");
                currentBeanFactory = null;
            }
        } else
            currentBeanFactory = null;
    } while (currentBeanFactory != null);

    for (Map.Entry<Class<?>, Map<String, SpringBeanDefinition>> entry : this.beanDefinitions.entrySet()) {
        Class<?> type = entry.getKey();
        Map<String, SpringBeanDefinition> definitions = entry.getValue();

        // Bind by name for each bean of this type and by type if there is no ambiguity
        for (SpringBeanDefinition candidate : definitions.values()) {
            if (debugEnabled)
                logger.info("Binding spring bean " + candidate.getName() + " by name and type "
                        + type.getCanonicalName());

            bind(type).annotatedWith(Names.named(candidate.getName())).toProvider(
                    new ByNameSpringContextProvider(type, candidate.getName(), candidate.getBeanFactory()));
        }
    }
}

From source file:io.nuun.plugin.spring.SpringModule.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void bindFromApplicationContext() {
    boolean debugEnabled = logger.isDebugEnabled();

    ConfigurableListableBeanFactory currentBeanFactory = beanFactory;
    do {/*from   w w  w  . j a  v a2  s  .c om*/
        for (String beanName : currentBeanFactory.getBeanDefinitionNames()) {
            BeanDefinition beanDefinition = currentBeanFactory.getMergedBeanDefinition(beanName);
            if (!beanDefinition.isAbstract()) {
                Class<?> beanClass = classFromString(beanDefinition.getBeanClassName());
                if (beanClass == null) {
                    logger.warn("Cannot bind spring bean " + beanName + " because its class "
                            + beanDefinition.getBeanClassName() + " failed to load");
                    return;
                }

                SpringBeanDefinition springBeanDefinition = new SpringBeanDefinition(beanName,
                        currentBeanFactory);

                // Adding bean with its base type
                addBeanDefinition(beanClass, springBeanDefinition);

                // Adding bean with its parent type if enabled
                Class<?> parentClass = beanClass.getSuperclass();
                if (parentClass != null && parentClass != Object.class) {
                    addBeanDefinition(parentClass, springBeanDefinition);
                }

                // Adding bean with its immediate interfaces if enabled
                for (Class<?> i : beanClass.getInterfaces()) {
                    addBeanDefinition(i, springBeanDefinition);
                }
            }
        }
        BeanFactory factory = currentBeanFactory.getParentBeanFactory();
        if (factory != null) {
            if (factory instanceof ConfigurableListableBeanFactory) {
                currentBeanFactory = (ConfigurableListableBeanFactory) factory;
            } else {
                logger.info(
                        "Cannot go up further in the bean factory hierarchy, parent bean factory doesn't implement ConfigurableListableBeanFactory");
                currentBeanFactory = null;
            }
        } else {
            currentBeanFactory = null;
        }
    } while (currentBeanFactory != null);

    for (Map.Entry<Class<?>, Map<String, SpringBeanDefinition>> entry : beanDefinitions.entrySet()) {
        Class<?> type = entry.getKey();
        Map<String, SpringBeanDefinition> definitions = entry.getValue();

        // Bind by name for each bean of this type and by type if there is no ambiguity
        for (SpringBeanDefinition candidate : definitions.values()) {
            if (debugEnabled) {
                logger.info("Binding spring bean " + candidate.getName() + " by name and type "
                        + type.getCanonicalName());
            }

            bind(type).annotatedWith(Names.named(candidate.getName())).toProvider(
                    new ByNameSpringContextProvider(type, candidate.getName(), candidate.getBeanFactory()));
        }
    }
}

From source file:org.seedstack.spring.internal.SpringModule.java

@SuppressWarnings("unchecked")
private void bindFromApplicationContext() {
    boolean debugEnabled = LOGGER.isDebugEnabled();

    ConfigurableListableBeanFactory currentBeanFactory = this.beanFactory;
    do {/* w w w . j a  v  a  2 s . c o  m*/
        for (String beanName : currentBeanFactory.getBeanDefinitionNames()) {
            BeanDefinition beanDefinition = currentBeanFactory.getMergedBeanDefinition(beanName);
            if (!beanDefinition.isAbstract()) {
                Class<?> beanClass;
                try {
                    beanClass = Class.forName(beanDefinition.getBeanClassName());
                } catch (ClassNotFoundException e) {
                    LOGGER.warn("Cannot bind spring bean " + beanName + " because its class "
                            + beanDefinition.getBeanClassName() + " failed to load", e);
                    continue;
                }

                // FactoryBean special case: retrieve it and query for the object type it creates
                if (FactoryBean.class.isAssignableFrom(beanClass)) {
                    beanClass = ((FactoryBean) currentBeanFactory.getBean('&' + beanName)).getObjectType();
                    if (beanClass == null) {
                        LOGGER.warn("Cannot bind spring bean " + beanName
                                + " because its factory bean cannot determine its class");
                        continue;
                    }
                }

                SpringBeanDefinition springBeanDefinition = new SpringBeanDefinition(beanName,
                        currentBeanFactory);

                // Adding bean with its base type
                addBeanDefinition(beanClass, springBeanDefinition);

                // Adding bean with its parent type if enabled
                Class<?> parentClass = beanClass.getSuperclass();
                if (parentClass != null && parentClass != Object.class) {
                    addBeanDefinition(parentClass, springBeanDefinition);
                }

                // Adding bean with its immediate interfaces if enabled
                for (Class<?> i : beanClass.getInterfaces()) {
                    addBeanDefinition(i, springBeanDefinition);
                }
            }
        }
        BeanFactory factory = currentBeanFactory.getParentBeanFactory();
        if (factory != null) {
            if (factory instanceof ConfigurableListableBeanFactory) {
                currentBeanFactory = (ConfigurableListableBeanFactory) factory;
            } else {
                LOGGER.info(
                        "Cannot go up further in the bean factory hierarchy, parent bean factory doesn't implement ConfigurableListableBeanFactory");
                currentBeanFactory = null;
            }
        } else {
            currentBeanFactory = null;
        }
    } while (currentBeanFactory != null);

    for (Map.Entry<Class<?>, Map<String, SpringBeanDefinition>> entry : this.beanDefinitions.entrySet()) {
        Class<?> type = entry.getKey();
        Map<String, SpringBeanDefinition> definitions = entry.getValue();

        // Bind by name for each bean of this type and by type if there is no ambiguity
        for (SpringBeanDefinition candidate : definitions.values()) {
            if (debugEnabled) {
                LOGGER.info("Binding spring bean " + candidate.getName() + " by name and type "
                        + type.getCanonicalName());
            }

            bind(type).annotatedWith(Names.named(candidate.getName()))
                    .toProvider(new SpringBeanProvider(type, candidate.getName(), candidate.getBeanFactory()));
        }
    }
}

From source file:org.springframework.boot.autoconfigure.condition.AbstractOnBeanCondition.java

protected boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata, List<String> beanClasses,
        List<String> beanNames) throws LinkageError {

    String checking = ConditionLogUtils.getPrefix(this.logger, metadata);

    SearchStrategy search = (SearchStrategy) metadata.getAnnotationAttributes(annotationClass().getName())
            .get("search");
    search = search == null ? SearchStrategy.ALL : search;

    boolean considerHierarchy = search == SearchStrategy.ALL;
    boolean parentOnly = search == SearchStrategy.PARENTS;

    List<String> beanClassesFound = new ArrayList<String>();
    List<String> beanNamesFound = new ArrayList<String>();

    // eagerInit set to false to prevent early instantiation (some
    // factory beans will not be able to determine their object type at this
    // stage, so those are not eligible for matching this condition)
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (parentOnly) {
        BeanFactory parent = beanFactory.getParentBeanFactory();
        if (!(parent instanceof ConfigurableListableBeanFactory)) {
            throw new IllegalStateException(
                    "Cannot use parentOnly if parent is not ConfigurableListableBeanFactory");
        }//from  ww w  .  j  a  v  a2 s  .  c om
        beanFactory = (ConfigurableListableBeanFactory) parent;
    }
    for (String beanClass : beanClasses) {
        try {
            Class<?> type = ClassUtils.forName(beanClass, context.getClassLoader());
            String[] beans = (considerHierarchy
                    ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, type, false, false)
                    : beanFactory.getBeanNamesForType(type, false, false));
            if (beans.length != 0) {
                beanClassesFound.add(beanClass);
            }
        } catch (ClassNotFoundException ex) {
            // swallow exception and continue
        }
    }
    for (String beanName : beanNames) {
        if (considerHierarchy ? beanFactory.containsBean(beanName) : beanFactory.containsLocalBean(beanName)) {
            beanNamesFound.add(beanName);
        }
    }

    boolean result = evaluate(beanClassesFound, beanNamesFound);
    if (this.logger.isDebugEnabled()) {
        logFoundResults(checking, "class", beanClasses, beanClassesFound);
        logFoundResults(checking, "name", beanNames, beanClassesFound);
        this.logger.debug(checking + "Match result is: " + result);
    }
    return result;
}

From source file:org.springframework.flex.config.RemotingAnnotationPostProcessor.java

/**
 * Helper that searches the BeanFactory for beans annotated with @RemotingDestination, being careful not to force
 * eager creation of the beans if it can be avoided.
 * /*from  w  ww.j av a  2 s .co m*/
 * @param beanFactory the BeanFactory to search
 * @return a set of collected RemotingDestinationMetadata
 */
private Set<RemotingDestinationMetadata> findRemotingDestinations(ConfigurableListableBeanFactory beanFactory) {
    Set<RemotingDestinationMetadata> remotingDestinations = new HashSet<RemotingDestinationMetadata>();
    Set<String> beanNames = new HashSet<String>();
    beanNames.addAll(Arrays.asList(beanFactory.getBeanDefinitionNames()));
    if (beanFactory.getParentBeanFactory() instanceof ListableBeanFactory) {
        beanNames.addAll(Arrays
                .asList(((ListableBeanFactory) beanFactory.getParentBeanFactory()).getBeanDefinitionNames()));
    }
    for (String beanName : beanNames) {
        if (beanName.startsWith("scopedTarget.")) {
            continue;
        }
        RemotingDestination remotingDestination = null;
        BeanDefinition bd = beanFactory.getMergedBeanDefinition(beanName);
        if (bd.isAbstract() || bd.isLazyInit()) {
            continue;
        }
        if (bd instanceof AbstractBeanDefinition) {
            AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
            if (abd.hasBeanClass()) {
                Class<?> beanClass = abd.getBeanClass();
                remotingDestination = AnnotationUtils.findAnnotation(beanClass, RemotingDestination.class);
                if (remotingDestination != null) {
                    remotingDestinations
                            .add(new RemotingDestinationMetadata(remotingDestination, beanName, beanClass));
                    continue;
                }
            }
        }
        Class<?> handlerType = beanFactory.getType(beanName);
        if (handlerType != null) {
            remotingDestination = AnnotationUtils.findAnnotation(handlerType, RemotingDestination.class);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Could not get type of bean '" + beanName + "' from bean factory.");
            }
        }
        if (remotingDestination != null) {
            remotingDestinations
                    .add(new RemotingDestinationMetadata(remotingDestination, beanName, handlerType));
        }
    }
    return remotingDestinations;
}