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
LoggerProducer
in theexpert.os.labs.persistence.cid.producer
package - Create a constant to log results using the
Logger
from thejava.util.logging
package - Annotate it with the
@ApplicationScoped
from thejakarta.enterprise.context
package - Add a producer method, annotating it with
@Produces
from thejakarta.enterprise.inject
where:- it has a parameter using the
InjectionPoint
class fromjakarta.enterprise.inject.spi
package - 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
NumberLogger
in theexpert.os.labs.persistence.cid
package - Define a private attribute
Logger
from thejava.util.logging
package - Add a default constructor
- Add a constructor that receives the
Logger
as a parameter and annotate it using the@Inject
class from thejakarta.inject
package - Add a log method to log a
BigDecimal
value
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
AppNumberLog
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
BigDecimal
through the container instance - Obtain the value of the
NumberLogger
through 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);
}
}
}