Podcast
Questions and Answers
What is the primary purpose of CDI in Java EE?
What is the primary purpose of CDI in Java EE?
What is the name of the file that CDI needs to identify beans in the class path?
What is the name of the file that CDI needs to identify beans in the class path?
What is a major feature added to CDI 2.0 to support Java SE?
What is a major feature added to CDI 2.0 to support Java SE?
What is the primary characteristic of a CDI Bean?
What is the primary characteristic of a CDI Bean?
Signup and view all the answers
What is the purpose of the @PostConstruct annotation in a CDI Bean?
What is the purpose of the @PostConstruct annotation in a CDI Bean?
Signup and view all the answers
What is the term for the process of CDI identifying beans in the class path?
What is the term for the process of CDI identifying beans in the class path?
Signup and view all the answers
What is the primary benefit of using CDI in Java EE applications?
What is the primary benefit of using CDI in Java EE applications?
Signup and view all the answers
What is the term for the annotations that can be used to configure or veto observer methods in CDI 2.0?
What is the term for the annotations that can be used to configure or veto observer methods in CDI 2.0?
Signup and view all the answers
What is the purpose of the @Inject annotation in a CDI Bean?
What is the purpose of the @Inject annotation in a CDI Bean?
Signup and view all the answers
What is the primary benefit of using asynchronous events in CDI 2.0?
What is the primary benefit of using asynchronous events in CDI 2.0?
Signup and view all the answers
Study Notes
CDI Beans Life Cycle Management
- In CDI, the container manages the life cycle of beans, resolving dependencies and invoking methods annotated with
@PostConstruct
before the first business method invocation. - The
@PreDestroy
callback notification signals that the instance is being removed by the container.
Scopes and Context
- CDI beans may be stateful and contextual, living in a well-defined scope (e.g., request, session, application, and conversation scopes).
Interception
- Interceptors are used to interpose on business method invocations, similar to aspect-oriented programming (AOP).
- Interceptors can be chained and are called before and/or after method execution.
- They separate technical concerns from business logic.
Loose Coupling and Strong Typing
- Interceptors separate technical concerns from business logic.
- CDI uses strongly typed annotations to wire beans together.
Deployment Descriptor
- The deployment descriptor is called
beans.xml
and is mandatory. - It can be used to configure functionalities (interceptors, decorators, alternatives, etc.).
Understanding Beans
- Beans are CDI objects built on the Managed Bean model.
- They have an improved life cycle for stateful objects, are bound to well-defined contexts, and bring a typesafe approach to dependency injection, interception, and decoration.
Dependency Injection
- Dependency Injection (DI) is a design pattern that decouples dependent components.
- It is part of inversion of control, where the container injects dependent objects.
Life Cycle of a POJO
- The life cycle of a POJO is simple: create an instance using the
new
keyword and wait for the Garbage Collector to free memory.
CDI Bean Life Cycle
- To run a CDI Bean inside a container, you can't use the
new
keyword; instead, you need to inject the bean and the container manages its life cycle. - The container creates the instance, gets rid of it, and gives a handle after constructing and before destroying it.
CDI Specifications Overview
- The aim of CDI was to fill gaps in the platform, knit together the web tier and transactional tier, make dependency injection first-class, and add extensions easily.
- CDI 2.0 added support for Java SE, API to boot CDI in Java SE, observers (events) ordering, asynchronous events, configurators, possibility to configure or veto observer methods, built-in annotation literals, and alignment with Java 8 features.
Writing a CDI Bean
- A CDI Bean can be any class containing business logic, called directly via injection or invoked via EL from a JSF page.
- A bean is a POJO that doesn't inherit or extend from anything, can inject references to other beans, has its life cycle managed by the container, and can get its method invocation intercepted.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn about the life cycle of CDI beans, including the post construct and pre destroy annotations, and how they relate to scopes and context in application software development.