This question is beyond doubt, in fact, this is advice.
Did you know that an EJB session redirected all dependencies before each call to a business method? Well, I didn’t know until a few minutes ago, and that gave me a big headache.
I thought dependency injection only happens when an instance is created, but that is not true. The EJB 3.1 specification says:
"If dependency injection is used in a bean session, the container enters these links after creating the bean instance and before any business methods are called in the bean instance." Section 4.3.2
Somehow, this definition can provide a non-bean session with the ability to have an interactive state, which is exactly what I need. For example, if you enter an @SessionScoped bean in the SLSB, no matter which pool instance processes your request, the SessionScoped bean will always match the session of the current client. In other words, it is not possible for an SLSB instance to have a SessionScoped bean of another client.
This allows the SLSB to get the registered user as an instance field via @Inject without any problems if two users use the same SLSB instance in turn. For example:
@Stateless public class StatelessSessionBean{ @Inject @LoggedInUser protected User loggedInUser;
Then the result of various calls to the test method is the following:
User 1 invoke 17:02:20,800 INFO [stdout] (http--0.0.0.0-8080-5) ejb: AccessControlBean@406189 17:02:20,801 INFO [stdout] (http--0.0.0.0-8080-5) user:User 1 User 2 invoke 17:02:56,227 INFO [stdout] (http--0.0.0.0-8080-8) ejb: AccessControlBean@406189 17:02:56,228 INFO [stdout] (http--0.0.0.0-8080-8) user:User 2 User 2 invoke 17:03:24,376 INFO [stdout] (http--0.0.0.0-8080-8) ejb: AccessControlBean@406189 17:03:24,378 INFO [stdout] (http--0.0.0.0-8080-8) user:User 2 User 1 invoke 17:03:24,517 INFO [stdout] (http--0.0.0.0-8080-6) ejb: AccessControlBean@1c05227 17:03:24,518 INFO [stdout] (http--0.0.0.0-8080-6) user:User 1 User 1 invoke 17:04:24,045 INFO [stdout] (http--0.0.0.0-8080-1) ejb: AccessControlBean@406189 17:04:24,047 INFO [stdout] (http--0.0.0.0-8080-1) user:User 1 User 2 invoke 17:04:24,179 INFO [stdout] (http--0.0.0.0-8080-8) ejb: AccessControlBean@1c05227 17:04:24,179 INFO [stdout] (http--0.0.0.0-8080-8) user:User 2
Note that even calling the same instance, the "loggedInUser" field changes correctly depending on the user who called the method.