@Override public void afterPropertiesSet() { if (this.argumentResolvers.getResolvers().isEmpty()) { this.argumentResolvers.addResolvers(initArgumentResolvers()); this.returnValueHandlers.addHandlers(initReturnValueHandlers()); Log returnValueLogger = getReturnValueHandlerLogger(); if (returnValueLogger != null) { this.returnValueHandlers.setLogger(returnValueLogger); this.handlerMethodLogger = getHandlerMethodLogger(); ApplicationContext context = getApplicationContext(); if (context == null) { return; if (beanType != null && isHandler(beanType)) { detectHandlerMethods(beanName);
processHandlerMethodException(handlerMethod, ex, message); processHandlerMethodException(handlerMethod, handlingException, message);
/** * 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 */ 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)); } }
addMatchesToCollection(mappingsByUrl, message, matches); addMatchesToCollection(allMappings, message, matches); handleNoMatch(this.handlerMethods.keySet(), lookupDestination, message); return; Comparator<Match> comparator = new MatchComparator(getMappingComparator(message)); matches.sort(comparator); if (logger.isTraceEnabled()) { handleMatch(bestMatch.mapping, bestMatch.handlerMethod, lookupDestination, message);
@Override public void handleMessage(Message<?> message) throws MessagingException { String destination = getDestination(message); if (destination == null) { return; } String lookupDestination = getLookupDestination(destination); if (lookupDestination == null) { return; } MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message); headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination); headerAccessor.setLeaveMutable(true); message = MessageBuilder.createMessage(message.getPayload(), headerAccessor.getMessageHeaders()); if (logger.isDebugEnabled()) { logger.debug("Searching methods to handle " + headerAccessor.getShortLogMessage(message.getPayload()) + ", lookupDestination='" + lookupDestination + "'"); } handleMessageInternal(message, lookupDestination); headerAccessor.setImmutable(); }
/** * 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 * @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.isTraceEnabled()) { logger.trace("Mapped \"" + mapping + "\" onto " + newHandlerMethod); } for (String pattern : getDirectLookupDestinations(mapping)) { this.destinationLookup.add(pattern, mapping); } }
@Override public String toString() { return getClass().getSimpleName() + "[prefixes=" + getDestinationPrefixes() + "]"; }
/** * Create a HandlerMethod instance from an Object handler that is either a handler * instance or a String-based bean name. */ protected HandlerMethod createHandlerMethod(Object handler, Method method) { HandlerMethod handlerMethod; if (handler instanceof String) { ApplicationContext context = getApplicationContext(); Assert.state(context != null, "ApplicationContext is required for resolving handler bean names"); String beanName = (String) handler; handlerMethod = new HandlerMethod(beanName, context.getAutowireCapableBeanFactory(), method); } else { handlerMethod = new HandlerMethod(handler, method); } return handlerMethod; }
AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(beanType); if (resolver == null) { resolver = createExceptionHandlerMethodResolverFor(beanType); this.exceptionHandlerCache.put(beanType, resolver);
public void registerHandler(Object handler) { super.detectHandlerMethods(handler); }
public void registerHandlerMethod(Object handler, Method method, String mapping) { super.registerHandlerMethod(handler, method, mapping); }
addMatchesToCollection(mappingsByUrl, message, matches); addMatchesToCollection(allMappings, message, matches); handleNoMatch(this.handlerMethods.keySet(), lookupDestination, message); return; Comparator<Match> comparator = new MatchComparator(getMappingComparator(message)); matches.sort(comparator); if (logger.isTraceEnabled()) { handleMatch(bestMatch.mapping, bestMatch.handlerMethod, lookupDestination, message);
@Override public void handleMessage(Message<?> message) throws MessagingException { String destination = getDestination(message); if (destination == null) { return; } String lookupDestination = getLookupDestination(destination); if (lookupDestination == null) { return; } MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message); headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination); headerAccessor.setLeaveMutable(true); message = MessageBuilder.createMessage(message.getPayload(), headerAccessor.getMessageHeaders()); if (logger.isDebugEnabled()) { logger.debug("Searching methods to handle " + headerAccessor.getShortLogMessage(message.getPayload()) + ", lookupDestination='" + lookupDestination + "'"); } handleMessageInternal(message, lookupDestination); headerAccessor.setImmutable(); }
/** * 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 * @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.isTraceEnabled()) { logger.trace("Mapped \"" + mapping + "\" onto " + newHandlerMethod); } for (String pattern : getDirectLookupDestinations(mapping)) { this.destinationLookup.add(pattern, mapping); } }
@Override public String toString() { return getClass().getSimpleName() + "[prefixes=" + getDestinationPrefixes() + "]"; }
/** * Create a HandlerMethod instance from an Object handler that is either a handler * instance or a String-based bean name. */ protected HandlerMethod createHandlerMethod(Object handler, Method method) { HandlerMethod handlerMethod; if (handler instanceof String) { ApplicationContext context = getApplicationContext(); Assert.state(context != null, "ApplicationContext is required for resolving handler bean names"); String beanName = (String) handler; handlerMethod = new HandlerMethod(beanName, context.getAutowireCapableBeanFactory(), method); } else { handlerMethod = new HandlerMethod(handler, method); } return handlerMethod; }
AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(beanType); if (resolver == null) { resolver = createExceptionHandlerMethodResolverFor(beanType); this.exceptionHandlerCache.put(beanType, resolver);
@Override public void afterPropertiesSet() { if (this.argumentResolvers.getResolvers().isEmpty()) { this.argumentResolvers.addResolvers(initArgumentResolvers()); this.returnValueHandlers.addHandlers(initReturnValueHandlers()); Log returnValueLogger = getReturnValueHandlerLogger(); if (returnValueLogger != null) { this.returnValueHandlers.setLogger(returnValueLogger); this.handlerMethodLogger = getHandlerMethodLogger(); ApplicationContext context = getApplicationContext(); if (context == null) { return; if (beanType != null && isHandler(beanType)) { detectHandlerMethods(beanName);
addMatchesToCollection(mappingsByUrl, message, matches); addMatchesToCollection(allMappings, message, matches); handleNoMatch(this.handlerMethods.keySet(), lookupDestination, message); return; Comparator<Match> comparator = new MatchComparator(getMappingComparator(message)); matches.sort(comparator); if (logger.isTraceEnabled()) { handleMatch(bestMatch.mapping, bestMatch.handlerMethod, lookupDestination, message);
/** * 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 */ 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)); } }