Knowledge Walls
J2EE Technologies Tutorial
Hyderabad, Andhra Pradesh, India
Spring MVC session scoped bean Example in Examples of Spring 3.0 Examples
10092 Views
Hints 
session scope beans are retains the memory of the object in user level. For each user or session access creates a instances and maintains the values of the bean in the application context for each user differently.
Step.1 Start a Web based Spring application 
  1. Select New menu -> Dynamic Web Project
  2. Enter Project Name as "SessionScopeExample"
  3. Click Next, Selecting Target runtime as Apache Tomcat 7.0
  4. Click Next, Check Generate web.xml deployment descriptor then click on "Finish"
  5. Copy and paste Spring's 21 Framework Jars, aopalliance-1.0.jar, aspectjtools-1.6.6.jar and commons-logging-1.1.jar into /WEB-INF/lib
Step. 2 Project Explorer Preview 
RndHolder.java
package com.knowledgewalls;
 
public class RndHolder {
    private int rndNumber;
     
    public int getRndNumber(){
        return rndNumber;
    }
    public void setRndNumber(int rndNumber){
        this.rndNumber = rndNumber;
    }
     
    public void generateRndNumber(){
        this.rndNumber = (int) (Math.random() * 99999);
    }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://java.sun.com/xml/ns/javaee"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
              id="WebApp_ID"
              version="3.0">

  <display-name>SessionScopeExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
 
  <servlet>
    <servlet-name>beans</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>beans</servlet-name>
    <url-pattern>/conn/*</url-pattern>
  </servlet-mapping>
</web-app>
beans-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="rndHolder" class="com.knowledgewalls.RndHolder" scope="session">
        <aop:scoped-proxy/>
    </bean>
    <context:component-scan base-package="com.knowledgewalls"></context:component-scan>

</beans>
ShowAccountDetails.java
package com.knowledgewalls;
 
 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller("/ShowAccountDetails")
public class ShowAccountDetails extends HttpServlet {
 
    @Autowired
    RndHolder rndHolder;
     
    @RequestMapping
    public @ResponseBody void getMethod(HttpServletRequest request,HttpServletResponse response) throws Exception{
        response.getWriter().write("Old No. "+rndHolder.getRndNumber()+"\n");
        rndHolder.generateRndNumber();
        response.getWriter().write("New No. "+rndHolder.getRndNumber()+"\n");
        response.getWriter().write("Hash code Ref. "+rndHolder);
    }
}
Hints 
Running URL: http://localhost:8089/SessionScopeExample/conn/ShowAccountDetails
Hints: Session level bean maintains old random numbers too in the session level.

Output:
Best Lessons of "Spring 3.0 Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details