13

In My app I am using a User bean that is Autowired to my service MyService and its used as the principle logged in user's info (So the user is not coming as a static bean from an xml but dynamically generated from a logged in user) If there are ten users logged in I will have ten candidates for the @AutoWired User field. (right?) and I can get any one of them cos AutoWired looks in the Spring container and not session.

tell me if I am wrong on this. and how to actually solve it if possible. But what if my AutoWired field is annotated with @Scope ("Session")

Like this :

@Component("user")
@Scope("session")
public class User 
{
String userid;
String name;
//getter setters etc
}

@Component
public class MyService
{
    @Autowired
    private User user;

}

Is it possible to get some other user's User bean when I call my MyService Component. Cos MyService is only @Component even though the User is @Scope(session).

Basically, (If I am wrong in my assumption) I think that when you @Autowire a field, its looks into the container as whole and the container is not divided into subcontainers per session.

skaffman
  • 398,947
  • 96
  • 818
  • 769
sarmahdi
  • 1,098
  • 4
  • 21
  • 61

2 Answers2

17

When you annotate User with @Scope("session") and then @Autowire that into another non-scoped component, Spring will generate a proxy which sits between MyService and User. This proxy will locate the User from the current session, and will delegate any calls from MyService to the proxy to the session-scoped User.

So it's perfectly safe, the MyService component will only have access to the User from the current session.

If the proxying didn't happen, then the container would fail to start, since you can't directly inject a session-scoped bean into singleton-scoped bean.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • So its not possible that when I do a ` SpringUtils.getContext(req).getBean("myservice")`, I will get some other user bean that is not in the current session. – sarmahdi Jan 15 '12 at 12:26
  • 1
    @sarmahdi: That's correct. The current user's session is a thread-local variable maintained by Spring. – skaffman Jan 15 '12 at 12:28
  • So its not possible that when I do a ` SpringUtils.getContext(req).getBean("myservice")`, I will get some other user bean that is not in the current session. Actually I got this issue that I loaded a page and I got some other users details. So i was thinking that it might be cos even though the User is sessions scoped the service is not and non scoped are by default singelton. – sarmahdi Jan 15 '12 at 12:36
  • @sarmahdi: No, this would not have been the cause - something else is going on. – skaffman Jan 15 '12 at 12:37
  • thanks. So when you actually Do a @Scope(Session) it gets bind with a session which is a thread local variable... – sarmahdi Jan 15 '12 at 12:37
  • Thanks... Its an Ahan! moment for me.. :) appreciate it – sarmahdi Jan 15 '12 at 12:38
  • Oh I have a complex scenario for this one again (Do I need to ask it as another question) If MyService which is just a atComponent calls another MySecondService which is also only atComponent and MySecondService has an atAutoWired User user; Will that also get the session scoped User for that user's thread local. – sarmahdi Jan 15 '12 at 12:43
  • never mind, component is a component doesnt matter what stage of the execution it is. – sarmahdi Jan 15 '12 at 12:44
  • If I annotate MyService with atScope("Singleton"), Will that be a problem in this case. I am asking cos when you dont annotate its by default singleton and some one told me that you need to make it atScope("prototype"). I kinda disagree with him and thats why I am asking you cos I think the problem should be else where it should not have any scope issues as long as my User is bind to session scope. – sarmahdi Jan 15 '12 at 16:02
15

Spring would not automatically create a proxy. You need to change your scope annotation to the following

@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)

This would ensure that when the session bean is getting autowired in a singleton class, a proxy is created around the bean which would be responsible to get the bean from the session.

prashant
  • 1,382
  • 1
  • 13
  • 19