Chapter 5. Using Quarkus dependency injection
Dependency injection enables a service to be used in a way that is completely independent of any client consumption. It separates the creation of client dependencies from the client’s behavior, which enables program designs to be loosely coupled.
Dependency injection in Red Hat build of Quarkus is based on Quarkus ArC which is a CDI-based build-time oriented dependency injection solution tailored for Quarkus architecture. Because ArC is a transitive dependency of quarkus-resteasy, and quarkus-resteasy is a dependency of your project, ArC will already be downloaded.
Prerequisites
- You have created the Quarkus Getting Started project.
Procedure
To modify the application and add a companion bean, create the
src/main/java/org/acme/quickstart/GreetingService.javafile with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the
src/main/java/org/acme/quickstart/GreetingResource.javato inject theGreetingServiceand create a new endpoint using it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you stopped the application, enter the following command to restart it:
./mvnw compile quarkus:dev
./mvnw compile quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow To verify that the endpoint returns
hello quarkus, enter the following command in a new terminal window:curl -w "\n" http://localhost:8080/hello/greeting/quarkus hello quarkus
curl -w "\n" http://localhost:8080/hello/greeting/quarkus hello quarkusCopy to Clipboard Copied! Toggle word wrap Toggle overflow