Broadleaf Microservices

Overriding Broadleaf Components

Table of Contents

Broadleaf comes with hundreds of Spring components, all of which can be overridden or customized in one way or another. A simple but common example is to override the DefaultCheckoutService to change how an order confirmation number gets generated in the CartOps Microservice.

Overrides

If we look for implementations of the CheckoutService interface we see DefaultCheckoutService instantiated. The CartOps service defines this bean with Spring’s @ConditionalOnMissingBean in its configuration class, which means that we need to create an override for the CheckoutService bean. Let’s start with creating a subclass of DefaultCheckoutService:

package com.mycompany.cartops;

public class MyCheckoutService extends DefaultCheckoutService {
    public MyCheckoutService(CheckoutWorkflow checkoutWorkflow, CartProvider cartProvider,
            TypeFactory typeFactory, CheckoutCompletionProducer checkoutCompletionProducer,
            CartStatusValidationHelper cartStatusValidationHelper) {
        super(checkoutWorkflow, cartProvider, typeFactory, checkoutCompletionProducer,
                cartStatusValidationHelper);
    }

    @Override
    protected String createOrderNumber(@NonNull Cart cart) {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
    }
}

Spring Configuration

Finally, you’ll want to register your component with Spring either by component scanning or explicitly defining the @Bean in your own configuration class

package com.mycompany.cartops;

@Configuration
public class CartOperationsServiceAutoConfiguration {

    @Bean
    @Primary
    CheckoutService myCheckoutService(CheckoutWorkflow checkoutWorkflow,
            CartProvider cartProvider,
            TypeFactory typeFactory,
            CheckoutCompletionProducer checkoutCompletionProducer,
            CartStatusValidationHelper cartStatusValidationHelper) {
        return new MyCheckoutService(checkoutWorkflow,
                cartProvider,
                typeFactory,
                checkoutCompletionProducer,
                cartStatusValidationHelper);
    }

}