CDI Bean Life Cycle Management

HeartfeltVision avatar
HeartfeltVision
·
·
Download

Start Quiz

Study Flashcards

10 Questions

What is the primary purpose of CDI in Java EE?

To enable dependency injection and other features

What is the name of the file that CDI needs to identify beans in the class path?

beans.xml

What is a major feature added to CDI 2.0 to support Java SE?

API to boot CDI in Java SE

What is the primary characteristic of a CDI Bean?

It can be any kind of class that contains business logic

What is the purpose of the @PostConstruct annotation in a CDI Bean?

To manage the lifecycle of the bean

What is the term for the process of CDI identifying beans in the class path?

Bean discovery

What is the primary benefit of using CDI in Java EE applications?

Simplified development

What is the term for the annotations that can be used to configure or veto observer methods in CDI 2.0?

Configurators

What is the purpose of the @Inject annotation in a CDI Bean?

To inject references to other beans

What is the primary benefit of using asynchronous events in CDI 2.0?

Decoupling between components

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.

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.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Licenciement CDI partie 1
31 questions
Licenciement CDI partie 2
33 questions
CDI de chantier ou d’opération
36 questions
Use Quizgecko on...
Browser
Browser