miercuri, 15 iulie 2015

JSF/CDI Bean Injection Short Overview

From the following figure, you can seek some guidance for dealing with some of the most popular cases:


As a general rule in JSF, don't use objects that have shorter lifespan than the objects you are calling it from. In other words, use objects whose lifespan is the same as, or longer than, the object being injected into. Breaking this rule will end up in a JSF exception.

Common mistakes (bad practices):

- Use request objects in session objects
- Use session objects in application objects

Nevertheless, for CDI, these cases are not such a big issue. When you are using an object that has a shorter lifespan than the object you are calling it from (for example, injecting a request scoped bean into a session scoped bean), CDI classifies the use case as a mismatched injection and fixes the issue via CDI proxies. For each request, the CDI proxy re-establishes the connection to a live instance of the request scoped bean.

Keep in mind also:

- Overusing a view scoped bean for request scoped data may affect memory.
- Overusing a request scoped bean for view scoped data may cause forms with unexpected behavior.
- Overusing an application scoped bean for request/view/session scoped data may cause an undesirably wide visibility of data across users and will affect memory.
- Overusing a session scoped bean for request/view data may cause an undesirably wide visibility of data across multiple browser windows/tabs in that session.

Starting with JSF 2.0, managed beans can be injected (dependency injection) into the property of another managed bean using the @ManagedProperty annotation. Another way to inject beans is to use the @Inject annotation, which is part of the CDI powerful injection mechanism.

Read more:
JSF 2.2, CDI and OmniFaces Scopes List

Niciun comentariu:

Trimiteți un comentariu