Home  >  Article  >  Java  >  How are Java EE's CDI annotations used for dependency injection?

How are Java EE's CDI annotations used for dependency injection?

王林
王林Original
2024-05-06 11:36:02957browse

Yes, the CDJ annotations used for dependency injection in Java EE include: @Inject: Inject dependencies. @Produces: Declares methods to produce dependencies. @Disposes: Declares a method to be called when a dependency is released. @Dependent: Specify the scope of the bean as the request scope. @ApplicationScoped: Specifies that the scope of the bean is application scope.

Java EE的CDI注解如何用于依赖注入?

CDJ annotations in Java EE are used for dependency injection

In Java Enterprise Edition (Java EE), dependency injection (DI ) is a technology that simplifies application development. With DI, instead of manually creating and managing dependencies, you can declare dependencies through annotations. These annotations will be automatically parsed and injected by the container (such as the GlassFish server).

CDI Annotations

In Java EE, the Context and Dependency Injection (CDI) specification provides a set of annotations for DI. These annotations include:

  • @Inject: used to inject a dependency.
  • @Produces: Used to declare that a method generates a dependency.
  • @Disposes: Used to declare a method to be called when the dependency is no longer needed.
  • @Dependent: Used to specify that the scope of a bean is the request scope.
  • @ApplicationScoped: Used to specify that the scope of a bean is the application scope.

Practical case

Suppose we have a UserService class, which depends on the UserRepository interface. Using CDI, we can declare dependencies in the following way:

import javax.inject.Inject;

public class UserService {

    @Inject
    private UserRepository userRepository;

    // ...
}

In the above code, the @Inject annotation indicates that the userRepository field should be automatically injected by the container.

import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class UserRepositoryProducer {

    @PersistenceContext
    private EntityManager em;

    @Produces
    private UserRepository createUserRepository() {
        return new UserJpaRepository(em);
    }
}

In this example, the @Produces annotation is used to declare that the createUserRepository method is responsible for producing the implementation of UserRepository, while the @PersistenceContext Annotations are used to inject EntityManager into methods.

Conclusion

CDI annotations provide a simple and efficient way to implement dependency injection. By using these annotations, you can reduce boilerplate code and make your application more modular and maintainable.

The above is the detailed content of How are Java EE's CDI annotations used for dependency injection?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn