This guide walks through the options available to inject optional dependencies with Spring DI.

Introduction

There are quite a few use-cases where it’s needed to make optional some of the dependencies injected, here are some of them:

  • Provide a default implementation whenever a required infrastructure dependency is not provided, such as DataSources.
  • Prevent the usage of dependencies such as monitoring strategies depending on the environment.
  • If you are building your own spring-boot auto-configuration component, sometimes it’s necessary to have optional dependencies.

@Autowired

The most know approach to achieve the optional injection is simply to use the @Autowired(required = false), something like this:

@Autowired(required = false)
private HelloService helloService;

It works fine and gets us to where we wanted, however, I don’t recommend anyone to use field injection and one of the reasons for that is the test layer of the application. Whenever field injection is used it’s mandatory to use reflection to inject a different implementation based on the test case.

Java 8 Optional type

You may be familiar with Java 8’s Optional type, it can also be used while injecting dependencies with Spring, here a sample:

@RestController
public class HelloController {

    private Optional<HelloService> optionalHelloService;

    public HelloController(Optional<HelloService> helloService) {
        this.optionalHelloService = helloService;
    }

    @GetMapping("/hello")
    public String hello() {
        return optionalHelloService.map(HelloService::hello)
                .orElse("Hello there, fallback!");
    }

}

The implementation above gives you an Optional monad where you can validate whether the implementation is present before using it.

Spring’s ObjectProvider

Since Spring 4.3 there’s this class named ObjectProvider designed specifically for injection points.

From the javadocs:

A variant of ObjectFactory designed specifically for injection points, allowing for programmatic optionality and lenient not-unique handling.

Using the same example from above:

@RestController
public class HelloController {

    private HelloService helloService;

    public HelloController(ObjectProvider<HelloService> helloServiceProvider) {
        this.helloService = helloServiceProvider.getIfAvailable(DefaultHelloService::new);
    }

    @GetMapping("/hello")
    public String hello() {
        return helloService.hello();
    }

    class DefaultHelloService implements HelloService {

        @Override
        public String hello() {
            return "Hello there, fallback!";
        }
    }

}

In this example, it’s not only optional but also it provides a default implementation as a fallback.

Summary

Congratulations! You just learned few ways to optionally inject dependencies within Spring apps.

Footnote