{"id":20357,"date":"2024-03-15T20:20:03","date_gmt":"2024-03-15T20:20:03","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/"},"modified":"2024-03-21T17:53:37","modified_gmt":"2024-03-21T17:53:37","slug":"what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/","title":{"rendered":"What is the usage of the @SessionScope annotation and Session in Spring Boot?"},"content":{"rendered":"<p>In Spring Boot, the @SessionScope annotation is used to specify that a bean has a session-level scope. This means that each user session will have its own instance.<\/p>\n<p>When using the @SessionScope annotation, Spring will create a new instance of a Bean for each user session and store it in the user&#8217;s session. This allows different requests from the same user to share the same Bean instance.<\/p>\n<p>When using the @SessionScope annotation, it is important to keep in mind the following points:<\/p>\n<ol>\n<li>You need to add the @SessionScope annotation to the configuration class or bean class.<\/li>\n<li>It is necessary to ensure that the HttpSession object is injected into the Bean in order to access and manipulate the data in the session.<\/li>\n<li>To enable Spring Session support, add the @EnableRedisHttpSession annotation to the configuration class.<\/li>\n<\/ol>\n<p>The main purpose of using Session is to share data between user sessions. It can be used to store and retrieve user login information, shopping cart contents, user preferences, etc.<\/p>\n<p>Here is an example using the @SessionScope annotation and Session:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta\">@Component<\/span>\r\n<span class=\"hljs-meta\">@SessionScope<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">ShoppingCart<\/span> {\r\n    <span class=\"hljs-keyword\">private<\/span> List&lt;Product&gt; products = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">ArrayList<\/span>&lt;&gt;();\r\n\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">addProduct<\/span><span class=\"hljs-params\">(Product product)<\/span> {\r\n        products.add(product);\r\n    }\r\n\r\n    <span class=\"hljs-keyword\">public<\/span> List&lt;Product&gt; <span class=\"hljs-title function_\">getProducts<\/span><span class=\"hljs-params\">()<\/span> {\r\n        <span class=\"hljs-keyword\">return<\/span> products;\r\n    }\r\n\r\n    <span class=\"hljs-comment\">\/\/ Other methods...<\/span>\r\n}\r\n<\/code><\/pre>\n<p>In the example above, the ShoppingCart class is declared as @SessionScope, which means that each user session will have a unique instance. Products can be added to the shopping cart and the list of products in the cart can be retrieved using the getProducts method.<\/p>\n<p>In the controller, you can access and update data in the Session by injecting the HttpSession object.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta\">@Controller<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">ShoppingCartController<\/span> {\r\n    <span class=\"hljs-meta\">@Autowired<\/span>\r\n    <span class=\"hljs-keyword\">private<\/span> HttpSession session;\r\n\r\n    <span class=\"hljs-meta\">@Autowired<\/span>\r\n    <span class=\"hljs-keyword\">private<\/span> ShoppingCart shoppingCart;\r\n\r\n    <span class=\"hljs-meta\">@PostMapping(\"\/addProduct\")<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> String <span class=\"hljs-title function_\">addProduct<\/span><span class=\"hljs-params\">(<span class=\"hljs-meta\">@RequestParam(\"productId\")<\/span> <span class=\"hljs-type\">int<\/span> productId)<\/span> {\r\n        <span class=\"hljs-comment\">\/\/ \u6839\u636eproductId\u83b7\u53d6Product\u5bf9\u8c61<\/span>\r\n        <span class=\"hljs-type\">Product<\/span> <span class=\"hljs-variable\">product<\/span> <span class=\"hljs-operator\">=<\/span> productService.getProductById(productId);\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u5c06\u4ea7\u54c1\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u4e2d<\/span>\r\n        shoppingCart.addProduct(product);\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u5b58\u50a8\u8d2d\u7269\u8f66\u5bf9\u8c61\u5230Session\u4e2d<\/span>\r\n        session.setAttribute(<span class=\"hljs-string\">\"shoppingCart\"<\/span>, shoppingCart);\r\n\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"redirect:\/shoppingCart\"<\/span>;\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@GetMapping(\"\/shoppingCart\")<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> String <span class=\"hljs-title function_\">viewShoppingCart<\/span><span class=\"hljs-params\">(Model model)<\/span> {\r\n        <span class=\"hljs-comment\">\/\/ \u4eceSession\u4e2d\u83b7\u53d6\u8d2d\u7269\u8f66\u5bf9\u8c61<\/span>\r\n        <span class=\"hljs-type\">ShoppingCart<\/span> <span class=\"hljs-variable\">shoppingCart<\/span> <span class=\"hljs-operator\">=<\/span> (ShoppingCart) session.getAttribute(<span class=\"hljs-string\">\"shoppingCart\"<\/span>);\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u5c06\u8d2d\u7269\u8f66\u5bf9\u8c61\u6dfb\u52a0\u5230\u6a21\u578b\u4e2d<\/span>\r\n        model.addAttribute(<span class=\"hljs-string\">\"shoppingCart\"<\/span>, shoppingCart);\r\n\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"shoppingCart\"<\/span>;\r\n    }\r\n\r\n    <span class=\"hljs-comment\">\/\/ Other methods...<\/span>\r\n}\r\n<\/code><\/pre>\n<p>In the example above, by injecting the HttpSession object into the controller, you can access and set data in the session. In the addProduct method, a product is added to the shopping cart and the cart object is stored in the session. In the viewShoppingCart method, the cart object is retrieved from the session and added to the model for the view to use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Spring Boot, the @SessionScope annotation is used to specify that a bean has a session-level scope. This means that each user session will have its own instance. When using the @SessionScope annotation, Spring will create a new instance of a Bean for each user session and store it in the user&#8217;s session. This allows [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-20357","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is the usage of the @SessionScope annotation and Session in Spring Boot? - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is the usage of the @SessionScope annotation and Session in Spring Boot?\" \/>\n<meta property=\"og:description\" content=\"In Spring Boot, the @SessionScope annotation is used to specify that a bean has a session-level scope. This means that each user session will have its own instance. When using the @SessionScope annotation, Spring will create a new instance of a Bean for each user session and store it in the user&#8217;s session. This allows [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T20:20:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-21T17:53:37+00:00\" \/>\n<meta name=\"author\" content=\"Isabella Edwards\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Isabella Edwards\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/\"},\"author\":{\"name\":\"Isabella Edwards\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\"},\"headline\":\"What is the usage of the @SessionScope annotation and Session in Spring Boot?\",\"datePublished\":\"2024-03-15T20:20:03+00:00\",\"dateModified\":\"2024-03-21T17:53:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/\"},\"wordCount\":314,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/\",\"name\":\"What is the usage of the @SessionScope annotation and Session in Spring Boot? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T20:20:03+00:00\",\"dateModified\":\"2024-03-21T17:53:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is the usage of the @SessionScope annotation and Session in Spring Boot?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\",\"name\":\"Isabella Edwards\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"caption\":\"Isabella Edwards\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What is the usage of the @SessionScope annotation and Session in Spring Boot? - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/","og_locale":"en_US","og_type":"article","og_title":"What is the usage of the @SessionScope annotation and Session in Spring Boot?","og_description":"In Spring Boot, the @SessionScope annotation is used to specify that a bean has a session-level scope. This means that each user session will have its own instance. When using the @SessionScope annotation, Spring will create a new instance of a Bean for each user session and store it in the user&#8217;s session. This allows [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T20:20:03+00:00","article_modified_time":"2024-03-21T17:53:37+00:00","author":"Isabella Edwards","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Isabella Edwards","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/"},"author":{"name":"Isabella Edwards","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd"},"headline":"What is the usage of the @SessionScope annotation and Session in Spring Boot?","datePublished":"2024-03-15T20:20:03+00:00","dateModified":"2024-03-21T17:53:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/"},"wordCount":314,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/","url":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/","name":"What is the usage of the @SessionScope annotation and Session in Spring Boot? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T20:20:03+00:00","dateModified":"2024-03-21T17:53:37+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-usage-of-the-sessionscope-annotation-and-session-in-spring-boot\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What is the usage of the @SessionScope annotation and Session in Spring Boot?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd","name":"Isabella Edwards","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","caption":"Isabella Edwards"},"url":"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/20357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=20357"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/20357\/revisions"}],"predecessor-version":[{"id":54150,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/20357\/revisions\/54150"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=20357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=20357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=20357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}