This project is a sample application demonstrating the use of Contexts and Dependency Injection (CDI) in Java. It includes various examples of CDI concepts such as qualifiers, producers, and dependency injection. Additionally, it presents interceptors and decorators.
For Jakarta EE in a real app, you need to remove the following dependency from the pom.xml file:
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
</dependency>It is only needed for Java SE.
The objective of this project is to demonstrate how to use CDI in Java applications. It includes examples of how to use qualifiers, producers, interceptors, and decorators. The project can be used as a JakartaEE application or as a standalone application.
The easiest entry point is the Main class used for Java SE.
-
The main class uses an instance of
PaymentProcessorto process payments. The implementation ofPaymentProcessoris injected using CDI, and no specific implementation is provided in the code. In this project, thePaymentProcessoruses aPaymentService(an interface) which is injected using CDI. There are three implementations ofPaymentServicein this project:
To choose the implementation of PaymentService to use, you can use the CreditCard, PayPal, Rest, and Websocket qualifiers.
In this sample, a PayPal implementation is chosen declaratively in the PaymentProcessor class. To change the implementation, you can change the qualifier in the PaymentProcessor class. As there are two implementations annotated with PayPal, one of them (PayPalPaymentWebsocketServiceImpl) is annotated with @Alternative. By default, the PayPalPaymentRestServiceImpl is used. To use the PayPalPaymentWebsocketServiceImpl, you can edit the beans.xml file and add the alternatives tag with the PayPalPaymentWebsocketServiceImpl class name.
To add functionality to the payment processing without changing the implementation, this project includes interceptors and decorators.
An interceptor is a class that intercepts the invocation of a method. It can be used to add functionality to a method without modifying the method itself. In this project, the PaymentInterceptor class is used to log payment transactions.
A decorator is a class that adds functionality to a bean. It can be used to add functionality to a bean without modifying the bean itself. In this project, the PaymentDecorator class is used to add a discount to payment transactions.
Producers are used to create instances of beans or to provide values for injection points. They can be used to create beans that are not managed by the CDI container or to provide values that are not available at compile time. In this project, producers are used to create instances of the MaxAmount and MinAmount classes, which are used to set the maximum and minimum amounts for payment transactions.
To build the project and run all tests, use the following commands:
To build a shaded JAR, use:
./mvnw clean packageTo run the application, use the following command:
./mvnw package exec:java -Dexec.mainClass="fr.univtln.bruno.samples.cdi.Main"- The project includes a REST resource that can be used to process payments. The
PaymentResourceclass is a JAX-RS resource that can be used to process payments using thePaymentProcessorclass. ThePaymentProcessoris injected in thePaymentResourceclass to process payments.
The payara micro maven plugin build the War file and bundle it in a jar withg a payara micro distribution. The JakartaEE server and the application are in a single jar.
./mvnw -P payaramicro clean install payara-micro:bundle
java -jar target/sample-cdi-0.1.0-SNAPSHOT-microbundle.jarcurl -X POST http://localhost:8080/sample-cdi-0.1.0-SNAPSHOT/api/v1/paymentThe Dockerfile is provided to build the application in a first stage and add it to a payara micro base image.
docker build --tag brunoe/sample-cdi .
docker run -p 8080:8080 brunoe/sample-cdicurl -X POST http://localhost:8080/sample-cdi-0.1.0-SNAPSHOT/api/v1/payment