bookmark_borderSpring boot dependency injection using @ConditionalOnProperty annotation

Suppose that you have a webshop with a credit card payment option. Credit card payments work fine in a production environment. There is a new requirement to implement a sandbox environment for the webshop. Sandbox environment doesn’t charge your credit card. It always returns a successful response.

Each checkout implementation is based on the following CheckoutService interface:

The implementation of CheckoutService an interface for the production environment is BankCheckoutService

CheckoutController gets instance of CheckoutService using @Autowired annotation (dependency injection).

Let us create a new implementation of CheckoutService interface for the sandbox environment:

It will work fine if we replace a constructor of CheckoutController to get SandboxCheckoutService bean:

However, we want to instantiate either BankCheckoutService or SandboxCheckoutService based on the variable in the application properties file. SandboxCheckoutService should be instantiated if application properties contains the following content:

If sandbox the variable is false BankCheckoutService will be instantiated.

Spring Boot already has a solution. @ConditionalOnProperty annotation provides us the possibility to conditionally instantiate bean based on property value from application.properties file.

BankCheckoutService:

SandboxCheckoutService

The controller doesn’t have to be changed since it expects CheckoutService interface which is implemented in both BankCheckoutService and SandboxCheckoutService. Controller depends on abstraction not implementation as it is specified in SOLID principle.

We can now switch between BankCheckoutService and SandboxCheckoutService by changing the sandbox variable in the application.properties file.