Common annotations and interfaces commonly used in the CDI API framework
CDI (Contexts and Dependency Injection) API is part of Java Ee (now renamed Jakarta EE), which provides a standardized mechanism for relying in injection in Java applications.The CDI API simplifies the process of dependent relationships between developers managing objects in applications through a set of annotations and interfaces.The following is the annotation and interface commonly used in the CDI API:
1. `@inject` Note: It is used to inject the dependent object into the target class.By using the `@inject` annotation of target attributes, constructors, or methods, the CDI container will automatically resolve and inject the corresponding objects.
@Inject
private ExampleService exampleService;
2. `@Named` Note: Used to specify a recognizable name for dependent objects.In this way, you can use the name to specify the specific object to be injected.
@Named("exampleDao")
public class ExampleDaoImpl implements ExampleDao {
...
}
3. `@Qualifier` Note: Used to combine `@inject` to provide more accurate injection methods.Developers can customize the Note of the Qualifier to distinguish different injection objects through this annotation.
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
public @interface ExampleQualifier {
...
}
@Inject
@ExampleQualifier
private ExampleService exampleService;
4. `@Singleton` Note: Used to make a class statement as a single example mode, only one instance is created throughout the application.Class marked by the annotation of the annotation of the CDI container will be automatically instantiated and managed when the CDI container starts.
@Singleton
public class ExampleSingleton {
...
}
5. `@RequestScoped` Note: Used to create an instance during each request processing process.This can ensure that the object is brand new at each request.
@RequestScoped
public class ExampleRequestScoped {
...
}
In addition to the above annotations, the CDI API also provides some commonly used interfaces to support the function of relying on injection, context management and event triggering.The following are some of the common interfaces:
1. `javax.nterprise.inject.spi.bean` interface: indicate an instance or constructor managed by CDI managed.Developers can obtain metadata information about instances through this interface and operation of instances.
public interface Bean<T> {
BeanAttributes<T> getBeanAttributes();
T create(CreationalContext<T> creationalContext);
void destroy(T instance, CreationalContext<T> creationalContext);
...
}
2. `javax.nterprise.inject.spi.injectPoint` interface: indicate a dependent injection point to describe the target information that will be injected.Through this interface, developers can obtain detailed information about the target attributes, constructing functions and methods.
public interface InjectionPoint {
Annotated getAnnotated();
Bean<?> getBean();
...
}
3. `javax.nterprise.context.spi.Context` interface: It means a context that is used to manage a life cycle of a certain type of instance.Different context types, such as the request scope, sessional scope, etc., can be configured and interacting in CDI containers and applications.
public interface Context {
<T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext);
<T> T get(Contextual<T> contextual);
...
}
By using the above annotations and interfaces, developers can easily implement functions such as dependency injection, context management, and event trigger to improve the maintenance and flexibility of the application.