Does CDI make sense if there is no web tier and therefore no HTTP session?

The new JSR 299, Contexts and Dependency Injection for Java EE, seems to be based on the concept of Scope.

beans are created and associated with one of the supported areas: applications, sessions (associated with an HTTP session), conversations, and requests.

Does it make sense to use CDI if there is no HTTP session (for example, an Enterprise application that provides functions through a remote EJB interface), since managed beans will not be associated with any Context (since they do not exist)?

Is it possible to use CDI in such a scenario? What benefits would he bring?

+6
source share
3 answers

This reminds me of my own question I asked a while ago: How does @SessionScoped work with EJB? Is CDI only for the web tier?

It seems that the idea of โ€‹โ€‹"scope" only matters in the case of an HTTP session.
However, I can see legitimate use of the @ApplicationScoped as a way to implement a singleton bean application, even though the request is HTTP code.

Javadoc says:

The application area is active:

(...)

  • during any call to the Java EE web service,

  • during any remote method call of any EJB, during any asynchronous method call of any EJB, during any EJB call, timeout, and when messages are delivered to any EJB managed bean,

+3
source

You can also create your own areas. CDI is very extensible and can be used in a variety of situations. It is also used in SE applications where there is neither HttpSession nor HttpRequest.

+3
source

In addition, CDI is not only designed for lifecycle management, you can use it to inject dependencies, thereby very easily separating interfaces from their implementations. You can also execute some AOP methods using hooks and decorators or create a very loosely coupled Observer pattern, taking advantage of CDI events.

0
source

Source: https://habr.com/ru/post/906923/


All Articles