title: How does the session scope of a bean work in a Spring MVC application?
body: <p>I am pretty new in Spring MVC and I have a doubt about the <strong>session scope</strong> of a bean.</p>
  
  <p>Into a project I have a <code>Cart</code> bean, this one:</p>
  
  <pre><code>@Component
  @Scope(value=WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
  public class Cart {
  
  
      private Map&lt;Product, Integer&gt; contents = new HashMap&lt;&gt;();
  
      public Map&lt;Product, Integer&gt; getContents() {
          return contents;
      }
  
      public Set&lt;Product&gt; getProducts() {
          return contents.keySet();
      }
  
      public void addProduct(Product product, int count) {
  
          if (contents.containsKey(product)) {
              contents.put(product, contents.get(product) + count);
          } 
          else {
              contents.put(product, count);
          }
      }
  
  
      public void removeProduct(Product product) {
          contents.remove(product);
      }
  
      public void clearCart() {
          contents.clear();
      }
  
      @Override
      public String toString() {
          return contents.toString();
      }
  
      public double getTotalCost() {
          double totalCost = 0;
          for (Product product : contents.keySet()) {
              totalCost += product.getPrice();
          }
          return totalCost;
      }
  
  }
  </code></pre>
  
  <p>So this bean is automatically detected as component by the container and it is set as a <strong>session bean</strong> by:</p>
  
  <pre><code>@Scope(value=WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
  </code></pre>
  
  <p>So, from what I have understand it means that it is automatically created a single bean for each user session.</p>
  
  <p>In my example the <code>Cart</code> class represent a shopping cart in which a logged user put the items that want buy. Does it mean that there exists a single <code>Cart</code> bean for each logged user section into the <code>HttpSession</code>? So this bean is into the session and the user can add or delete item from it. Is this interpretation right or am I missing something?</p>
  
  <p>Another doubt is related to the <code>proxyMode = ScopedProxyMode.TARGET_CLASS</code> attribute. What exactly does that mean? Why is it applied to this bean?</p>