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:org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.java

@Override
public void afterPropertiesSet() {
    if (this.argumentResolvers.getResolvers().isEmpty()) {
        this.argumentResolvers.addResolvers(initArgumentResolvers());
    }/*  w  ww  .  ja v a 2  s  .co  m*/

    if (this.returnValueHandlers.getReturnValueHandlers().isEmpty()) {
        this.returnValueHandlers.addHandlers(initReturnValueHandlers());
    }

    ApplicationContext context = getApplicationContext();
    if (context == null) {
        return;
    }
    for (String beanName : context.getBeanNamesForType(Object.class)) {
        if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
            Class<?> beanType = null;
            try {
                beanType = context.getType(beanName);
            } catch (Throwable ex) {
                // An unresolvable bean type, probably from a lazy bean - let's ignore it.
                if (logger.isDebugEnabled()) {
                    logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
                }
            }
            if (beanType != null && isHandler(beanType)) {
                detectHandlerMethods(beanName);
            }
        }
    }
}

From source file:org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if (ClassUtils.isPresent("org.springframework.security.web.authentication.RememberMeServices", null)) {
        this.usesSpringSessionRememberMeServices = !ObjectUtils
                .isEmpty(applicationContext.getBeanNamesForType(SpringSessionRememberMeServices.class));
    }//from  ww  w .j  av a2s.  c o  m
}

From source file:test.profile.ClassPathXmlJobRegistry.java

public void afterPropertiesSet() throws Exception {

    for (Resource resource : jobPaths) {
        ClassPathXmlApplicationContextFactory applicationContextFactory = new ClassPathXmlApplicationContextFactory();
        applicationContextFactory.setPath(resource);
        applicationContextFactory.setApplicationContext(parent);
        ApplicationContext context = applicationContextFactory.createApplicationContext();
        String[] names = context.getBeanNamesForType(Job.class);

        for (String name : names) {
            logger.debug("Registering job: " + name + " from context: " + resource);
            ApplicationContextJobFactory jobFactory = new ApplicationContextJobFactory(
                    applicationContextFactory, name);
            jobRegistry.register(jobFactory);
        }/*from  w  w w  .  j av  a  2s  .com*/
    }

    if (jobRegistry.getJobNames().isEmpty()) {
        throw new NoSuchJobException("Could not locate any jobs in resources provided.");
    }

}