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,
        boolean includeNonSingletons, boolean allowEagerInit) 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.wallride.autoconfigure.WallRideDispatcherServlet.java

@Override
protected void initStrategies(ApplicationContext context) {
    super.initStrategies(context);

    if (this.detectParentHandlerMappings) {
        this.parentHandlerMappings = null;
        if (getWebApplicationContext().getParent() != null) {
            Map<String, HandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
                    getWebApplicationContext().getParent(), HandlerMapping.class, true, false);
            if (!matchingBeans.isEmpty()) {
                this.parentHandlerMappings = new ArrayList<>(matchingBeans.values());
                AnnotationAwareOrderComparator.sort(this.parentHandlerMappings);
            }/*from ww w.ja va 2  s .com*/
        }
    }
}

From source file:org.devefx.httpmapper.spring.mapper.MapperFactoryBean.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    final List<MappedListener> mappedListeners = new ArrayList<MappedListener>();
    mappedListeners.addAll(BeanFactoryUtils
            .beansOfTypeIncludingAncestors(applicationContext, MappedListener.class, true, false).values());
    config.setMappedListeners(mappedListeners);
}

From source file:com.griddynamics.banshun.web.ScanChildrenHandlerMapping.java

public void createHandlerMappingsAndRegisterHandlers(ApplicationContext child) {
    Map<String, HandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(child,
            HandlerMapping.class, true, false);

    List<HandlerMapping> handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());
    OrderComparator.sort(handlerMappings);
    handlerMappings.remove(this);
    if (handlerMappings.isEmpty()) {
        handlerMappings = createDefaultHandlerMappings(child);
        if (logger.isDebugEnabled()) {
            logger.debug("No HandlerMappings found in context '" + child.getDisplayName() + "': using default");
        }//from  w w  w  .  ja v a 2 s  .  c  o m
    }
    registerHandlers(handlerMappings);
}

From source file:net.daum.clix.springframework.data.rest.client.repository.RestRepositories.java

/**
 * Creates a new {@link RestRepositories} instance by looking up the
 * repository instances and meta information from the given
 * {@link ListableBeanFactory}.//www.  ja  v a 2 s.c  om
 * 
 * @param factory
 *            must not be {@literal null}.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public RestRepositories(ListableBeanFactory factory) {

    Assert.notNull(factory);

    Collection<RestRepositoryFactoryBean> providers = BeanFactoryUtils
            .beansOfTypeIncludingAncestors(factory, RestRepositoryFactoryBean.class, true, false).values();

    for (RepositoryFactoryInformation<Object, Serializable> info : providers) {

        RepositoryInformation information = info.getRepositoryInformation();
        Class repositoryInterface = information.getRepositoryInterface();

        if (CrudRepository.class.isAssignableFrom(repositoryInterface)) {
            Class<CrudRepository<Object, Serializable>> objectType = repositoryInterface;
            CrudRepository<Object, Serializable> repository = BeanFactoryUtils
                    .beanOfTypeIncludingAncestors(factory, objectType);

            this.domainClassToBeanName.put(information.getDomainType(), info);
            this.resourcePathToBeanName.put(getResourcePath(repositoryInterface), info);
            this.repositories.put(info, repository);

        }
    }
}

From source file:org.tangram.components.spring.TangramViewHandler.java

/**
 * Initialize the ViewResolvers used by this class.
 * <p>/*from w ww  .  ja v a  2 s.co m*/
 * If no ViewResolver beans are defined in the BeanFactory for this namespace, we default to
 * InternalResourceViewResolver.
 */
private void initViewResolvers(ApplicationContext context) {
    this.modelAwareViewResolvers = null;

    if (this.detectAllModelAwareViewResolvers) {
        // Find all ViewResolvers in the ApplicationContext, including ancestor contexts.
        Map<String, ModelAwareViewResolver> matchingBeans = BeanFactoryUtils
                .beansOfTypeIncludingAncestors(context, ModelAwareViewResolver.class, true, false);
        if (!matchingBeans.isEmpty()) {
            this.modelAwareViewResolvers = new ArrayList<>(matchingBeans.values());
            // We keep ViewResolvers in sorted order.
            OrderComparator.sort(this.modelAwareViewResolvers);
        } // if
    } else {
        try {
            ModelAwareViewResolver vr = context.getBean(DispatcherServlet.VIEW_RESOLVER_BEAN_NAME,
                    ModelAwareViewResolver.class);
            this.modelAwareViewResolvers = Collections.singletonList(vr);
        } catch (NoSuchBeanDefinitionException e) {
            // Ignore, we'll add a default ViewResolver later.
            LOG.warn("initViewResolvers()", e);
        } // try/catch
    } // if
}

From source file:com.alibaba.dubbo.config.spring.ReferenceBean.java

public void afterPropertiesSet() throws Exception {
    if (getConsumer() == null) {
        Map<String, ConsumerConfig> consumerConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ConsumerConfig.class,
                        false, false);/*from  w  w w. ja va  2 s .  com*/
        if (consumerConfigMap != null && consumerConfigMap.size() > 0) {
            ConsumerConfig consumerConfig = null;
            for (ConsumerConfig config : consumerConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    if (consumerConfig != null) {
                        throw new IllegalStateException(
                                "Duplicate consumer configs: " + consumerConfig + " and " + config);
                    }
                    consumerConfig = config;
                }
            }
            if (consumerConfig != null) {
                setConsumer(consumerConfig);
            }
        }
    }
    if (getApplication() == null && (getConsumer() == null || getConsumer().getApplication() == null)) {
        Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class,
                        false, false);
        if (applicationConfigMap != null && applicationConfigMap.size() > 0) {
            ApplicationConfig applicationConfig = null;
            for (ApplicationConfig config : applicationConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    if (applicationConfig != null) {
                        throw new IllegalStateException(
                                "Duplicate application configs: " + applicationConfig + " and " + config);
                    }
                    applicationConfig = config;
                }
            }
            if (applicationConfig != null) {
                setApplication(applicationConfig);
            }
        }
    }
    if (getModule() == null && (getConsumer() == null || getConsumer().getModule() == null)) {
        Map<String, ModuleConfig> moduleConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ModuleConfig.class, false,
                        false);
        if (moduleConfigMap != null && moduleConfigMap.size() > 0) {
            ModuleConfig moduleConfig = null;
            for (ModuleConfig config : moduleConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    if (moduleConfig != null) {
                        throw new IllegalStateException(
                                "Duplicate module configs: " + moduleConfig + " and " + config);
                    }
                    moduleConfig = config;
                }
            }
            if (moduleConfig != null) {
                setModule(moduleConfig);
            }
        }
    }
    if ((getRegistries() == null || getRegistries().size() == 0)
            && (getConsumer() == null || getConsumer().getRegistries() == null
                    || getConsumer().getRegistries().size() == 0)
            && (getApplication() == null || getApplication().getRegistries() == null
                    || getApplication().getRegistries().size() == 0)) {
        Map<String, RegistryConfig> registryConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RegistryConfig.class,
                        false, false);
        if (registryConfigMap != null && registryConfigMap.size() > 0) {
            List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
            for (RegistryConfig config : registryConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    registryConfigs.add(config);
                }
            }
            if (registryConfigs != null && registryConfigs.size() > 0) {
                super.setRegistries(registryConfigs);
            }
        }
    }
    if (getMonitor() == null && (getConsumer() == null || getConsumer().getMonitor() == null)
            && (getApplication() == null || getApplication().getMonitor() == null)) {
        Map<String, MonitorConfig> monitorConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MonitorConfig.class, false,
                        false);
        if (monitorConfigMap != null && monitorConfigMap.size() > 0) {
            MonitorConfig monitorConfig = null;
            for (MonitorConfig config : monitorConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    if (monitorConfig != null) {
                        throw new IllegalStateException(
                                "Duplicate monitor configs: " + monitorConfig + " and " + config);
                    }
                    monitorConfig = config;
                }
            }
            if (monitorConfig != null) {
                setMonitor(monitorConfig);
            }
        }
    }
    Boolean b = isInit();
    if (b == null && getConsumer() != null) {
        b = getConsumer().isInit();
    }
    if (b != null && b.booleanValue()) {
        getObject();
    }
}

From source file:com.alexshabanov.springrestapi.restapitest.DefaultRestTestSupport.java

private boolean handleException(Exception e, HttpServletRequest request, HttpServletResponse response,
        Object handler) {// w  w  w. j a  va 2  s .co m
    final Map<String, HandlerExceptionResolver> resolverMap = BeanFactoryUtils
            .beansOfTypeIncludingAncestors(context, HandlerExceptionResolver.class, true, false);

    final List<HandlerExceptionResolver> resolvers = new ArrayList<HandlerExceptionResolver>(
            resolverMap.values());
    OrderComparator.sort(resolvers);

    for (HandlerExceptionResolver exceptionResolver : resolvers) {
        final Object o = exceptionResolver.resolveException(request, response, handler, e);
        if (o != null) {
            // we don't care about model-and-view in the result (this is not needed for testing)
            return true;
        }
    }

    return false;
}

From source file:org.iterx.miru.spring.beans.SpringBeanFactory.java

public Object getBeanOfType(Class type) {
    assert (type != null) : "type == null";
    try {/*from ww w. j  ava2 s  .c o  m*/
        Map map;

        if ((factory instanceof ListableBeanFactory) && (map = BeanFactoryUtils
                .beansOfTypeIncludingAncestors((ListableBeanFactory) factory, type, true, false)).size() > 0)
            return ((map.values()).iterator()).next();

    } catch (BeansException e) {
        if (logger.isDebugEnabled())
            logger.debug("Failed to create bean implementation [" + type.toString() + "]", e);
    }

    return (parent != null) ? parent.getBeanOfType(type) : null;
}

From source file:org.iterx.miru.spring.beans.SpringBeanFactory.java

public Object getBeanOfType(Class[] types) {
    assert (types != null && types.length > 0) : "types == null";
    try {/* w ww  . ja v a2s .  c om*/
        Map map;

        if ((factory instanceof ListableBeanFactory)
                && (map = BeanFactoryUtils.beansOfTypeIncludingAncestors((ListableBeanFactory) factory,
                        types[0], true, false)).size() > 0) {

            for (Iterator beans = (map.values()).iterator(); beans.hasNext();) {
                Object bean;
                Class cls;
                int i;

                cls = (bean = beans.next()).getClass();

                for (i = types.length; i-- > 1;) {
                    if (!types[i].isAssignableFrom(cls))
                        break;
                }

                if (i < 1)
                    return bean;
            }
        }
    } catch (BeansException e) {
        if (logger.isDebugEnabled()) {
            StringBuffer buffer;

            buffer = new StringBuffer();
            for (int i = 0; i < types.length; i++) {
                buffer.append(',');
                buffer.append(types[i]);
            }
            logger.debug("Failed to create bean implementation [" + buffer.substring(1) + "]", e);
        }
    }

    return (parent != null) ? parent.getBeanOfType(types) : null;
}

From source file:com.alibaba.dubbo.config.spring.ServiceBean.java

@SuppressWarnings({ "deprecation" })
public void afterPropertiesSet() throws Exception {
    if (getProvider() == null) {
        Map<String, ProviderConfig> providerConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProviderConfig.class,
                        false, false);/*from w  w w.j av a2s. com*/
        if (providerConfigMap != null && providerConfigMap.size() > 0) {
            Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null
                    : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class,
                            false, false);
            if ((protocolConfigMap == null || protocolConfigMap.size() == 0) && providerConfigMap.size() > 1) { // 
                List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>();
                for (ProviderConfig config : providerConfigMap.values()) {
                    if (config.isDefault() != null && config.isDefault().booleanValue()) {
                        providerConfigs.add(config);
                    }
                }
                if (providerConfigs.size() > 0) {
                    setProviders(providerConfigs);
                }
            } else {
                ProviderConfig providerConfig = null;
                for (ProviderConfig config : providerConfigMap.values()) {
                    if (config.isDefault() == null || config.isDefault().booleanValue()) {
                        if (providerConfig != null) {
                            throw new IllegalStateException(
                                    "Duplicate provider configs: " + providerConfig + " and " + config);
                        }
                        providerConfig = config;
                    }
                }
                if (providerConfig != null) {
                    setProvider(providerConfig);
                }
            }
        }
    }
    if (getApplication() == null && (getProvider() == null || getProvider().getApplication() == null)) {
        Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class,
                        false, false);
        if (applicationConfigMap != null && applicationConfigMap.size() > 0) {
            ApplicationConfig applicationConfig = null;
            for (ApplicationConfig config : applicationConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    if (applicationConfig != null) {
                        throw new IllegalStateException(
                                "Duplicate application configs: " + applicationConfig + " and " + config);
                    }
                    applicationConfig = config;
                }
            }
            if (applicationConfig != null) {
                setApplication(applicationConfig);
            }
        }
    }
    if (getModule() == null && (getProvider() == null || getProvider().getModule() == null)) {
        Map<String, ModuleConfig> moduleConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ModuleConfig.class, false,
                        false);
        if (moduleConfigMap != null && moduleConfigMap.size() > 0) {
            ModuleConfig moduleConfig = null;
            for (ModuleConfig config : moduleConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    if (moduleConfig != null) {
                        throw new IllegalStateException(
                                "Duplicate module configs: " + moduleConfig + " and " + config);
                    }
                    moduleConfig = config;
                }
            }
            if (moduleConfig != null) {
                setModule(moduleConfig);
            }
        }
    }
    if ((getRegistries() == null || getRegistries().size() == 0)
            && (getProvider() == null || getProvider().getRegistries() == null
                    || getProvider().getRegistries().size() == 0)
            && (getApplication() == null || getApplication().getRegistries() == null
                    || getApplication().getRegistries().size() == 0)) {
        Map<String, RegistryConfig> registryConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RegistryConfig.class,
                        false, false);
        if (registryConfigMap != null && registryConfigMap.size() > 0) {
            List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
            for (RegistryConfig config : registryConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    registryConfigs.add(config);
                }
            }
            if (registryConfigs != null && registryConfigs.size() > 0) {
                super.setRegistries(registryConfigs);
            }
        }
    }
    if (getMonitor() == null && (getProvider() == null || getProvider().getMonitor() == null)
            && (getApplication() == null || getApplication().getMonitor() == null)) {
        Map<String, MonitorConfig> monitorConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MonitorConfig.class, false,
                        false);
        if (monitorConfigMap != null && monitorConfigMap.size() > 0) {
            MonitorConfig monitorConfig = null;
            for (MonitorConfig config : monitorConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    if (monitorConfig != null) {
                        throw new IllegalStateException(
                                "Duplicate monitor configs: " + monitorConfig + " and " + config);
                    }
                    monitorConfig = config;
                }
            }
            if (monitorConfig != null) {
                setMonitor(monitorConfig);
            }
        }
    }
    if ((getProtocols() == null || getProtocols().size() == 0) && (getProvider() == null
            || getProvider().getProtocols() == null || getProvider().getProtocols().size() == 0)) {
        Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class,
                        false, false);
        if (protocolConfigMap != null && protocolConfigMap.size() > 0) {
            List<ProtocolConfig> protocolConfigs = new ArrayList<ProtocolConfig>();
            for (ProtocolConfig config : protocolConfigMap.values()) {
                if (config.isDefault() == null || config.isDefault().booleanValue()) {
                    protocolConfigs.add(config);
                }
            }
            if (protocolConfigs != null && protocolConfigs.size() > 0) {
                super.setProtocols(protocolConfigs);
            }
        }
    }
    if (getPath() == null || getPath().length() == 0) {
        if (beanName != null && beanName.length() > 0 && getInterface() != null && getInterface().length() > 0
                && beanName.startsWith(getInterface())) {
            setPath(beanName);
        }
    }
    if (!isDelay()) {
        export();
    }
}