1. What does a session scoped bean mean in Spring?
Why: A session scoped bean has one instance per HTTP session. Each user session gets its own instance.
2. How long does a session scoped bean live?
Why: The bean lives within the HTTP session. When the HTTP session is invalidated or times out, Spring destroys the bean instance (and invokes destruction callbacks, such as @PreDestroy), removing it from the session attributes
3. What happens when a new HTTP session starts?
Why: Each new HTTP session gets its own new instance of the session scoped bean.
4. Two different users are using the application with two different HTTP sessions. How do they work with a session scoped bean?
Why: Because the bean is tied to the HTTP session, each session has its own instance. Data of one user is not seen by another user.
5. A user sends several requests using the same HTTP session. What happens to the data stored in a session scoped bean?
Why: The same bean instance is used for all requests of the same session, so the data stays available between requests.
6. Which of these is a good use of a session scoped bean?
Why: A session scoped bean is best for data that belongs to one user session and must be remembered across multiple requests.
7. Why is a scoped proxy used when a session scoped bean is injected into a singleton bean, such as a controller?
Why: A singleton is created once, while session beans change per session. The injected proxy looks up the bean of the current session each time a method is called.
8. What does the injected scoped proxy do when a method is called on it?
Why: The proxy is a stand-in object. On each call, it gets the real bean for the current session and delegates the call to it.
9. Which @Scope attribute is used to enable a scoped proxy for a bean?
Why: The proxyMode attribute of @Scope tells Spring if and how a scoped proxy should be created.
10. Which proxyMode value tells Spring to create a class based proxy?
Why:ScopedProxyMode.TARGET_CLASS creates a proxy by subclassing the bean class. INTERFACES creates an interface based proxy instead.
11. Which constant of WebApplicationContext represents the name of the session scope?
Why:WebApplicationContext.SCOPE_SESSION is the constant for the session scope name.
12. What is the effect of the following bean configuration?
@Component
@Scope(value = WebApplicationContext.SCOPE_SESSION,
proxyMode = ScopedProxyMode.TARGET_CLASS)
public class UserCart {
}
Why: The scope is session, so there is one instance per HTTP session. TARGET_CLASS means the injected proxy is class based.
13. When a class based proxy is used, what type can be used at the injection point?
Why: A class based proxy extends the bean class, so the bean can be injected using its own class type. No interface is needed.
14. Besides a scoped proxy, which approach can be used to get a session scoped bean inside a singleton bean?
Why: Injecting a Provider and calling its get() method returns the bean of the current session. This is an alternative to using a scoped proxy.