Jakarta CDI - Lab 1 - Basic part 1
In this lab, we will explore creating the first injection where the Vehicle interface into a single implementation Car.
1. Defining the interface
Steps
- Open the
01-jakarta-ee
project and navigate to thesrc/main/java
- Create an interface called
Vehicle
in theexpert.os.labs.persistence.cid.vehicle
package -
Add a method called
move()
Info
The
move()
method represents the action of moving a vehicle. Any class implementing this interface must provide an implementation for this method.
Expected results
- Interface
Vehicle
created in thesrc/main/java
at theexpert.os.labs.persistence.cdi.vehicle
Solution
2. Defining the class
Steps
- Create a class called
Car
in theexpert.os.labs.persistence.cid.vehicle
package - Implements the
Vehicle
interface -
Annotate the
Car
class with the@ApplicationScoped
annotation fromjakarta.enterprise.context
packageInfo
This annotation is commonly used in Jakarta EE (formerly Java EE) to define a class as an application-scoped bean. It means that only one instance of this class will be created and managed by the Jakarta EE container throughout the application's lifetime.
-
Declare a logger for this class
-
Add a field called
name
of typeString
-
Add a constructor to the class associating a random Sxtring to the
name
fieldTip
You can use the
UUID.randomUUID().toString()
method to generate a random String -
Override the method
move()
by logging thename
of the vehicle using the logger
Expected results
- Class
Car
implementingVehicle
created containing a methodmove()
Solution
Click to see...
import jakarta.enterprise.context.ApplicationScoped;
import java.util.Objects;
import java.util.UUID;
import java.util.logging.Logger;
@ApplicationScoped
public class Car implements Vehicle {
private static final Logger LOGGER = Logger.getLogger(Car.class.getName());
private final String name;
public Car() {
this.name = UUID.randomUUID().toString();
}
@Override
public void move() {
LOGGER.info("My car is moving. The car's name is: " + name);
}
}
3. Explore the class usage
Steps
- Create a class called
AppVehicle
in theexpert.os.labs.persistence.cid
package -
Add the following code to the class
import jakarta.enterprise.inject.se.SeContainer; import jakarta.enterprise.inject.se.SeContainerInitializer; public class AppVehicle { public static void main(String[] args) { try (SeContainer container = SeContainerInitializer.newInstance().initialize()) { Vehicle vehicle = container.select(Vehicle.class).get(); vehicle.move(); Car car = container.select(Car.class).get(); car.move(); } } }
-
Run the class
Expected results
-
Two logs related to the
move()
method execution, one for thevehicle.move()
and another from thecar.move()
showing:
Solution
Click to see...
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
public class AppVehicle {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Vehicle vehicle = container.select(Vehicle.class).get();
vehicle.move();
Car car = container.select(Car.class).get();
car.move();
}
}
}