Monday, June 11, 2012

Spring Session Scope Bean

Since HTTP stateless, in a web application, a typical way to keep an user states across multiple requests is through HttpSession. However, doing it this way makes your application has tight dependency on HttpSession and its application container. With Spring, carrying states through multiple request can be done through its session scoped bean. It is cleaner and more flexible. Spring session scoped bean is very easy setup, I will try to explain it in a couple steps.

Scenario: Whenever an user enters your site, he is asked to pick a theme to use when navigating your site.
Solution: Store user's selection using Spring Session scoped bean

1. Annotate your bean

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class UserSelection{
   private String selectedTheme;
   //and other session values

   public void setSelectedTheme(String theme){
        this.selectedTheme=selectedTheme;
   }

   public String getSelectedTheme(){
       return this.selectedTheme;
   }

}

2. Inject your session scoped bean

@Controller
@RequestMapping("/")
public class SomeController{
   private UserSelection userSelection;

   @Resource
   public void setUserSelection(UserSelectio userSelection){
         this.userSelection=userSelection;
   }

   @RequestMapping("saveThemeChoice")
   public void saveThemeChoice(@RequestParam String selectedTheme){
        userSelection.setSelectedTheme(selectedTheme);
   }

   
   public String doOperationBasedOnTheme(){
          String theme=userSelection.selectedTheme();
 
          //operation codes
   }

}
In above code snippet, Although 'SomeController' has scope of singleton(default scope of Spring bean), each session will have its own instance of UserSelection. Spring will make the judgement, and inject a new instance of UserSelection if the request is a new session.

Required jars

Besides spring library, aopalliance and cglib jar are also required
http://mvnrepository.com/artifact/cglib/cglib-nodep
http://mvnrepository.com/artifact/aopalliance/aopalliance

27 comments:

  1. Awesome..You have clearly explained ...Its very useful for me to know about new things..Keep on blogging..
    Java Training in Chennai

    ReplyDelete
  2. Thanks for this selfless act. really feel very happy about sharing your useful update with us.
    ROI Services in Chennai

    ReplyDelete
  3. the site contains a very great article. the information present in this site will be very useful for us. thank you for sharing the blog with us.
    ROI Services in Chennai

    ReplyDelete
  4. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    ReplyDelete
  5. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    ReplyDelete
  6. Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  7. Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  8. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.

    seo company in india

    ReplyDelete
  9. Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Great work admin.Keep update more blog.
    Germany Education Consultants in Chennai

    ReplyDelete
  10. Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

    artificial intelligence and machine learning course in chennai
    machine learning with python course in Chennai
    machine learning certification in chennai
    Android training in chennai
    PMP training in chennai

    ReplyDelete
  11. Such a Great Article!! I learned something new from your blog. Amazing stuff. I would like to follow your blog frequently. Keep Rocking!!
    Blue Prism training in chennai | Best Blue Prism Training Institute in Chennai

    ReplyDelete
  12. A bewildering web journal I visit this blog, it's unfathomably heavenly. Oddly, in this present blog's substance made purpose of actuality and reasonable. The substance of data is informative
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  13. A befuddling web diary I visit this blog, it's incredibly grand. Strangely, in this present blog's substance made motivation behind fact and sensible. The substance of information is instructive
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  14. A bewildering web journal I visit this blog, it's unfathomably heavenly. Oddly, in this present blog's substance made purpose of actuality and reasonable. The substance of data is informative
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  15. An overwhelming web journal I visit this blog, it's unfathomably amazing. Unusually, in this present blog's substance made inspiration driving truth and reasonable. The substance of data is enlightening
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  16. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.

    Workday Online Training

    ReplyDelete
  17. نحن بشركة تنظيف مفروشات نقدم مساحيق الغسيل و منظفات مطابقة للمواصفات العالمية كما أن المنظفات مصرح بها من وزارة الصحة وذات جودة تنظيف هائلة و تقوم الشركة بتقديم أفضل النتائج و يقوم بتقديم الخدمات أفضل الفنيين المدربين و أمهر الخبراء المتخصصين في مجال تنظيف المفروشات و يقومون بإستخدام أجهزة البخار لنظافة كاملة .

    شركة تنظيف مجالس بالاحساء
    شركة تنظيف كنب بالاحساء
    شركة تنظيف شقق بالاحساء
    شركة تنظيف سجاد بالاحساء

    ReplyDelete
  18. تستخدم شركة تنظيف شقق بالإحساء الماكينات الحديثة التي تناسب في عمليات التنظيف و نمتلك أمهر طاقم عمل مجهز بأحدث التقنيات و نقوم بتنظيف الشقق بأعلى مستوى و لدينا أكفأ المتخصصين حيث يلتزمون بالميعاد المتفق عليه لتسليم الشقق نظيفة تواصل معنا الآن ليصلك كل جديد و حصري .




    شركة تنظيف بالاحساء
    شركة تنظيف منازل بالاحساء
    شركة تنظيف فلل بالاحساء
    شركة تنظيف بيوت بالاحساء

    ReplyDelete
  19. يقوم عمال و فنيين شركة مكافحة النمل الأبيض بالإحساء بإكتشاف مكان تواجد النمل و إبادةه و ذلك لأنه يسببه خسارة فادحة للمنزل فهم متخصصون فى إبادة النمل برشه بالمبيدات الحشرية المناسبة للتخلص من النمل الأبيض نهائياً فشركتنا من أهم شركات مكافحة النمل و تقدم أسعار تنافسية لكل عملائها الكرام تواصل معنا الآن .


    شركة مكافحة الصراصير بالاحساء
    شركة مكافحة النمل الابيض بالاحساء
    شركة مكافحة حشرات بالاحساء
    شركة مكافحة بق الفراش بالاحساء
    شركة مكافحة الفئران بالاحساء

    شركة رش مبيدات بالإحساء
    شركة تركيب طارد حمام بالاحساء

    ReplyDelete
  20. تعلن شركة تنظيف خزانات بالإحساء عن توافر أرخص الأسعار بين شركات التنظيف و الافضل من حيث الجودة كما اننا لدينا طاقم عمل مجهز بالكامل على أعلى المستويات في تنظيف الخزانات و يعمل بإحترافية في تنفيذ و أداء كافة مهام تنظيف خزانات للحفاظ على الصحة للإستخدام الآمن للمياه النظيفة تواصل معنا الآن ليصلك كل جديد .

    شركة تنظيف خزانات بالاحساء
    شركة تسليك مجاري بالاحساء

    ReplyDelete