Jakarta CDI - After Intermediate Lab 1
In this lab, we will explore the CDI decorator using Workers as the sample, and we will decorate it with a manager.
1. Create the LoggerProducer class
Steps
- Create a new package called
decoratorin theexpert.os.labs.persistence.cidpackage - Create an interface called
Worker - Create the
workermethod which will set ajob
Solution
2. Create the Programmer Class
- Create a class called
Programmerin theexpert.os.labs.persistence.cid.decoratorpackage - Implements
Workerand the methodwork()- the
Consumerclass is from thejava.util.functionpackage
- the
- Define a private attribute
Loggerfrom thejava.util.loggingpackage - In the method
work(), return aStringbased on its job
Solution
Click to see...
import jakarta.enterprise.context.ApplicationScoped;
import java.util.logging.Logger;
@ApplicationScoped
public class Programmer implements Worker {
private static final Logger LOGGER = Logger.getLogger(Programmer.class.getName());
@Override
public String work(String job) {
return "A programmer has received a job, it will convert coffee in code: " + job;
}
}
3. Create the Manager Class (Decorator)
Steps
- Create a class called
Managerin theexpert.os.labs.persistence.cid.decoratorpackage - Implements the
Workerinterface - Annotate the class with
@Decoratorfrom thejakarta.decoratorpackage - Add a priority to the decorator using the
@Priorityfrom thejakarta.annotationpackage- the priority must be set as
Interceptor.Priority.APPLICATION
- the priority must be set as
- Add a private field
Workerannotating it with:@Injectfrom thejakarta.injectpackage@Delegatefrom thejakarta.decoratorpackage@Anyfrom thejakarta.enterprise.injectpackage
Solution
Click to see...
import jakarta.annotation.Priority;
import jakarta.decorator.Decorator;
import jakarta.decorator.Delegate;
import jakarta.enterprise.inject.Any;
import jakarta.inject.Inject;
import jakarta.interceptor.Interceptor;
@Decorator
@Priority(Interceptor.Priority.APPLICATION)
public class Manager implements Worker {
@Inject
@Delegate
@Any
private Worker worker;
@Override
public String work(String job) {
return "A manager has received a job and it will delegate to a programmer -> " + worker.work(job);
}
}
6. Create the app class
Steps
- Create a class called
AppWorkerin theexpert.os.labs.persistence.cidpackage -
Add a main method
-
Set up a try-with-resources block, inside the
mainmethod, to manage the Jakarta EESeContainerthat is responsible for dependency injection and managing resources -
Obtain the value of the
Workerthrough the container instance - Define a work from the
workerinstance - Printout the work
Expected results
-
The following output
Solution
Click to see...
import expert.os.labs.persistence.cdi.decorator.Worker;
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
public class AppWorker {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Worker worker = container.select(Worker.class).get();
String work = worker.work("Just a single button");
System.out.println("The work result: " + work);
}
}
}