Scope equivalent to @Stateless in CDI

4

I'm migrating the EJB annotations of my application to CDI and I have some questions:

  • What is the scope equivalent to @Stateless of EJB in CDI?
  • Would @ResquestScoped do equivalent behavior?
  • I have tested using @Stateless injecting with CDI using @Inject , is this correct?

I'm migrating a service class that only receives the requests from my managedBean and passes the information to the persistence class and does not need to keep its scope alive.

    
asked by anonymous 11.09.2017 / 00:44

2 answers

1

Except with the use of some extension, there is nothing 100% equivalent in CDI.

However, RequestScoped can serve well in many cases, noting that EJBs also control transactions (they have created new annotations for transactions in CDI 1.1 or 1.2) and have a life cycle that reuses instances (from a pool) instead of create new ones. If those little differences do not get in the way, fine.

The use of Inject, at first (without seeing the code), seems correct.

This article link indicates features that exist in EJB but not in CDI, but does not include news created after CDI 1.0.

    
14.09.2017 / 17:30
1

What best fits to replacing @Stateless with CDI is @RequestScoped .

The concept of stateless is to consider each request as an independent transaction that is not related to any previous request .

Already the @RequestScoped it functions as a simple HTTP request, it is discarded at the end of each request, the ManagedBean will not keep its state between the requests / HTTP requests that the user makes.

So the best option is it itself .

Stop complementary, I often use the @ViewScoped because it keeps the state while the user stays on the page, being a middle term between @RequestScoped and @SessionScoped .

    
15.09.2017 / 14:48