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
decorator
in theexpert.os.labs.persistence.cid
package - Create an interface called
Worker
- Create the
worker
method which will set ajob
Solution
2. Create the Programmer
Class
- Create a class called
Programmer
in theexpert.os.labs.persistence.cid.decorator
package - Implements
Worker
and the methodwork()
- the
Consumer
class is from thejava.util.function
package
- the
- Define a private attribute
Logger
from thejava.util.logging
package - In the method
work()
, return aString
based 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
Manager
in theexpert.os.labs.persistence.cid.decorator
package - Implements the
Worker
interface - Annotate the class with
@Decorator
from thejakarta.decorator
package - Add a priority to the decorator using the
@Priority
from thejakarta.annotation
package- the priority must be set as
Interceptor.Priority.APPLICATION
- the priority must be set as
- Add a private field
Worker
annotating it with:@Inject
from thejakarta.inject
package@Delegate
from thejakarta.decorator
package@Any
from thejakarta.enterprise.inject
package
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
AppWorker
in theexpert.os.labs.persistence.cid
package -
Add a main method
-
Set up a try-with-resources block, inside the
main
method, to manage the Jakarta EESeContainer
that is responsible for dependency injection and managing resources -
Obtain the value of the
Worker
through the container instance - Define a work from the
worker
instance - 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);
}
}
}