Skip to content

Commit

Permalink
Merge pull request #2629 from OLibutzki/olibutzki/objectmapper-cannot…
Browse files Browse the repository at this point in the history
…-be-2628

[#2628] Extended support for Spring application context hierarchy
  • Loading branch information
smcvb committed Mar 3, 2023
2 parents 039ddfa + 6091ae5 commit 5b60a19
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors;

import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -181,7 +183,7 @@ private Serializer buildSerializer(RevisionResolver revisionResolver,
SerializerProperties.SerializerType serializerType) {
switch (serializerType) {
case JACKSON:
Map<String, ObjectMapper> objectMapperBeans = applicationContext.getBeansOfType(ObjectMapper.class);
Map<String, ObjectMapper> objectMapperBeans = beansOfTypeIncludingAncestors(applicationContext, ObjectMapper.class);
ObjectMapper objectMapper = objectMapperBeans.containsKey("defaultAxonObjectMapper")
? objectMapperBeans.get("defaultAxonObjectMapper")
: objectMapperBeans.values().stream().findFirst()
Expand All @@ -197,7 +199,7 @@ private Serializer buildSerializer(RevisionResolver revisionResolver,
case XSTREAM:
case DEFAULT:
default:
Map<String, XStream> xStreamBeans = applicationContext.getBeansOfType(XStream.class);
Map<String, XStream> xStreamBeans = beansOfTypeIncludingAncestors(applicationContext, XStream.class);
XStream xStream = xStreamBeans.containsKey("defaultAxonXStream")
? xStreamBeans.get("defaultAxonXStream")
: xStreamBeans.values().stream().findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -76,7 +78,7 @@ public void setApplicationContext(@Nonnull ApplicationContext applicationContext

@Override
public void afterPropertiesSet() {
factories.addAll(applicationContext.getBeansOfType(ParameterResolverFactory.class).values());
factories.addAll(beansOfTypeIncludingAncestors(applicationContext, ParameterResolverFactory.class).values());
parameterResolverFactory = MultiParameterResolverFactory.ordered(factories);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.axonframework.commandhandling.CommandMessageHandler;
import org.axonframework.messaging.MessageHandler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.SmartLifecycle;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void stop(Runnable callback) {
@Override
@SuppressWarnings("unchecked")
public void start() {
if (commandBus == null && !applicationContext.getBeansOfType(CommandBus.class).isEmpty()) {
if (commandBus == null && !beansOfTypeIncludingAncestors(applicationContext, CommandBus.class).isEmpty()) {
commandBus = applicationContext.getBean(CommandBus.class);
}
if (commandHandlers == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -127,8 +129,8 @@ public void setApplicationContext(@Nonnull ApplicationContext applicationContext

private void initialize() {
definitions.addAll(ClasspathHandlerDefinition.forClassLoader(classLoader).getDelegates());
definitions.addAll(applicationContext.getBeansOfType(HandlerDefinition.class).values());
definitions.addAll(beansOfTypeIncludingAncestors(applicationContext, HandlerDefinition.class).values());
enhancers.addAll(ClasspathHandlerEnhancerDefinition.forClassLoader(classLoader).getDelegates());
enhancers.addAll(applicationContext.getBeansOfType(HandlerEnhancerDefinition.class).values());
enhancers.addAll(beansOfTypeIncludingAncestors(applicationContext, HandlerEnhancerDefinition.class).values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -110,8 +112,7 @@ public void setAdditionalHandlers(List<HandlerEnhancerDefinition> additionalFact

private void initialize() {
enhancers.addAll(ClasspathHandlerEnhancerDefinition.forClassLoader(classLoader).getDelegates());
Map<String, HandlerEnhancerDefinition> enhancersFound = applicationContext.getBeansOfType(
HandlerEnhancerDefinition.class);
Map<String, HandlerEnhancerDefinition> enhancersFound = beansOfTypeIncludingAncestors(applicationContext, HandlerEnhancerDefinition.class);
enhancers.addAll(enhancersFound.values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors;

import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -93,11 +95,11 @@ protected AggregateFactory<?> getAggregateFactory(Class<?> aggregateType) {
AggregateFactory<?> aggregateFactory = super.getAggregateFactory(aggregateType);
if (aggregateFactory == null) {
Optional<AggregateFactory> factory =
applicationContext.getBeansOfType(AggregateFactory.class).values().stream()
beansOfTypeIncludingAncestors(applicationContext, AggregateFactory.class).values().stream()
.filter(af -> Objects.equals(af.getAggregateType(), aggregateType))
.findFirst();
if (!factory.isPresent()) {
factory = applicationContext.getBeansOfType(EventSourcingRepository.class).values().stream()
factory = beansOfTypeIncludingAncestors(applicationContext, EventSourcingRepository.class).values().stream()
.map((Function<EventSourcingRepository, AggregateFactory>) EventSourcingRepository::getAggregateFactory)
.filter(af -> Objects.equals(af.getAggregateType(), aggregateType))
.findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -69,7 +71,7 @@ public class SpringAggregateSnapshotterFactoryBean
public SpringAggregateSnapshotter getObject() {
if (transactionManager == null) {
Map<String, PlatformTransactionManager> candidates =
applicationContext.getBeansOfType(PlatformTransactionManager.class);
beansOfTypeIncludingAncestors(applicationContext, PlatformTransactionManager.class);
if (candidates.size() == 1) {
this.transactionManager = candidates.values().iterator().next();
}
Expand All @@ -91,8 +93,8 @@ public SpringAggregateSnapshotter getObject() {
}

if (handlerDefinition == null) {
handlerDefinition = new HandlerDefinitionFactoryBean(new ArrayList<>(applicationContext.getBeansOfType(HandlerDefinition.class).values()),
new ArrayList<>(applicationContext.getBeansOfType(HandlerEnhancerDefinition.class).values()))
handlerDefinition = new HandlerDefinitionFactoryBean(new ArrayList<>(beansOfTypeIncludingAncestors(applicationContext, HandlerDefinition.class).values()),
new ArrayList<>(beansOfTypeIncludingAncestors(applicationContext, HandlerEnhancerDefinition.class).values()))
.getObject();
}

Expand Down

0 comments on commit 5b60a19

Please sign in to comment.