1
votes

How can I inject this class (It's inside the war part of the project):

@Named("user")
@SessionScoped
public class User implements Serializable
{....}

Into this EJB session bean:

@Stateless
@LocalBean
public class testSB 
{    inject User here... }

I tried using @Inject but it does not work. It seems that it does not recognize User class. I tried looking for an example of injecting CDI into EJB, but could not find anything that worked.

Is that even possible? What is the right way to do this? I would like to get data from the User class directly instead of passing the values inside the methods.

3

3 Answers

1
votes

Inject a Provider<User> into your stateless bean. Then, when you need the user, do userProdiver.get().

0
votes

Pay attention to not instantiate a "testSB" object (in this way you can't expect the container to inject an object of class "User" to you). To the container be able to inject one object of class "User" to you, you MUST let the container instantiate the object of class "testSB".

I don't think that there is a problem in injecting a "SessionBean" inside a "RequestBean". All your "RequestBean" objects will share the same object of the "SessionBean" class.

0
votes

With CDI you should be able to inject anything anywhere, even in EJB SessionScoped.

Try adding an empty beans.xml to your META-INF. Please check my answer here:

Java EE 7 - @Decorator, @Stateless and @PersistenceContext = nullpointerException

Best Regards, Alexander