Example usage for org.springframework.context ApplicationContext getType

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

Introduction

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

Prototype

@Nullable
Class<?> getType(String name) throws NoSuchBeanDefinitionException;

Source Link

Document

Determine the type of the bean with the given name.

Usage

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"));
        }/*  ww w . j a v a2 s  .  c o m*/
        classMap.put(name, c);
        beanMap.put(name, beanName);
    }

}

From source file:org.craftercms.commons.ebus.config.EBusBeanAutoConfiguration.java

@Override
public void onApplicationEvent(final ContextRefreshedEvent contextRefreshedEvent) {
    ApplicationContext ctx = contextRefreshedEvent.getApplicationContext();

    if (applicationContext != ctx) {
        return;// w w  w  .j a  va2  s .  co m
    }

    if (null == beanResolver) {
        beanResolver = new BeanFactoryResolver(ctx);
    }

    if (null == conversionService) {
        try {
            conversionService = ctx.getBean(ConsumerBeanAutoConfiguration.REACTOR_CONVERSION_SERVICE_BEAN_NAME,
                    ConversionService.class);
        } catch (BeansException be) {
            // TODO: log that conversion service is not found.
        }
    }

    synchronized (this) {
        if (started) {
            return;
        }

        Set<Method> methods;
        Class<?> type;
        for (String beanName : ctx.getBeanDefinitionNames()) {
            type = ctx.getType(beanName);
            methods = findHandlerMethods(type, LISTENER_METHOD_FILTER);
            if (methods != null && methods.size() > 0) {
                wireBean(ctx.getBean(beanName), methods);
            }
        }

        started = true;
    }
}

From source file:org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.java

@Override
public void afterPropertiesSet() {
    if (this.argumentResolvers.getResolvers().isEmpty()) {
        this.argumentResolvers.addResolvers(initArgumentResolvers());
    }/* www .  j  av a 2  s .  c o 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.messaging.handler.invocation.AbstractMethodMessageHandler.java

/**
 * Detect if the given handler has any methods that can handle messages and if
 * so register it with the extracted mapping information.
 * @param handler the handler to check, either an instance of a Spring bean name
 */// www  . ja  v a 2s  .  co m
protected final void detectHandlerMethods(final Object handler) {
    Class<?> handlerType;
    if (handler instanceof String) {
        ApplicationContext context = getApplicationContext();
        Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
        handlerType = context.getType((String) handler);
    } else {
        handlerType = handler.getClass();
    }

    if (handlerType != null) {
        final Class<?> userType = ClassUtils.getUserClass(handlerType);
        Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
                (MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
        if (logger.isDebugEnabled()) {
            logger.debug(methods.size() + " message handler methods found on " + userType + ": " + methods);
        }
        methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
    }
}

From source file:org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.java

/**
 * Detect if the given handler has any methods that can handle messages and if
 * so register it with the extracted mapping information.
 * <p><strong>Note:</strong> This method is protected and can be invoked by
 * sub-classes, but this should be done on startup only as documented in
 * {@link #registerHandlerMethod}.//w w w  .ja v  a 2  s .  c om
 * @param handler the handler to check, either an instance of a Spring bean name
 */
protected final void detectHandlerMethods(Object handler) {
    Class<?> handlerType;
    if (handler instanceof String) {
        ApplicationContext context = getApplicationContext();
        Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
        handlerType = context.getType((String) handler);
    } else {
        handlerType = handler.getClass();
    }
    if (handlerType != null) {
        final Class<?> userType = ClassUtils.getUserClass(handlerType);
        Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
                (MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
        if (logger.isDebugEnabled()) {
            logger.debug(formatMappings(userType, methods));
        }
        methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
    }
}

From source file:org.springframework.web.socket.server.endpoint.EndpointExporter.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) {

    this.applicationContext = applicationContext;

    this.serverContainer = getServerContainer();

    Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ServerEndpoint.class);
    for (String beanName : beans.keySet()) {
        Class<?> beanType = applicationContext.getType(beanName);
        if (logger.isInfoEnabled()) {
            logger.info(//from   w ww.j a va 2 s . co m
                    "Detected @ServerEndpoint bean '" + beanName + "', registering it as an endpoint by type");
        }
        this.annotatedEndpointBeanTypes.add(beanType);
    }
}

From source file:org.springframework.web.socket.server.standard.ServerEndpointExporter.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
    this.serverContainer = getServerContainer();
    Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ServerEndpoint.class);
    for (String beanName : beans.keySet()) {
        Class<?> beanType = applicationContext.getType(beanName);
        if (logger.isInfoEnabled()) {
            logger.info(//  w  ww  . j  a va  2  s . c  o m
                    "Detected @ServerEndpoint bean '" + beanName + "', registering it as an endpoint by type");
        }
        this.annotatedEndpointBeanTypes.add(beanType);
    }
}