Broadleaf Microservices
  • v1.0.0-latest-prod

Overriding Broadleaf Components [DEPRECATED]

Important
This guide has been deprecated in favor of the Microservices Concepts project. To take advantage of the new Extensibility Patterns outlined in this project, you will need to upgrade to the latest broadleaf common library dependencies listed here which can be applied to any installation running Release Train 1.7.3 or above. The patterns outlined in this article are still applicable to those running libraries older than those identified above.

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);
    }

}

Flex Package spring.factories Note

If you are going to include the CartOperations Service jar inside a Flex Package composition, you will want to also make sure to define a spring.factories class allowing your Flex Package application to auto-register your auto-configuration class.

i.e. create a src/main/resources/META-INF/spring.factories file with the following contents:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.mycompany.cartops.CartOperationsServiceAutoConfiguration