org.axonframework.spring.config.MessageHandlerLookup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of axon-spring Show documentation
Show all versions of axon-spring Show documentation
Module providing Spring specific helper functionality to ease configuration / set-up of an Axon application, as
well as some Spring specific infrastructure components.
/*
* Copyright (c) 2010-2022. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.axonframework.spring.config;
import org.axonframework.common.ObjectUtils;
import org.axonframework.common.ReflectionUtils;
import org.axonframework.common.annotation.AnnotationUtils;
import org.axonframework.messaging.Message;
import org.axonframework.messaging.annotation.MessageHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.annotation.OrderUtils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
/**
* A {@link BeanDefinitionRegistryPostProcessor} implementation that detects beans with Axon Message handlers and
* registers an {@link MessageHandlerConfigurer} to have these handlers registered in the Axon
* {@link org.axonframework.config.Configuration}.
*
* @author Allard Buijze
* @since 4.6.0
*/
public class MessageHandlerLookup implements BeanDefinitionRegistryPostProcessor {
private static final Logger logger = LoggerFactory.getLogger(MessageHandlerLookup.class);
/**
* Returns a list of beans found in the given {@code register} that contain a handler for the given
* {@code messageType}. The search will not consider prototype beans (or any other non-singleton or abstract bean
* definitions).
*
* @param messageType The type of message to find handlers for.
* @param registry The registry to find these handlers in.
* @return A list of bean names with message handlers.
*/
public static List messageHandlerBeans(Class extends Message>> messageType,
ConfigurableListableBeanFactory registry) {
return messageHandlerBeans(messageType, registry, false);
}
/**
* Returns a list of beans found in the given {@code register} that contain a handler for the given
* {@code messageType}. The search will only consider prototype beans (or any other non-singleton or abstract bean
* definitions) when {@code includePrototypeBeans} is {@code true}.
*
* @param messageType The type of message to find handlers for.
* @param registry The registry to find these handlers in.
* @return A list of bean names with message handlers.
*/
public static List messageHandlerBeans(Class extends Message>> messageType,
ConfigurableListableBeanFactory registry,
boolean includePrototypeBeans) {
List found = new ArrayList<>();
for (String beanName : registry.getBeanDefinitionNames()) {
BeanDefinition bd = registry.getBeanDefinition(beanName);
if (includePrototypeBeans || (bd.isSingleton() && !bd.isAbstract())) {
Class> beanType = registry.getType(beanName);
if (beanType != null && hasMessageHandler(messageType, beanType)) {
found.add(beanName);
}
}
}
return found;
}
private static boolean hasMessageHandler(Class extends Message>> messageType, Class> beanType) {
for (Method m : ReflectionUtils.methodsOf(beanType)) {
Optional