Skip to content

Instantly share code, notes, and snippets.

@MagnusSmith
Last active March 25, 2022 00:38
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MagnusSmith/8938755 to your computer and use it in GitHub Desktop.
Save MagnusSmith/8938755 to your computer and use it in GitHub Desktop.
Java configuration for Spring with Axon Framework 2.09
package config;
import orders.commandhandling.Order;
import orders.commandhandling.OrderCommandHandler;
import org.axonframework.commandhandling.CommandBus;
import org.axonframework.commandhandling.SimpleCommandBus;
import org.axonframework.commandhandling.annotation.AnnotationCommandHandlerBeanPostProcessor;
import org.axonframework.commandhandling.gateway.CommandGatewayFactoryBean;
import org.axonframework.eventhandling.EventBus;
import org.axonframework.eventhandling.SimpleEventBus;
import org.axonframework.eventhandling.annotation.AnnotationEventListenerBeanPostProcessor;
import org.axonframework.eventsourcing.EventSourcingRepository;
import org.axonframework.eventstore.EventStore;
import org.axonframework.eventstore.fs.FileSystemEventStore;
import org.axonframework.eventstore.fs.SimpleEventFileResolver;
import org.axonframework.repository.Repository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.io.File;
import java.util.Arrays;
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"config", "orders.commandhandling", "orders.query.repositories"})
public class Application {
@Bean
public CommandGatewayFactoryBean commandGateway() {
CommandGatewayFactoryBean factory = new CommandGatewayFactoryBean();
factory.setCommandBus(commandBus());
return factory;
}
@Bean
public OrderCommandHandler orderCommandHandler() {
return new OrderCommandHandler();
}
@Bean
public CommandBus commandBus() {
return new SimpleCommandBus();
}
@Bean
AnnotationCommandHandlerBeanPostProcessor annotationCommandHandlerBeanPostProcessor() {
AnnotationCommandHandlerBeanPostProcessor handler = new AnnotationCommandHandlerBeanPostProcessor();
handler.setCommandBus(commandBus());
return handler;
}
@Bean
public EventStore eventStore() {
return new FileSystemEventStore(new SimpleEventFileResolver(new File("./events")));
}
@Bean
public EventBus eventBus() {
return new SimpleEventBus();
}
@Bean
AnnotationEventListenerBeanPostProcessor annotationEventListenerBeanPostProcessor() {
AnnotationEventListenerBeanPostProcessor listener = new AnnotationEventListenerBeanPostProcessor();
listener.setEventBus(eventBus());
return listener;
}
@Bean(name = "orderRepository")
public Repository<Order> repository() {
EventSourcingRepository repository = new EventSourcingRepository<Order>(Order.class, eventStore());
repository.setEventBus(eventBus());
return repository;
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
}
@MagnusSmith
Copy link
Author

updated to use BeanPostProcessor to handle the subscribing the command handler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment