0

If an EJB is injected by CDI container using @Inject, because that CDI has scope, that means this EJB will have scope and it's lifecycle will be controlled by CDI container instead of EJB container?

Or the CDI container only injects and the lifecycle is controlled by EJB container?

For example: I implement a SLSB which has it's lifecycle controled by EJB container. It means that the EJB container will control the creation, pooling and destruction of the bean.

By default CDI bean has Dependent scope. If I inject this SLSB using @Inject CDI will control the bean's lifecycle so that it will be created and destroyed every time the parent class is created and destroyed? If that is true the EJB lost the concurrence characteristics (pool of bean).

Marcelo Keiti
  • 1,200
  • 9
  • 10
  • You might find [this](https://github.com/MartinanderssonDotcom/java-ee-concepts/blob/master/src/test/java/com/martinandersson/javaee/ejb/sessionbeans/testdriver/TestDriver.java#L238) interesting. – Martin Andersson Mar 27 '15 at 12:46
  • Thanks. I read the CDI specification mentioned in your link and found what I was looking for: "A stateless session bean must belong to the `@Dependent` pseudo-scope. A singleton session bean must belong to either the `@ApplicationScoped` scope or to the `@Dependent` pseudo-scope. If a session bean specifies an illegal scope, the container automatically detects the problem and treats it as a definition error. A stateful session bean may have any scope." – Marcelo Keiti Mar 27 '15 at 13:05
  • See if you can answer [this question](http://stackoverflow.com/q/28926854) =). – Martin Andersson Mar 27 '15 at 13:08
  • Sorry, I don't understand why there are 2 scopes allowed for `@Singleton` too =( – Marcelo Keiti Mar 27 '15 at 13:46

1 Answers1

1

From bkail's answer on a similar question:

The @Inject version will respect the scope of the EJB. For example, using @EJB to inject an SFSB into a servlet makes no sense because only one SFSB will exist for every request. Using @Inject to inject a @SessionScoped SFSB into a servlet means you have a CDI proxy that creates a new SFSB as needed for each session.

Community
  • 1
  • 1
Ryan
  • 2,058
  • 1
  • 15
  • 29