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:com.jgoetsch.eventtrader.EventTraderSpringLauncher.java

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: " + EventTraderSpringLauncher.class.getSimpleName() + " <files>...");
        System.out.println("       files - List of paths to spring bean definition xml files.");
        System.out.println("               Each object defined that implements Runnable will be executed");
        System.out.println("               in its own thread.");
    } else {/* ww w  .  ja va2  s  .  com*/
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(args);

        // auto register growl notifications after all GrowlNotification objects have been instantiated
        // if it is found on the classpath
        try {
            Class.forName("com.jgoetsch.eventtrader.processor.GrowlNotification").getMethod("autoRegister")
                    .invoke(null);
        } catch (Exception e) {
            log.warn("Growl not found, cannot autoRegister notifications: {}", e.getMessage());
        }

        Map<String, Runnable> runnables = BeanFactoryUtils.beansOfTypeIncludingAncestors(context,
                Runnable.class);
        List<Thread> threads = new ArrayList<Thread>(runnables.size());
        for (final Map.Entry<String, Runnable> runner : runnables.entrySet()) {
            final Thread th = new Thread(runner.getValue(), runner.getKey());
            threads.add(th);
            th.start();
        }

        // close spring context on JVM shutdown
        // this causes all @PreDestroy methods in the runnables to be called to allow for
        // them to shutdown gracefully
        context.registerShutdownHook();

        // wait for launched threads to finish before cleaning up beans
        for (Thread th : threads) {
            try {
                th.join();
            } catch (InterruptedException e) {
            }
        }
    }
}

From source file:net.chrisrichardson.ormunit.hibernate.LocalSessionFactoryBeanUtil.java

public static LocalSessionFactoryBean getLocalSessionFactoryBean(ApplicationContext applicationContext) {
    Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext,
            LocalSessionFactoryBean.class);
    Assert.assertEquals("LocalSessionFactoryBean", 1, beans.size());
    LocalSessionFactoryBean localSessionFactoryBean = (LocalSessionFactoryBean) beans.values().iterator()
            .next();/*from ww  w . j a v  a 2s.  c  o m*/
    return localSessionFactoryBean;
}

From source file:net.freedom.gj.beans.factory.ext.SpringTreeBeanFactory.java

@Override
protected List<Object> findAllByType(Class type) {
    List<Object> list = new ArrayList<Object>();
    Map<String, Object> objects = BeanFactoryUtils
            .beansOfTypeIncludingAncestors((ListableBeanFactory) beanFactory, type);

    for (String key : objects.keySet()) {
        if (objects.get(key).getClass().isAnnotationPresent(Criteria.class)
                || objects.get(key).getClass().isAnnotationPresent(CriteriaList.class)
                || type.isInstance(objects.get(key))) {
            list.add(objects.get(key));//from  w w  w  .j  av a 2 s  . co  m
        }
    }
    return list;
}

From source file:io.fns.calculator.config.LegacyJacksonConfig.java

@PostConstruct
public void init() {
    Collection<ObjectMapper> mappers = BeanFactoryUtils
            .beansOfTypeIncludingAncestors(this.beanFactory, ObjectMapper.class).values();
    Collection<Module> modules = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.beanFactory, Module.class)
            .values();//from  w  w w .ja va  2s . c  o  m
    for (ObjectMapper mapper : mappers) {
        mapper.registerModule(modules.iterator().next()); // there can be only one!
        break;
    }
}

From source file:com.avanza.astrix.gs.GsBinder.java

private GigaSpace findEmbeddedSpace(ApplicationContext applicationContext) {
    GigaSpace result = null;/*from w ww . j a  va 2s .c  o m*/
    for (GigaSpace gigaSpace : BeanFactoryUtils
            .beansOfTypeIncludingAncestors(applicationContext, GigaSpace.class).values()) {
        if (isEmbedded(gigaSpace)) {
            if (result != null) {
                throw new IllegalStateException("Multiple embedded spaces defined in applicationContext");
            } else {
                result = gigaSpace;
            }
        }
    }
    if (result == null) {
        throw new IllegalStateException("Failed to find an embedded space in applicationContext");
    }
    return result;
}

From source file:net.sourceforge.hypo.inject.resolver.DefaultTypeSpringBeanResolver.java

/**
 * Performs injection by looking up the Spring ApplicationContext to find the single bean
 * which has the same type as or is a subclass of the Inject's type. 
 * @param dep the Dependency to be injected
 * @param target the object which is to have the member injected
 * @return true if a single matching bean existed in the context; false if no suitable 
 * bean was found//from w w  w.j a va2s. c  o m
 * @throws RuntimeException if more than one bean in the Spring ApplicationContext matched
 * the Dependency
 */
public ResolutionResult doResolve(Dependency dep, Object target) {
    Class<?> type = dep.getType();
    Map<?, ?> map = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, type);
    if (map.size() < 1) {
        if (log.isLoggable(Level.FINE))
            log.fine("No beans of required type found for dependency " + dep + ". Skipping.");
    } else if (map.size() > 1) {
        throw new RuntimeException("Multiple beans of required type " + type + " found. Aborting.");
    } else {
        Object bean = map.values().iterator().next();
        if (log.isLoggable(Level.FINE))
            log.fine("Found bean [" + bean + "] to inject for dependency " + dep + ".");
        return ResolutionResult.resolved(bean);
    }
    return ResolutionResult.couldNotResolve();
}

From source file:com.googlecode.shutdownlistener.spring.ShutdownHandlerBean.java

public void afterPropertiesSet() throws Exception {
    if (this.shutdownListeners == null) {
        final Map<String, ShutdownListener> shutdownListenerMap = BeanFactoryUtils
                .beansOfTypeIncludingAncestors(this.applicationContext, ShutdownListener.class);
        this.shutdownListeners = new ArrayList<ShutdownListener>(shutdownListenerMap.values());
        this.logger.debug(
                "No explicit shutdownListeners configured, using {} ShutdownListeners from application context.",
                this.shutdownListeners.size());
    }/* w  w  w.  j a v a2 s  .  co m*/

    this.start();
}

From source file:com.github.relai.vertx.springdata.JacksonAutoWire.java

private <T> Collection<T> getBeans(Class<T> type) {
    return BeanFactoryUtils.beansOfTypeIncludingAncestors(this.beanFactory, type).values();
}

From source file:de.metas.ui.web.websocket.WebSocketProducersRegistry.java

@PostConstruct
private void registerProducerFactoriesFromContext() {
    BeanFactoryUtils.beansOfTypeIncludingAncestors(context, WebSocketProducerFactory.class).values()
            .forEach(producerFactory -> registerProducerFactory(producerFactory));

}

From source file:org.synyx.hades.extensions.beans.GenericDaoPropertyEditorRegistrar.java

@SuppressWarnings("unchecked")
public void setApplicationContext(ApplicationContext context) {

    @SuppressWarnings("rawtypes")
    Collection<GenericDao> daos = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, GenericDao.class)
            .values();/*w  w w  .  j  a  v a2s  . co  m*/

    for (GenericDao<?, ?> dao : daos) {
        this.daoMap.put(getDomainClass(dao), (GenericDao<?, Serializable>) dao);
    }
}