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 theprintmethod to print a Hello WorldHelloWorldPrinterCapitalize: Implementation ofHelloWorldPrinterthat prints Hello World in capitalized format.HelloWorldPrinterUppercase: Implementation ofHelloWorldPrinterthat prints Hello World in uppercase format.HelloWorldClient: Client that injects all implementations ofHelloWorldPrinterand calls theprintmethod of each.
