Example usage for org.springframework.messaging.handler HandlerMethod getBean

List of usage examples for org.springframework.messaging.handler HandlerMethod getBean

Introduction

In this page you can find the example usage for org.springframework.messaging.handler HandlerMethod getBean.

Prototype

public Object getBean() 

Source Link

Document

Return the bean for this handler method.

Usage

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

/**
 * Register a handler method and its unique mapping.
 * @param handler the bean name of the handler or the handler instance
 * @param method the method to register//  w w w.j a v  a 2 s  . co  m
 * @param mapping the mapping conditions associated with the handler method
 * @throws IllegalStateException if another method was already registered
 * under the same mapping
 */
protected void registerHandlerMethod(Object handler, Method method, T mapping) {
    Assert.notNull(mapping, "Mapping must not be null");
    HandlerMethod newHandlerMethod = createHandlerMethod(handler, method);
    HandlerMethod oldHandlerMethod = this.handlerMethods.get(mapping);

    if (oldHandlerMethod != null && !oldHandlerMethod.equals(newHandlerMethod)) {
        throw new IllegalStateException("Ambiguous mapping found. Cannot map '" + newHandlerMethod.getBean()
                + "' bean method \n" + newHandlerMethod + "\nto " + mapping + ": There is already '"
                + oldHandlerMethod.getBean() + "' bean method\n" + oldHandlerMethod + " mapped.");
    }

    this.handlerMethods.put(mapping, newHandlerMethod);
    if (logger.isInfoEnabled()) {
        logger.info("Mapped \"" + mapping + "\" onto " + newHandlerMethod);
    }

    for (String pattern : getDirectLookupDestinations(mapping)) {
        this.destinationLookup.add(pattern, mapping);
    }
}

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

/**
 * Find an {@code @MessageExceptionHandler} method for the given exception.
 * The default implementation searches methods in the class hierarchy of the
 * HandlerMethod first and if not found, it continues searching for additional
 * {@code @MessageExceptionHandler} methods among the configured
 * {@linkplain org.springframework.messaging.handler.MessagingAdviceBean
 * MessagingAdviceBean}, if any.//from   w ww .  j  a v a  2  s.c  om
 * @param handlerMethod the method where the exception was raised
 * @param exception the raised exception
 * @return a method to handle the exception, or {@code null}
 * @since 4.2
 */
@Nullable
protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching methods to handle " + exception.getClass().getSimpleName());
    }
    Class<?> beanType = handlerMethod.getBeanType();
    AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(beanType);
    if (resolver == null) {
        resolver = createExceptionHandlerMethodResolverFor(beanType);
        this.exceptionHandlerCache.put(beanType, resolver);
    }
    Method method = resolver.resolveMethod(exception);
    if (method != null) {
        return new InvocableHandlerMethod(handlerMethod.getBean(), method);
    }
    for (MessagingAdviceBean advice : this.exceptionHandlerAdviceCache.keySet()) {
        if (advice.isApplicableToBeanType(beanType)) {
            resolver = this.exceptionHandlerAdviceCache.get(advice);
            method = resolver.resolveMethod(exception);
            if (method != null) {
                return new InvocableHandlerMethod(advice.resolveBean(), method);
            }
        }
    }
    return null;
}

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

/**
 * Register a handler method and its unique mapping.
 * <p><strong>Note:</strong> This method is protected and can be invoked by
 * sub-classes. Keep in mind however that the registration is not protected
 * for concurrent use, and is expected to be done on startup.
 * @param handler the bean name of the handler or the handler instance
 * @param method the method to register/*w  w  w  . j  av  a2  s  .c o m*/
 * @param mapping the mapping conditions associated with the handler method
 * @throws IllegalStateException if another method was already registered
 * under the same mapping
 */
protected final void registerHandlerMethod(Object handler, Method method, T mapping) {
    Assert.notNull(mapping, "Mapping must not be null");
    HandlerMethod newHandlerMethod = createHandlerMethod(handler, method);
    HandlerMethod oldHandlerMethod = this.handlerMethods.get(mapping);

    if (oldHandlerMethod != null && !oldHandlerMethod.equals(newHandlerMethod)) {
        throw new IllegalStateException("Ambiguous mapping found. Cannot map '" + newHandlerMethod.getBean()
                + "' bean method \n" + newHandlerMethod + "\nto " + mapping + ": There is already '"
                + oldHandlerMethod.getBean() + "' bean method\n" + oldHandlerMethod + " mapped.");
    }

    this.handlerMethods.put(mapping, newHandlerMethod);

    for (String pattern : getDirectLookupMappings(mapping)) {
        this.destinationLookup.add(pattern, mapping);
    }
}

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

/**
 * Find an exception handling method for the given exception.
 * <p>The default implementation searches methods in the class hierarchy of
 * the HandlerMethod first and if not found, it continues searching for
 * additional handling methods registered via
 * {@link #registerExceptionHandlerAdvice}.
 * @param handlerMethod the method where the exception was raised
 * @param ex the exception raised or signaled
 * @return a method to handle the exception, or {@code null}
 *//*from w w  w  .j  av a2s.c o  m*/
@Nullable
public InvocableHandlerMethod initExceptionHandlerMethod(HandlerMethod handlerMethod, Throwable ex) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching for methods to handle " + ex.getClass().getSimpleName());
    }
    Class<?> beanType = handlerMethod.getBeanType();
    AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(beanType);
    if (resolver == null) {
        resolver = this.exceptionMethodResolverFactory.apply(beanType);
        this.exceptionHandlerCache.put(beanType, resolver);
    }
    InvocableHandlerMethod exceptionHandlerMethod = null;
    Method method = resolver.resolveMethod(ex);
    if (method != null) {
        exceptionHandlerMethod = new InvocableHandlerMethod(handlerMethod.getBean(), method);
    } else {
        for (Map.Entry<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver> entry : this.exceptionHandlerAdviceCache
                .entrySet()) {
            MessagingAdviceBean advice = entry.getKey();
            if (advice.isApplicableToBeanType(beanType)) {
                resolver = entry.getValue();
                method = resolver.resolveMethod(ex);
                if (method != null) {
                    exceptionHandlerMethod = new InvocableHandlerMethod(advice.resolveBean(), method);
                    break;
                }
            }
        }
    }
    if (exceptionHandlerMethod != null) {
        logger.debug("Found exception handler " + exceptionHandlerMethod.getShortLogMessage());
        exceptionHandlerMethod.setArgumentResolvers(this.argumentResolvers.getResolvers());
    } else {
        logger.error("No exception handling method", ex);
    }
    return exceptionHandlerMethod;
}