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

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

Introduction

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

Prototype

public static <T> Map<String, T> beansOfTypeIncludingAncestors(ListableBeanFactory lbf, Class<T> type)
        throws BeansException 

Source Link

Document

Return all beans of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory.

Usage

From source file:org.openehealth.ipf.commons.map.config.CustomMappingsConfigurer.java

/**
 * lookup for the specific {@link CustomMappings} objects inside
 * the given beanFactory//w  w  w.j  a v a 2  s .co  m
 * 
 * @see SpringConfigurer
 */
@SuppressWarnings("unchecked")
@Override
public Collection<CustomMappings> lookup(ListableBeanFactory source) {
    return BeanFactoryUtils.beansOfTypeIncludingAncestors(source, CustomMappings.class).values();
}

From source file:org.openehealth.ipf.modules.hl7.config.CustomModelClassFactoryConfigurer.java

@Override
public Collection<CustomModelClasses> lookup(ListableBeanFactory source) {
    return BeanFactoryUtils.beansOfTypeIncludingAncestors(source, CustomModelClasses.class).values();
}

From source file:org.openehealth.ipf.platform.camel.core.config.CustomRouteBuilderConfigurer.java

@Override
public Collection<CustomRouteBuilder> lookup(ListableBeanFactory source) {
    List list = new ArrayList(
            BeanFactoryUtils.beansOfTypeIncludingAncestors(source, CustomRouteBuilder.class).values());
    Collections.sort(list);//w  ww  .  jav a 2s. c  o  m
    return list;
}

From source file:org.springframework.cloud.stream.reactive.StreamEmitterAnnotationBeanPostProcessor.java

@Override
@SuppressWarnings("unchecked")
public void afterPropertiesSet() throws Exception {
    Map<String, StreamListenerParameterAdapter> parameterAdapterMap = BeanFactoryUtils
            .beansOfTypeIncludingAncestors(this.applicationContext, StreamListenerParameterAdapter.class);
    parameterAdapterMap.values().iterator().forEachRemaining(this.streamListenerParameterAdapters::add);
    Map<String, StreamListenerResultAdapter> resultAdapterMap = BeanFactoryUtils
            .beansOfTypeIncludingAncestors(this.applicationContext, StreamListenerResultAdapter.class);
    this.streamListenerResultAdapters.add(new MessageChannelStreamListenerResultAdapter());
    resultAdapterMap.values().iterator().forEachRemaining(this.streamListenerResultAdapters::add);
}

From source file:org.springframework.data.hadoop.admin.WorkerNode.java

public <T> T getHighestPriorityLauncher(Class<T> t) {
    Map<String, T> providers = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.applicationContext, t);
    List<T> sortedProviders = new ArrayList<T>(providers.values());
    Collections.sort(sortedProviders, annocationOrderComparator);
    T highestPriorityProvider = sortedProviders.get(0);
    return highestPriorityProvider;
}

From source file:org.springframework.integration.history.MessageHistoryConfigurer.java

private static Collection<TrackableComponent> getTrackableComponents(ListableBeanFactory beanFactory) {
    return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, TrackableComponent.class).values();
}

From source file:org.springframework.springfaces.internal.WrapperHandler.java

/**
 * Wrap the specified delegate by consulting all {@link FacesWrapperFactory factories} registered with Spring.
 * @param externalContext the external context
 * @param delegate the root delegate/* w w w . j  av a2  s.  co  m*/
 * @return a wrapped implementation
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private T wrap(ExternalContext externalContext, T delegate) {
    if (!SpringFacesIntegration.isInstalled(externalContext)) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("SpringFacesSupport is not yet installed, wrapping will be deferred");
        }
        if (this.logger.isWarnEnabled() && this.warnOnMissingSpringFaces) {
            this.logger.warn(
                    "SpringFacesSupport is not installed, full Spring/JSF integration may not be availble");
        }
        return delegate;
    }

    ApplicationContext applicationContext = SpringFacesIntegration.getCurrentInstance(externalContext)
            .getApplicationContext();

    List<Map.Entry<String, FacesWrapperFactory>> orderdBeans = new ArrayList<Map.Entry<String, FacesWrapperFactory>>();
    orderdBeans.addAll(BeanFactoryUtils
            .beansOfTypeIncludingAncestors(applicationContext, FacesWrapperFactory.class).entrySet());
    Collections.sort(orderdBeans, new OrderedMapEntryComparator());
    T rtn = delegate;
    for (Map.Entry<String, FacesWrapperFactory> entry : orderdBeans) {
        FacesWrapperFactory factory = entry.getValue();
        if (isFactorySupported(factory)) {
            T wrapper = (T) factory.newWrapper(this.typeClass, rtn);
            if (wrapper != null) {
                Assert.isInstanceOf(this.typeClass, wrapper,
                        "FacesWrapperFactory " + entry.getValue() + " returned incorrect type ");
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Wrapping " + this.typeClass.getSimpleName() + " with "
                            + wrapper.getClass() + " obtained from FacesWrapperFactory " + entry.getValue());
                }
                postProcessWrapper(wrapper);
                rtn = wrapper;
            }
        }
    }
    return rtn;
}

From source file:org.springframework.test.context.transaction.TestContextTransactionUtils.java

/**
 * Retrieve the {@link DataSource} to use for the supplied {@linkplain TestContext
 * test context}./*from   w  ww.  j  a  va2  s. c o  m*/
 * <p>The following algorithm is used to retrieve the {@code DataSource} from
 * the {@link org.springframework.context.ApplicationContext ApplicationContext}
 * of the supplied test context:
 * <ol>
 * <li>Look up the {@code DataSource} by type and name, if the supplied
 * {@code name} is non-empty, throwing a {@link BeansException} if the named
 * {@code DataSource} does not exist.
 * <li>Attempt to look up the single {@code DataSource} by type.
 * <li>Attempt to look up the <em>primary</em> {@code DataSource} by type.
 * <li>Attempt to look up the {@code DataSource} by type and the
 * {@linkplain #DEFAULT_DATA_SOURCE_NAME default data source name}.
 * @param testContext the test context for which the {@code DataSource}
 * should be retrieved; never {@code null}
 * @param name the name of the {@code DataSource} to retrieve; may be {@code null}
 * or <em>empty</em>
 * @return the {@code DataSource} to use, or {@code null} if not found
 * @throws BeansException if an error occurs while retrieving an explicitly
 * named {@code DataSource}
 */
@Nullable
public static DataSource retrieveDataSource(TestContext testContext, @Nullable String name) {
    Assert.notNull(testContext, "TestContext must not be null");
    BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();

    try {
        // look up by type and explicit name
        if (StringUtils.hasText(name)) {
            return bf.getBean(name, DataSource.class);
        }
    } catch (BeansException ex) {
        logger.error(String.format("Failed to retrieve DataSource named '%s' for test context %s", name,
                testContext), ex);
        throw ex;
    }

    try {
        if (bf instanceof ListableBeanFactory) {
            ListableBeanFactory lbf = (ListableBeanFactory) bf;

            // look up single bean by type
            Map<String, DataSource> dataSources = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf,
                    DataSource.class);
            if (dataSources.size() == 1) {
                return dataSources.values().iterator().next();
            }

            try {
                // look up single bean by type, with support for 'primary' beans
                return bf.getBean(DataSource.class);
            } catch (BeansException ex) {
                logBeansException(testContext, ex, PlatformTransactionManager.class);
            }
        }

        // look up by type and default name
        return bf.getBean(DEFAULT_DATA_SOURCE_NAME, DataSource.class);
    } catch (BeansException ex) {
        logBeansException(testContext, ex, DataSource.class);
        return null;
    }
}

From source file:org.springframework.test.context.transaction.TestContextTransactionUtils.java

/**
 * Retrieve the {@linkplain PlatformTransactionManager transaction manager}
 * to use for the supplied {@linkplain TestContext test context}.
 * <p>The following algorithm is used to retrieve the transaction manager
 * from the {@link org.springframework.context.ApplicationContext ApplicationContext}
 * of the supplied test context:/*ww  w. j a  v  a2 s . co m*/
 * <ol>
 * <li>Look up the transaction manager by type and explicit name, if the supplied
 * {@code name} is non-empty, throwing a {@link BeansException} if the named
 * transaction manager does not exist.
 * <li>Attempt to look up the single transaction manager by type.
 * <li>Attempt to look up the <em>primary</em> transaction manager by type.
 * <li>Attempt to look up the transaction manager via a
 * {@link TransactionManagementConfigurer}, if present.
 * <li>Attempt to look up the transaction manager by type and the
 * {@linkplain #DEFAULT_TRANSACTION_MANAGER_NAME default transaction manager
 * name}.
 * @param testContext the test context for which the transaction manager
 * should be retrieved; never {@code null}
 * @param name the name of the transaction manager to retrieve; may be
 * {@code null} or <em>empty</em>
 * @return the transaction manager to use, or {@code null} if not found
 * @throws BeansException if an error occurs while retrieving an explicitly
 * named transaction manager
 * @throws IllegalStateException if more than one TransactionManagementConfigurer
 * exists in the ApplicationContext
 */
@Nullable
public static PlatformTransactionManager retrieveTransactionManager(TestContext testContext,
        @Nullable String name) {
    Assert.notNull(testContext, "TestContext must not be null");
    BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();

    try {
        // look up by type and explicit name
        if (StringUtils.hasText(name)) {
            return bf.getBean(name, PlatformTransactionManager.class);
        }
    } catch (BeansException ex) {
        logger.error(String.format("Failed to retrieve transaction manager named '%s' for test context %s",
                name, testContext), ex);
        throw ex;
    }

    try {
        if (bf instanceof ListableBeanFactory) {
            ListableBeanFactory lbf = (ListableBeanFactory) bf;

            // look up single bean by type
            Map<String, PlatformTransactionManager> txMgrs = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf,
                    PlatformTransactionManager.class);
            if (txMgrs.size() == 1) {
                return txMgrs.values().iterator().next();
            }

            try {
                // look up single bean by type, with support for 'primary' beans
                return bf.getBean(PlatformTransactionManager.class);
            } catch (BeansException ex) {
                logBeansException(testContext, ex, PlatformTransactionManager.class);
            }

            // look up single TransactionManagementConfigurer
            Map<String, TransactionManagementConfigurer> configurers = BeanFactoryUtils
                    .beansOfTypeIncludingAncestors(lbf, TransactionManagementConfigurer.class);
            Assert.state(configurers.size() <= 1,
                    "Only one TransactionManagementConfigurer may exist in the ApplicationContext");
            if (configurers.size() == 1) {
                return configurers.values().iterator().next().annotationDrivenTransactionManager();
            }
        }

        // look up by type and default name
        return bf.getBean(DEFAULT_TRANSACTION_MANAGER_NAME, PlatformTransactionManager.class);
    } catch (BeansException ex) {
        logBeansException(testContext, ex, PlatformTransactionManager.class);
        return null;
    }
}

From source file:org.tinygroup.springmvc.coc.ConventionBeanDefinitionRegistryPostProcessor.java

public void afterPropertiesSet() throws Exception {
    if (CollectionUtil.isEmpty(conventionComponentIdentifierComposite)) {
        Map<String, ConventionComponentIdentifier> map = BeanFactoryUtils.beansOfTypeIncludingAncestors(
                this.getListableBeanFactory(), ConventionComponentIdentifier.class);
        conventionComponentIdentifierComposite = new ArrayList<ConventionComponentIdentifier>();
        conventionComponentIdentifierComposite.addAll(map.values());
    }/*from  ww  w  . ja v  a  2s  .c  o m*/

}