Jakarta CDI - Intermediate Lab 2
In this lab, we will create an object taking information from who needs this information using the InjectionPoint. Thus, we will create a Logger producer.
1. Create the LoggerProducer class
Steps
- Create a class called
LoggerProducerin theexpert.os.labs.persistence.cid.producerpackage - Create a constant to log results using the
Loggerfrom thejava.util.loggingpackage - Annotate it with the
@ApplicationScopedfrom thejakarta.enterprise.contextpackage - Add a producer method, annotating it with
@Producesfrom thejakarta.enterprise.injectwhere:- it has a parameter using the
InjectionPointclass fromjakarta.enterprise.inject.spipackage - get the class name using the Injection point, associating it with a variable
- log the class name using the logger
- it has a parameter using the
Solution
Click to see...
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.spi.InjectionPoint;
import java.util.logging.Logger;
@ApplicationScoped
public class LoggerProducer {
private static final Logger LOGGER = Logger.getLogger(LoggerProducer.class.getName());
@Produces
Logger getLog(InjectionPoint injectionPoint) {
String declaringClass = injectionPoint.getMember().getDeclaringClass().getName();
LOGGER.info("Creating instance log to " + declaringClass);
return Logger.getLogger(declaringClass);
}
}
Note
The getLog() method is annotated with @Produces, indicating that it is a CDI producer method. This method produces java.util.logging.Logger instances. It takes an InjectionPoint as a parameter, which provides information about the injection point. It retrieves the declaring class name from the injection point and creates a Logger instance for that class name. It also logs a message indicating the creation of the logger.
2. Define a class that uses the produced logger
Steps
- Create a class called
NumberLoggerin theexpert.os.labs.persistence.cidpackage - Define a private attribute
Loggerfrom thejava.util.loggingpackage - Add a default constructor
- Add a constructor that receives the
Loggeras a parameter and annotate it using the@Injectclass from thejakarta.injectpackage - Add a log method to log a
BigDecimalvalue
Solution
Click to see...
import jakarta.inject.Inject;
import java.math.BigDecimal;
import java.util.logging.Logger;
public class NumberLogger {
private Logger logger;
NumberLogger() {
}
@Inject
public NumberLogger(Logger logger) {
this.logger = logger;
}
public void log(BigDecimal value) {
this.logger.info("The BigDecimal value is " + value);
}
}
3. Create the app class
Steps
- Create a class called
AppNumberLogin 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
BigDecimalthrough the container instance - Obtain the value of the
NumberLoggerthrough the container instance - Printout the value of the
NumberLogger - Run the
main()method
Expected results
-
The following output
Solution
Click to see...
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import java.math.BigDecimal;
public class AppNumberLog {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
BigDecimal value = container.select(BigDecimal.class).get();
NumberLogger logger = container.select(NumberLogger.class).get();
logger.log(value);
}
}
}