Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does CDI make sense if there is no Web layer and hence no HTTP session?

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

The beans are created and associated to one of the supported Scopes: Application, Session (mapped to a HTTP session), Conversation, and Request.

Does it make sense to use CDI if there is no HTTP Session (for example, an Enterprise application that exposes functionality through EJBs remoting) since the Managed Beans are not going to be associated to any Context (since they do not exist)?

Is it even possible to use CDI in such an scenario? Which advantages would it bring to it?

like image 727
Mr.Eddart Avatar asked Jan 26 '12 10:01

Mr.Eddart


2 Answers

It reminds me of my own question I asked some time ago: How does @SessionScoped work with EJB? Is CDI only for web-tier?

It seems that the idea of 'scope' is relevant only in the case of HTTP Session.
However, I can see a valid use of the @ApplicationScoped scope as a way to implement an application-singleton bean, despite if the request is a HTTP one.

Javadoc says:

The application scope is active:

(...)

  • during any Java EE web service invocation,

  • during any remote method invocation of any EJB, during any asynchronous method invocation of any EJB, during any call to an EJB timeout method and during message delivery to any EJB message-driven bean,

like image 128
Piotr Nowicki Avatar answered Oct 15 '22 03:10

Piotr Nowicki


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

like image 23
LightGuard Avatar answered Oct 15 '22 03:10

LightGuard