gstackoverflow
gstackoverflow

Reputation: 37034

What scopes exist in spring mvc?

I know in servlets many scopes(request, session....).

  1. How it correlates with Spring MVC?
  2. How can I use needful scope using Spring style?

I don't want directly use HttpRequest and HttpResponse.

Upvotes: 3

Views: 4377

Answers (3)

Adi
Adi

Reputation: 2394

Spring MVC offers more scopes like request, session, global session.

You can annotate spring beans to use correct scope. Please follow below link to find more.

http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes

Upvotes: 0

Ravi
Ravi

Reputation: 131

You can configure the scope of a bean you wire up in your configuration

Example configuration:

<bean id="bean-id-name" class="abc.xyz.classname" scope="singleton">

Reference on spring bean scopes: http://static.springsource.org/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/beans.html#beans-factory-scopes

Upvotes: 0

hasan.alkhatib
hasan.alkhatib

Reputation: 1559

• Singleton: This scopes the bean definition to a single instance per Spring IoC container (default).

• Prototype: This scopes a single bean definition to have any number of object instances.

Message aMessage; //object

// singleton bean scope 
aMessageA = new Message();
aMessageA = (Message) applicationContext.getBean("message");
aMessageA.setText("message1 :D");
System.out.println(aMessageA.getText());

aMessageB = new Message();
aMessageB = (Message) applicationContext.getBean("message");
System.out.println(aMessageB.getText());
// result will be the same for both objects because it's just one instance.


// Prototype bean scope. scope="prototype"
aMessageA = new Message();
aMessageA = (Message) applicationContext.getBean("message");
aMessageA.setText("message :D");
System.out.println(aMessageA.getText());

aMessageB = new Message();
aMessageB = (Message) applicationContext.getBean("message");
System.out.println(aMessageB.getText());

/* first object will print the message, but the second one won't because it's a new instance.*/

Upvotes: 1

Related Questions