CDI-based Dependency Injection

In the previous step, you created a basic RESTful Java application with Quarkus. In this step, we'll add a custom bean that will use the ArC extension which provides a CDI-based dependency injection solution tailored for the Quarkus architecture.

Adding a Custom Bean

Let’s modify the application and add a companion bean. Before we get started, keep in mind that we can leave the application running; we don't need to stop and restart it to pick up the changes. Create the following file:

src/main/java/org/acme/quickstart/GreetingService.java

and add the following contents:

package org.acme.quickstart;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class GreetingService {

    private String hostname = System.getenv().getOrDefault("HOSTNAME", "unknown");

    public String greeting(String name) {
        return "hello " + name + " from " + hostname;
    }

}
Next, the injection point needs to be added to the existing GreetingResource.java file. For simplicity, you can replace its existing contents with the following:

 

package org.acme.quickstart;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @Inject
    GreetingService service;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/greeting/{name}")
    public String greeting(@PathParam("name") String name) {
        return service.greeting(name);
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello RESTEasy";
    }
}

Inspecting the Results

Since we still have our app running using mvn quarkus:dev, when you make these changes and reload the endpoint, Quarkus will notice all of these changes and reload them automatically.

 

The new endpoint can be accessed using the following command:
curl localhost:8080/hello/greeting/quarkus

The result should be similar to the following:

hello quarkus from demo

In this case, demo is the hostname of the local host the Quarkus process is running on. It will be different when it's deployed to Kubernetes.

Next Steps

It's a familiar CDI-based environment for Enterprise Java developers, with powerful mechanisms to reload your code in near real-time. In the next step, we'll package and run it as a standalone executable JAR, which should be familiar to microservice developers.
Daniel Oh
Daniel Oh
Senior Principal Technical Marketing Manager
Daniel Oh is a senior principal technical marketing manager at Red Hat to evangelize developers for building Cloud-Native Microservices and Serverless Functions with Cloud-Native Runtimes(i.e. Quarkus, Spring Boot, Node.js) and OpenShift/Kubernetes. Daniel also continues to contribute to various cloud open-source projects and ecosystems as a CNCF ambassador for accelerating DevOps adoption in enterprises. He's speaking at lots of technical seminars, workshops, and meetups to elaborate on new emerging technologies for enterprise developers & DevOps teams.