DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • Getting Started With the YugabyteDB Managed REST API
  • File Upload Security and Malware Protection

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • Getting Started With the YugabyteDB Managed REST API
  • File Upload Security and Malware Protection
  1. DZone
  2. Coding
  3. Frameworks
  4. Another Spring Boot Integration: Axon

Another Spring Boot Integration: Axon

Lieven Doclo user avatar by
Lieven Doclo
·
Aug. 25, 14 · Interview
Like (0)
Save
Tweet
Share
10.27K Views

Join the DZone community and get the full member experience.

Join For Free

Creating Spring Boot integrations for frameworks is just so easy that I couldn’t resist making another one. One of my recent posts covered CQRS principles and Axon is a framework that currently is the reference when it comes to implementing CQRS and event-sourcing in Java. As it happens it also has great integration with Spring.

Autoconfigurations with Spring Boot are actually just configurations with conditionals to get out of the way if you want to override the auto configuration. In the case of Axon, the autoconfiguration is very easy, defining an eventbus, eventstore, commandbus and gateway, in addition to the postprocessors than link annotated commandhandlers and eventhandlers to their buses.

So, without further ado, the configuration for adding Axon features to Spring Boot:

@Configuration
@ConditionalOnClass(CommandBus)
class AxonConfiguration {
    @Bean
    @ConditionalOnMissingBean
    CommandBus commandBus() {
         def bus = new SimpleCommandBus();
        return bus
    }

    @Bean
    @ConditionalOnMissingBean
    CommandGateway commandGateway() {
        def gateway = new DefaultCommandGateway(commandBus());
        return gateway
    }

    @Bean
    @ConditionalOnMissingBean
    EventBus eventBus() {
        def bus =  new SimpleEventBus()
        return bus
    }

    @Bean
    @ConditionalOnMissingBean
    EventStore eventStore() {
        def tempDir = File.createTempDir("axon", "_events")
        def resolver = new SimpleEventFileResolver(tempDir);
        def store =  new FileSystemEventStore(resolver)
        return store
    }

    @Bean
    AnnotationCommandHandlerBeanPostProcessor annotationCommandHandlerBeanPostProcessor() {
        def p =  new AnnotationCommandHandlerBeanPostProcessor()
        p.commandBus = commandBus()
        return p
    }

    @Bean
    AnnotationEventListenerBeanPostProcessor annotationEventListenerBeanPostProcessor() {
        def p = new AnnotationEventListenerBeanPostProcessor()
        p.eventBus = eventBus()
        return p
    }
}

If you now create a method annotated with @CommandHandler and send a command through the gateway, this configuration will make sure the method gets called.

You can always declare your own EventBus, CommandBus, EventStore or CommandGateway in your configuration and the autoconfigured beans will be ignored. So if you want the EventStore persisting events to a MongoDB or through JPA, you just need to create the beans in your config.

Currently, Spring Boot has a lot of integrations in its autoconfigure library but more can always be added. Axon seemed like a valid addition to that library (as is Hystrix covered in an earlier article). For example, Vaadin has also provided a library that provides Spring Boot integration. Another prime candidate would be Activiti. Spring Boot takes a lot of the guesswork out of integrating frameworks. If a framework has integration with Spring, you can make an autoconfiguration for Spring Boot.

Spring Framework Spring Boot Integration

Published at DZone with permission of Lieven Doclo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • Getting Started With the YugabyteDB Managed REST API
  • File Upload Security and Malware Protection

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: