AndreaNobili
AndreaNobili

Reputation: 42957

How exactly works the Spring session scope of a bean? what is the default scope of a bean in the web context?

I am studying Spring MVC and I have the following doubts:

  1. What exactly is the purpose of the session scope?

Reading the documentation I know that this scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext. And also that a new instance is created once per user session.

But when exactly is it used? and for what purpose? Can you make a practical example?

  1. In Spring MVC what is the default scope in the web context?

I know that in Spring the default scope for a bean is singleton but what about the scope of a bean in the web context?

Upvotes: 27

Views: 65572

Answers (5)

Sabir Khan
Sabir Khan

Reputation: 10132

But when exactly is it used? and for what purpose? Can you make a practical example?

In one of my JSP based Spring MVC web apps, we use it to store data that doesn't vary after user's first request in the session i.e. we populate fields of this bean when user first comes to server & then we use ( aka read ) these values in subsequent requests ( next requests in session ) e.g. user name, user org group, address , logged in client number etc etc.

These constants values are kind of mandatory & be needed in all log messages or in all SQL queries.

Request routing is designed in such a way that bean population ( setting of bean properties ) happens only once per session.

Other interesting part as pointed in Yasir Shabbir Choudhary's answer that you can mimic same behavior using traditional way.

Is Spring session scoped bean saved in HttpSession?

Your second question is already answered by many that default scope - Singleton is applicable here too.

Upvotes: 2

Roberto Rodriguez
Roberto Rodriguez

Reputation: 3347

I had the same problem, I was using:

@Component
@Scope("session")

And this made the magic for me:

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

I hope it helps ;-)

Upvotes: 7

Yasir Shabbir Choudhary
Yasir Shabbir Choudhary

Reputation: 2578

Actually Spring help you create Session scope bean instead traditional way

httpSession.setAttribute("Object",new Object());
&&
httpSession.getAttribute("Object");

and Spring provide this efficient way

@Component
@Scope("session")
public class Foo{
}

now it's headache of spring to create and destroy this associated session object using Factory Pattern

Upvotes: 12

sachin k
sachin k

Reputation: 246

Ans 1) session scope is very similar to HttpSession scope. Beans instantiated based on session scope scope lives through the HTTP session. Similar to request scope, it is applicable only for web aware spring application contexts.

/** * Annotation-based configuration of session scope */ 
@Component
@Scope("session") 
public class ShopCart { }

and then

@Inject
private ShopCart cart;

Ans 2) Default is Singleton everywhere.

Upvotes: 17

Vojtech Ruzicka
Vojtech Ruzicka

Reputation: 17075

  1. You use spring session beans for beans which are stateful and their state differs per user. These can be for example preferences of currently logged in user.
  2. Default scope of bean in spring is singleton and it is no different in Web Application context.

Note than in web environment you can use also REQUEST scoped beans and their lifetime is only per one user request. You should use request scope when session is not necessary and request is sufficient.

Also, in portlet environment, you can use another scope which is GLOBAL SESSION. Each portlet has its own indepenednt session and typically those portlets are preffered to have their own state encapsulated only for themselves. But if you need to share session data among different portlets, you will need to use global session scope.

Upvotes: 16

Related Questions