Example usage for org.springframework.context ApplicationContext getBeanNamesForType

List of usage examples for org.springframework.context ApplicationContext getBeanNamesForType

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getBeanNamesForType.

Prototype

String[] getBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit);

Source Link

Document

Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

Usage

From source file:org.sakaiproject.entitybroker.impl.event.EventReceiverCoordinator.java

public void setApplicationContext(ApplicationContext context) throws BeansException {
    String[] autobeans = context.getBeanNamesForType(EventReceiver.class, false, false);
    for (String autobean : autobeans) {
        EventReceiver receiver = (EventReceiver) context.getBean(autobean);
        if (receiver != null) {
            receivers.put(receiver.getClass().getClassLoader(), receiver);
        }// w w  w  . j  a v  a  2  s  .  com
    }
}

From source file:org.sakaiproject.entitybroker.util.spring.EntityProviderAutoRegistrar.java

public void setApplicationContext(ApplicationContext context) throws BeansException {
    log.debug("setAC: " + context.getDisplayName());
    String[] autobeans = context.getBeanNamesForType(AutoRegisterEntityProvider.class, false, false);
    StringBuilder registeredPrefixes = new StringBuilder();
    for (String autobean : autobeans) {
        AutoRegisterEntityProvider register = (AutoRegisterEntityProvider) context.getBean(autobean);
        if (register.getEntityPrefix() == null || register.getEntityPrefix().equals("")) {
            // should this die here or is this error log enough? -AZ
            log.error(/*from   w ww . ja v a  2 s  .co  m*/
                    "Could not autoregister EntityProvider because the enity prefix is null or empty string for class: "
                            + register.getClass().getName());
        } else {
            registeredPrefixes.append(" : " + register.getEntityPrefix());
            entityProviderManager.registerEntityProvider(register);
        }
    }
    log.info("AutoRegistered EntityProvider prefixes " + registeredPrefixes);
    // TODO - deal with de-registration in the case we ever support dynamic contexts.
}

From source file:org.solmix.runtime.support.spring.SpringBeanProvider.java

private Set<String> doGetBeanNamesOfType0(ApplicationContext context, Class<?> type) {

    Set<String> s = new LinkedHashSet<String>(Arrays.asList(context.getBeanNamesForType(type, false, false)));
    if (context.getParent() != null) {
        s.addAll(doGetBeanNamesOfType0(context.getParent(), type));
    }//from   w ww. j  ava2 s  . c  o m
    return s;
}

From source file:org.cloudfoundry.reconfiguration.util.StandardCloudUtils.java

private Boolean hasBeanOfType(ApplicationContext applicationContext, String cloudServiceClassName) {
    if (ClassUtils.isPresent(cloudServiceClassName, null)) {
        Class<?> cloudServiceClass = ClassUtils.resolveClassName(cloudServiceClassName, null);
        return applicationContext.getBeanNamesForType(cloudServiceClass, true, false).length != 0;
    }//  ww w .j av a2 s .  c om

    return false;
}

From source file:org.geoserver.wps.jts.SpringBeanProcessFactory.java

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;

    // loads all of the beans implementing the marker interface
    String[] beanNames = applicationContext.getBeanNamesForType(markerInterface, true, true);
    // build a name to class and name to bean name maps
    classMap = new HashMap<String, Class>();
    beanMap = new HashMap<String, String>();
    for (String beanName : beanNames) {
        Class c = applicationContext.getType(beanName);
        String name = c.getSimpleName();
        if (name.endsWith("Process")) {
            name = name.substring(0, name.indexOf("Process"));
        }//from   w  ww  . ja va 2  s.com
        classMap.put(name, c);
        beanMap.put(name, beanName);
    }

}

From source file:org.solmix.runtime.support.spring.SpringBeanProvider.java

private <T> Collection<? extends T> _doGetBeansOfType(ApplicationContext context, Class<T> type) {
    if (context != null) {
        Set<String> s = new LinkedHashSet<String>(
                Arrays.asList(context.getBeanNamesForType(type, false, false)));
        s.removeAll(passThroughs);//from w  w  w.  j  a  v a  2s  . c  o m
        List<T> lst = new LinkedList<T>();
        for (String n : s) {
            lst.add(type.cast(context.getBean(n, type)));
        }
        if (context.getParent() != null) {
            lst.addAll(_doGetBeansOfType(context.getParent(), type));
        }
        return lst;
    }
    return null;

}

From source file:org.apache.camel.spring.CamelContextFactoryBean.java

protected <S> S getBeanForType(Class<S> clazz) {
    S bean = null;/*w  w w  . ja  v a  2s  . co  m*/
    String[] names = getApplicationContext().getBeanNamesForType(clazz, true, true);
    if (names.length == 1) {
        bean = (S) getApplicationContext().getBean(names[0], clazz);
    }
    if (bean == null) {
        ApplicationContext parentContext = getApplicationContext().getParent();
        if (parentContext != null) {
            names = parentContext.getBeanNamesForType(clazz, true, true);
            if (names.length == 1) {
                bean = (S) parentContext.getBean(names[0], clazz);
            }
        }
    }
    return bean;
}

From source file:org.openspaces.remoting.ExecutorRemotingTask.java

public static SpaceRemotingServiceExporter getServiceExporter(ApplicationContext applicationContext) {
    try {//from  w  w  w.j  a  v  a  2s . c o  m
        if (logger.isDebugEnabled())
            logger.debug(
                    "Looking for default serviceExporter - applicationContext.getBean(\"serviceExporter\")");
        return (SpaceRemotingServiceExporter) applicationContext.getBean("serviceExporter");
    } catch (NoSuchBeanDefinitionException e) {
        String[] names = applicationContext.getBeanNamesForType(SpaceRemotingServiceExporter.class, false,
                true);
        if (names == null || names.length == 0)
            throw new IllegalStateException(
                    "Failed to find remoting service exporter defined within the application context");
        String name = names[0];
        if (logger.isDebugEnabled())
            logger.debug("Looking for first serviceExporter - applicationContext.getBean(" + name + ")");
        return (SpaceRemotingServiceExporter) applicationContext.getBean(name);
    }
}

From source file:org.springframework.osgi.extender.internal.support.SingleImporterFastShutdown.java

public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextClosedEvent) {
        ApplicationContext context = ((ContextClosedEvent) event).getApplicationContext();
        System.out.println("Trying to get the bean names...");
        String[] beanNamesForType = context.getBeanNamesForType(OsgiServiceProxyFactoryBean.class, true, false);
        for (int i = 0; i < beanNamesForType.length; i++) {
            String string = beanNamesForType[i];
            System.out.println("found importers ... " + string);
        }//from w  w w  .  ja  v a  2 s .co  m
        if (log.isDebugEnabled())
            log.debug("Resetting importers (on shutdown): " + beanNamesForType);
        // for (Iterator iterator = beansOfType.values().iterator(); iterator.hasNext();) {
        // OsgiServiceProxyFactoryBean type = (OsgiServiceProxyFactoryBean) iterator.next();
        // type.setTimeout(0);
        // }
    }
}