Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LUT-29143 : Removal of obsolete API calls #487

Open
wants to merge 3 commits into
base: develop8.x
Choose a base branch
from

Conversation

jlalouf-eviden
Copy link
Contributor

No description provided.

@ryahiaoui
Copy link
Member

ryahiaoui commented Dec 18, 2024

Do not directly call the CDI container (CDI.current()) to retrieve a bean, except as a last resort. This approach has several disadvantages:

-Lower performance: Each call to CDI.current() performs a search within the container, which can negatively impact performance.
-Unpredictable bean lifecycle management: Managing the lifecycle of beans can become difficult to predict, leading to undesirable behaviors.
-Testing challenges: Classes that explicitly depend on the CDI container are harder to unit test, as they require the CDI container to be initialized for dependency injection. This complicates testing and makes it more cumbersome to implement.
-Reduced portability and flexibility: Making the application dependent on the CDI container limits its portability and flexibility, especially if you want to run it in an environment without CDI.

It is therefore recommended to avoid using the CDI container directly unless there is no viable alternative.
Example:

Avoid using directly:

IMyBean _myBean = CDI.current().select(IMyBean.class).get();

Instead, prefer injecting via CDI:

@Inject
private transient IMyBean _myBean;

Alternatively, to facilitate unit testing, you can use a constructor for injection:

private transient IMyBean _myBean;

@Inject
public MyService(IMyBean myBean) {
_myBean = myBean;
}

Don't forget to use the transient keyword for class members that do not require serialization, such as CDI beans with ApplicationScoped or similar scopes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants