3

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.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • 2
    Servlets themselves don't have any scopes, but I'm guessing you're referring to CDI scopes. CDI scopes, although commonly used in the context of web applications, are not tied to them. For example, the request scope is active during servlet requests, web service invocations, EJB remote invocation, just to name several. – rdcrng Aug 14 '13 at 18:52

3 Answers3

1

• 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.*/
hasan.alkhatib
  • 1,509
  • 3
  • 14
  • 28
0

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

Ravi
  • 131
  • 1
  • 8
0

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

Adi
  • 2,364
  • 1
  • 17
  • 23