A small example of how to use dependency injection in Java using the jakarta.inject
API and the Weld SE implementation.
Weld SE is an implementation of CDI (Context and Dependency Injection) in Java SE. The CDI specification is based on JSR-330 for dependency injection, which allows injecting dependencies into objects without the need to explicitly create instances in the code.
HelloWorldPrinter
: Interface that defines theprint
method to print a Hello WorldHelloWorldPrinterCapitalize
: Implementation ofHelloWorldPrinter
that prints Hello World in capitalized format.HelloWorldPrinterUppercase
: Implementation ofHelloWorldPrinter
that prints Hello World in uppercase format.HelloWorldClient
: Client that injects all implementations ofHelloWorldPrinter
and calls theprint
method of each.