This project has been created and used from me to train/onboard mid-to-senior level engineers in the CDI with the usage of Quarkus framework.
It explores basic CDI stuff but goes also in more depth in some specific topics.
- What is Dependency Injection (DI) ?
- How we can create just a basic DI framework ?
- What is CDI Bean == Bean
- CDI Scopes and Contexts
Diagram 1:
[Thread] ---> [ProxyBean] ---> [Bean]
Diagram 2:
[Context] --- gets accessed from multiple ---> [Thread] (eg: is that scope active for that thread ?)
\
\
--- has a ---> [Scope] ---which manages multiple---> [BeanInstance] (eg: managing all bean instances of this scope)
- CDI qualifiers
- CDI Events
- CDI Producers
- Decorators and Interceptors
- Dynamic Beans
Notes on Dynamic Beans
-----------------------------
Manually implementing a Bean<T>
1. we need to define the Contextual
public interface Contextual<T> {
public T create(CreationalContext<T> creationalContext);
public void destroy(T instance, CreationalContext<T> creationalContext);
}
2. The Core Attributes (Bean Attributes)
- Types
- Qualifier
- Name
- Scope
- Stereotypes
- Alternative (a boolean indicating the bean is an alternative or not)
public interface BeanAttributes<T> {
public Set<Type> getTypes();
public Set<Annotation> getQualifiers();
public Class<? extends Annotation> getScope();
public String getName();
public Set<Class<? extends Annotation>> getStereotypes();
public boolean isAlternative();
}
3. The Bean, which combines the Contextual and the BeanAttributes defined on the above steps
public interface Bean<T> extends Contextual<T>, BeanAttributes<T> {
public Class<?> getBeanClass();
public Set<InjectionPoint> getInjectionPoints();
}
4. The PassivationCapable interface
5. Now, we can add the DynamicBean by using CDI Extensions or Configurator
This project uses Quarkus, the Supersonic Subatomic Java Framework. If you want to learn more about Quarkus, please visit its website: https://quarkus.io/ .
https://lordofthejars.github.io/quarkus-cheat-sheet/
You can run your application in dev mode that enables live coding using:
./mvnw compile quarkus:dev
NOTE: Quarkus now ships with a Dev UI, which is available in dev mode only at http://localhost:8080/q/dev/.
The application can be packaged using:
./mvnw package
It produces the quarkus-run.jar
file in the target/quarkus-app/
directory.
Be aware that it’s not an über-jar as the dependencies are copied into the target/quarkus-app/lib/
directory.
The application is now runnable using java -jar target/quarkus-app/quarkus-run.jar
.
If you want to build an über-jar, execute the following command:
./mvnw package -Dquarkus.package.type=uber-jar
The application, packaged as an über-jar, is now runnable using java -jar target/*-runner.jar
.
You can create a native executable using:
./mvnw package -Pnative
Or, if you don't have GraalVM installed, you can run the native executable build in a container using:
./mvnw package -Pnative -Dquarkus.native.container-build=true
You can then execute your native executable with: ./target/cdi-demo-1.0-SNAPSHOT-runner
If you want to learn more about building native executables, please consult https://quarkus.io/guides/maven-tooling.
- Logging GELF (guide): Log using the Graylog Extended Log Format and centralize your logs in ELK or EFK
- YAML Configuration (guide): Use YAML to configure your Quarkus application
- RESTEasy Classic (guide): REST endpoint framework implementing Jakarta REST and more
Configure your application with YAML
The Quarkus application configuration is located in src/main/resources/application.yml
.
Easily start your RESTful Web Services