Skip to content

Commit

Permalink
Added @saga annotation parameter to specify SagaConfiguration bean name
Browse files Browse the repository at this point in the history
This to be able to override the default lookup mechanism based on bean
name, which is error-prone.
  • Loading branch information
abuijze committed Dec 1, 2017
1 parent 302a68b commit 699498d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
Expand Up @@ -32,12 +32,7 @@
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.eventsourcing.eventstore.EventStore;
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine;
import org.axonframework.messaging.annotation.MultiParameterResolverFactory;
import org.axonframework.messaging.annotation.ParameterResolverFactory;
import org.axonframework.messaging.annotation.SimpleResourceParameterResolverFactory;
import org.axonframework.messaging.annotation.FixedValueParameterResolver;
import org.axonframework.messaging.annotation.ParameterResolver;
import org.axonframework.messaging.annotation.ParameterResolverFactory;
import org.axonframework.messaging.annotation.*;
import org.axonframework.messaging.correlation.CorrelationDataProvider;
import org.axonframework.messaging.correlation.SimpleCorrelationDataProvider;
import org.axonframework.serialization.Serializer;
Expand All @@ -59,12 +54,12 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import static java.util.Collections.singleton;
import java.lang.reflect.Executable;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

import static java.util.Collections.singleton;
import static org.axonframework.eventhandling.GenericEventMessage.asEventMessage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -100,6 +95,7 @@ public void testContextInitialization() throws Exception {
assertEquals(0, applicationContext.getBeansOfType(TokenStore.class).size());
assertNotNull(applicationContext.getBean(Context.MySaga.class));
assertNotNull(applicationContext.getBean(Context.MyAggregate.class));
assertNotNull(applicationContext.getBean("myDefaultConfigSagaConfiguration", SagaConfiguration.class));

assertEquals(2, configuration.correlationDataProviders().size());

Expand Down Expand Up @@ -156,7 +152,7 @@ public void on(String type, SomeComponent test) {
}
}

@Saga
@Saga(configurationBean = "myCustomNamedSagaConfiguration")
public static class MySaga {
@SagaEventHandler(associationProperty = "toString")
public void handle(String type, SomeComponent test) {
Expand All @@ -165,8 +161,17 @@ public void handle(String type, SomeComponent test) {

}

@Saga
public static class MyDefaultConfigSaga {
@SagaEventHandler(associationProperty = "toString")
public void handle(String type, SomeComponent test) {

}

}

@Bean
public SagaConfiguration<MySaga> mySagaConfiguration() {
public SagaConfiguration<MySaga> myCustomNamedSagaConfiguration() {
return SagaConfiguration.subscribingSagaManager(MySaga.class);
}

Expand Down
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2010-2017. 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
Expand All @@ -23,13 +24,7 @@
import org.axonframework.common.lock.LockFactory;
import org.axonframework.common.lock.NullLockFactory;
import org.axonframework.common.transaction.TransactionManager;
import org.axonframework.config.AggregateConfigurer;
import org.axonframework.config.Configuration;
import org.axonframework.config.Configurer;
import org.axonframework.config.DefaultConfigurer;
import org.axonframework.config.EventHandlingConfiguration;
import org.axonframework.config.ModuleConfiguration;
import org.axonframework.config.SagaConfiguration;
import org.axonframework.config.*;
import org.axonframework.eventhandling.EventBus;
import org.axonframework.eventhandling.EventMessage;
import org.axonframework.eventhandling.ListenerInvocationErrorHandler;
Expand Down Expand Up @@ -237,9 +232,11 @@ private void registerSagaBeanDefinitions(Configurer configurer) {
for (String saga : sagas) {
Saga sagaAnnotation = beanFactory.findAnnotationOnBean(saga, Saga.class);
Class<?> sagaType = beanFactory.getType(saga);

String configName = lcFirst(sagaType.getSimpleName()) + "Configuration";
if (beanFactory.containsBean(configName)) {
boolean explicitSagaConfig = !"".equals(sagaAnnotation.configurationBean());
String configName = explicitSagaConfig
? sagaAnnotation.configurationBean()
: lcFirst(sagaType.getSimpleName()) + "Configuration";
if (explicitSagaConfig || beanFactory.containsBean(configName)) {
configurer.registerModule(new LazyRetrievedModuleConfiguration(
() -> beanFactory.getBean(configName, ModuleConfiguration.class))
);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2016. Axon Framework
* Copyright (c) 2010-2017. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,4 +39,9 @@
*/
String sagaStore() default "";

/**
* Defines the name of the bean that configures this Saga type. When defined, a bean of type {@link org.axonframework.config.SagaConfiguration} with such name must exist. If not defined, Axon will attempt to locate a bean named `&lt;sagaSimpleClassName&gt;Configuration`, creating a default configuration if none is found.
*/
String configurationBean() default "";

}

0 comments on commit 699498d

Please sign in to comment.