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

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

Introduction

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

Prototype

public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class<?> type) 

Source Link

Document

Get all bean names for the given type, including those defined in ancestor factories.

Usage

From source file:com.laxser.blitz.util.SpringUtils.java

public static <T> T getBean(ListableBeanFactory bf, Class<?> beanClass) {
    String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanClass);
    @SuppressWarnings("unchecked")
    T bean = (T) ((names.length == 0) ? null : bf.getBean(names[0]));
    return bean;//from   w ww.  j  a v a  2s  . c  o m
}

From source file:com.laxser.blitz.util.SpringUtils.java

public static String[] getBeanNames(ListableBeanFactory bf, Class<?> beanClass) {
    return BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanClass);
}

From source file:com.laxser.blitz.util.SpringUtils.java

@SuppressWarnings("unchecked")
public static <T> List<T> getBeans(ListableBeanFactory bf, Class<T> beanClass) {
    String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(bf, beanClass);
    List<T> beans = new ArrayList<T>(names.length);
    for (String name : names) {
        beans.add((T) bf.getBean(name));
    }/* ww  w  .ja va 2s  .c  o m*/
    return beans;
}

From source file:io.neba.core.spring.applicationcontext.configuration.PlaceholderVariableResolverRegistrar.java

public void registerResolvers(BundleContext bundleContext, ConfigurableListableBeanFactory beanFactory) {
    this.logger.info("Detecting " + PlaceholderVariableResolver.class + " instances in "
            + displayNameOf(bundleContext.getBundle()) + "...");
    String[] valueResolvers = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory,
            PlaceholderVariableResolver.class);

    for (String resolverName : valueResolvers) {
        StringValueResolver embeddableResolver = new PlaceholderVariableResolverWrapper(beanFactory,
                resolverName);//from  w  w w.  jav  a 2  s .  c o  m
        beanFactory.addEmbeddedValueResolver(embeddableResolver);
    }
    this.logger.info("Detected and registered " + valueResolvers.length + " resolver(s).");
}

From source file:com.enterra.batch.admin.sample.BootstrapTests.java

@Test
public void testServletConfiguration() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml" },
            parent);//from  www. ja v  a2  s .  c  om

    assertTrue(context.containsBean("jobRepository"));
    String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(),
            JobController.class);
    assertEquals(1, beanNames.length);

    Job job = context.getBean(JobRegistry.class).getJob("job1");
    final JobExecution jobExecution = parent.getBean(JobLauncher.class).run(job,
            new JobParametersBuilder().addString("fail", "false").toJobParameters());

    new DirectPoller<BatchStatus>(100).poll(new Callable<BatchStatus>() {
        public BatchStatus call() throws Exception {
            BatchStatus status = jobExecution.getStatus();
            if (status.isLessThan(BatchStatus.STOPPED) && status != BatchStatus.COMPLETED) {
                return null;
            }
            return status;
        }
    }).get(2000, TimeUnit.MILLISECONDS);

    context.close();
    parent.close();

    assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

}

From source file:com.aol.advertising.qiao.util.ContextUtils.java

public static String[] getBeanNames(Class<?> clz) {
    if (_instance == null) {
        throw new FatalBeanException("Missing ContextUtil bean");
    }//from   w  w  w.  j av  a 2  s.c o  m

    return BeanFactoryUtils.beanNamesForTypeIncludingAncestors(_instance.applicationContext, clz);

}

From source file:ro.pippo.spring.AnnotationFieldValueProvider.java

private String getBeanNameOfClass(Class<?> clazz) {
    // get the list of all possible matching beans
    List<String> names = Arrays
            .asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, clazz));

    // filter out beans that are not candidates for auto wiring
    if (applicationContext instanceof AbstractApplicationContext) {
        Iterator<String> iterator = names.iterator();
        while (iterator.hasNext()) {
            String possibility = iterator.next();
            BeanDefinition beanDef = getBeanDefinition(
                    ((AbstractApplicationContext) applicationContext).getBeanFactory(), possibility);
            if (BeanFactoryUtils.isFactoryDereference(possibility) || possibility.startsWith("scopedTarget.")
                    || (beanDef != null && !beanDef.isAutowireCandidate())) {
                iterator.remove();/*from   w w w.  jav a 2 s.  c  o m*/
            }
        }
    }

    if (names.isEmpty()) {
        return null;
    }

    if (names.size() > 1) {
        if (applicationContext instanceof AbstractApplicationContext) {
            List<String> primaries = new ArrayList<>();
            for (String name : names) {
                BeanDefinition beanDef = getBeanDefinition(
                        ((AbstractApplicationContext) applicationContext).getBeanFactory(), name);
                if (beanDef instanceof AbstractBeanDefinition) {
                    if (beanDef.isPrimary()) {
                        primaries.add(name);
                    }
                }
            }
            if (primaries.size() == 1) {
                return primaries.get(0);
            }
        }
        StringBuilder message = new StringBuilder();
        message.append("More than one bean of type [");
        message.append(clazz.getName());
        message.append("] found, you have to specify the name of the bean ");
        message.append("(@Named(\"foo\") if using @javax.inject classes) in order to resolve this conflict. ");
        message.append("Matched beans: ");
        message.append(names);
        throw new PippoRuntimeException(message.toString());
    }

    return names.get(0);
}

From source file:br.com.caelum.vraptor.ioc.spring.SpringBasedContainer.java

public <T> boolean canProvide(Class<T> type) {
    return BeanFactoryUtils.beanNamesForTypeIncludingAncestors(parentContext, type).length > 0;
}

From source file:org.web4thejob.context.DefaultSessionContext.java

@Override
public <T extends Panel> List<T> getPanels(Class<T> requiredType) {
    List<T> panels = new ArrayList<T>();
    for (String beanid : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this, requiredType)) {
        try {//from   w w w.  ja v a2 s  . c  o m
            panels.add(getBean(beanid, requiredType));
        } catch (BeansException ignore) {
            ignore.printStackTrace();
        }
    }
    Collections.sort(panels, new Comparator<Panel>() {
        @Override
        public int compare(Panel p1, Panel p2) {
            return p1.toString().compareToIgnoreCase(p2.toString());
        }
    });
    return panels;
}

From source file:org.web4thejob.context.DefaultSessionContext.java

@Override
public boolean hasPanel(String beanid, Class<? extends Panel> requiredType) {
    if (Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this, requiredType))
            .contains(beanid)) {//from  w w  w.j av  a 2  s . c  om
        try {
            return getBean(beanid, requiredType) != null;
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
    }
    return false;
}