Controllers in Spring MVC 3.x are singleton scoped. There’s only one instance of the Controller shared by all the requests. So it’s bad idea to have member variables in a Controller class.
If you want to inject a bean that’s in session scope to a Controller you have to do some extra work, because the Controller needs to know how to fetch the bean from a session scope. Therefore you need to inject a proxy of the bean into the Controller. The proxy will do the fetching-the-bean-from-session job for the Controller.
The bean in session scope needs to be configured with a proxyMode property of @Scope annotation. The proxyMode attribute can hold a value of a ScopedProxyMode enumeration. You can specify a ScopedProxyMode.INTERFACES or ScopedProxyMode.TARGET_CLASS based on the interface-based or class-based proxy to be generated. If you have a session scoped POJO, Home that needs to be injected to a Controller here’s how you configure the @Scope annotation.
@Component @Scope(value="session",proxyMode=ScopedProxyMode.TARGET_CLASS) public class Home{ } @Controller public class SampleController{ @Autowired private Home home; }