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);

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:com.opensymphony.able.jpa.JpaInjectionTest.java

@Test(dataProvider = "springUriWithEntityManager")
public void testInjectionOfActionBeans(String classpathUri) throws Exception {
    ApplicationContext context = loadContext(classpathUri);

    String[] names = context.getBeanNamesForType(EntityManagerFactory.class);
    Assert.assertEquals(1, names.length, "number of names");

    final PersonActionBean target = new PersonActionBean();
    SpringHelper.injectBeans(target, context);

    JpaCrudService<Person> service = (JpaCrudService<Person>) target.getService();

    final JpaTemplate jpaTemplate = service.getJpaTemplate();
    Assert.assertNotNull(jpaTemplate, "Failed to inject an JpaTemplate: " + jpaTemplate);

    System.out.println("Found template: " + jpaTemplate);

    EntityManager entityManager = (EntityManager) jpaTemplate.execute(new JpaCallback() {
        public Object doInJpa(EntityManager entityManager) throws PersistenceException {
            return entityManager;
        }/*from  ww  w . j  av a  2s.  c om*/
    });

    Assert.assertNotNull(entityManager, "Failed to inject an EntityManager: " + entityManager);
}

From source file:com.griddynamics.banshun.RegistryBeanTest.java

private boolean hasNoExports(ApplicationContext ctx) {
    String[] beanNames = ctx.getBeanNamesForType(TargetSource.class);
    for (String beanName : beanNames) {
        if (beanName.contains(ContextParentBean.TARGET_SOURCE_SUFFIX)) {
            ExportTargetSource elits = (ExportTargetSource) ctx.getBean(beanName, TargetSource.class);
            if (elits.getBeanFactory() != null) {
                return false;
            }//from  w ww.j a  v  a 2 s.co  m
        }
    }
    return true;
}

From source file:me.zhuoran.amoeba.netty.server.HttpServer.java

public HttpServer(final int port, ApplicationContext ctx) {
    this.port = port;
    HttpServer.ctx = ctx;/*from  w  w  w. j a v  a 2s .  c o  m*/
    String[] executorNames = ctx.getBeanNamesForType(AbstractExecutor.class);
    for (String beanName : executorNames) {
        executorNameList.add(beanName.toLowerCase());
        logger.debug("Spring loaded bean name " + beanName.toLowerCase());
    }
}

From source file:org.spring.data.gemfire.AbstractGemFireTest.java

protected void printBeanNames(ApplicationContext applicationContext, Class<?> beanType) {
    for (String beanName : applicationContext.getBeanNamesForType(beanType)) {
        System.out.printf("%1$s%n", beanName);
    }//from  ww w.  j  a  va2  s.co m
}

From source file:org.dphibernate.serialization.SpringContextSerializerFactory.java

String getUUniqueBeanName(ApplicationContext context, Class beanClass) {
    String[] beanNames = context.getBeanNamesForType(beanClass);
    if (beanNames.length == 0) {
        return null;
    }/*from  w  w  w  .j a  v a  2  s.  c  om*/
    if (beanNames.length > 1) {
        throw new RuntimeException(
                "More than one Serializer is configured in the Spring context.  Ensure exactly one one ISerializer instance is declared");
    }
    return beanNames[0];
}

From source file:ch.ralscha.extdirectspring.controller.MethodRegistrar.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {

    ApplicationContext context = (ApplicationContext) event.getSource();

    String[] beanNames = context.getBeanNamesForType(Object.class);

    for (String beanName : beanNames) {

        Class<?> handlerType = context.getType(beanName);
        final Class<?> userType = ClassUtils.getUserClass(handlerType);

        Set<Method> methods = MethodIntrospector.selectMethods(userType, new MethodFilter() {
            @Override// ww  w.  j a  v a2 s .  co m
            public boolean matches(Method method) {
                return AnnotationUtils.findAnnotation(method, ExtDirectMethod.class) != null;
            }
        });

        for (Method method : methods) {
            ExtDirectMethod directMethodAnnotation = AnnotationUtils.findAnnotation(method,
                    ExtDirectMethod.class);
            final String beanAndMethodName = beanName + "." + method.getName();
            if (directMethodAnnotation.value().isValid(beanAndMethodName, userType, method)) {
                this.methodInfoCache.put(beanName, handlerType, method, event.getApplicationContext());

                // /CLOVER:OFF
                if (log.isDebugEnabled()) {
                    String info = "Register " + beanAndMethodName + "(" + directMethodAnnotation.value();
                    if (StringUtils.hasText(directMethodAnnotation.group())) {
                        info += ", " + directMethodAnnotation.group();
                    }
                    info += ")";
                    log.debug(info);
                }
                // /CLOVER:ON
            }
        }

    }
}

From source file:org.langera.freud.optional.spring.SpringBeanIterator.java

public SpringBeanIterator(final ApplicationContext applicationContext, final boolean alertOnEmptyIterator,
        final Class<? extends Integer> beansType) {
    this(applicationContext, alertOnEmptyIterator, applicationContext.getBeanNamesForType(beansType));
}

From source file:org.tangram.spring.TangramServlet.java

/**
 * Obtain view handler and expose application scope to the servlet context.
 *
 * resolves the view handler instance from the application context and exposes any bean on that context
 * to the servleet context scope if the servlet layer.
 *
 * @param context (spring) application context
 *//*ww w. j  a  v  a2s . c om*/
@Override
protected void initStrategies(ApplicationContext context) {
    super.initStrategies(context);
    viewHandler = context.getBeansOfType(ViewHandler.class).values().iterator().next();
    for (String name : context.getBeanNamesForType(Object.class)) {
        getServletContext().setAttribute(name, context.getBean(name));
    } // for
    if (viewHandler == null) {
        throw new RuntimeException("no view handler");
    } // if
}

From source file:fr.itinerennes.bundler.cli.GtfsItinerennesBundler.java

private List<AbstractTask> verifySelectedTasksExists(final ApplicationContext ctx) {
    if (tasks.isEmpty()) {
        tasks.addAll(Arrays.asList(ctx.getBeanNamesForType(AbstractTask.class)));
    }//  www. ja  v a2s.  co  m
    final List<AbstractTask> selectedTasks = new ArrayList<AbstractTask>();
    final List<String> missingTasks = new ArrayList<String>();
    for (final String tName : tasks) {
        if (ctx.containsBean(tName)) {
            selectedTasks.add(ctx.getBean(tName, AbstractTask.class));
            LOGGER.info("Added task '{}' in execution queue", tName);
        } else {
            missingTasks.add(tName);
            LOGGER.error("Task '{}' doesn't exist", tName);
        }
    }
    if (!missingTasks.isEmpty()) {
        System.exit(1);
    }
    return selectedTasks;
}

From source file:com.agiletec.ConfigTestUtils.java

/**
 * Effettua la chiusura dei datasource definiti nel contesto.
 * @param applicationContext Il contesto dell'applicazione.
 * @throws Exception In caso di errore nel recupero o chiusura dei DataSource.
 *//*w ww  .  j  a  v a  2s. com*/
public void closeDataSources(ApplicationContext applicationContext) throws Exception {
    String[] dataSourceNames = applicationContext.getBeanNamesForType(BasicDataSource.class);
    for (int i = 0; i < dataSourceNames.length; i++) {
        BasicDataSource dataSource = (BasicDataSource) applicationContext.getBean(dataSourceNames[i]);
        if (null != dataSource) {
            dataSource.close();
        }
    }
}