Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 453 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring store object in session

#1
I would like to implement a shopping cart with Spring, so I need to save an object `Cart` ( which has attributes like products, paymentType and deliveryType ) in session. I've tried to create it with bean and attribute "scope" set to "session", but it just doesn't work, should I use some additional annotations in my controller or `Cart` class? Any example usage would be really helpful :-) Thanks in advance.
Reply

#2
You just need to add Scope annotation as below with session and proxy mode


@Component
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class ShoppingCart implements Serializable{
}

Where ever you need to use shopping cart object, you can autowire it

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
Logger logger = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);


@Autowired
ShoppingCart shoppingCart;
}

Disclosure: I have developed a sample project, which uses spring MVC, angularJS and bootstrap that demonstrate Spring Session scope -

[To see links please register here]

Reply

#3
If you are injecting the shopping cart directly into your controller, the issue is likely happening because your controller is singleton scoped (by default), which is wider scope than the bean you're injecting. This excellent article gives an overview of four approaches to exactly what you're trying to do:

[To see links please register here]

.

Here's a quick summary of solutions:

1. Scope the controller to session scope (use `@scope("session")` on controller level) and just have a shopping cart instance in the controller.
2. Scope the controller to request and have session-scoped shopping cart injected.
3. Just use the session directly - kind of messy, IMO.
4. Use Spring's annotation `<aop:scoped-proxy/>`.

All of the methods have their pros and cons. I usually go with option 2 or 4. Option 4 is actually pretty simple and is the only approach I have seen [documented by Spring.](

[To see links please register here]

)
Reply

#4
@Component
@Scope("session")
public class Cart { .. }

and then

@Inject
private Cart cart;

should work, if it is declared in the web context (dispatcher-servlet.xml). An alternative option is to use the raw session and put your cart object there:


@RequestMapping(..)
public String someControllerMethod(HttpSession session) {
session.setAttribute(Constants.CART, new Cart());
...
Cart cart = (Cart) session.getAttribute(Constants.CART);
}
Reply

#5
try to autowired HttpSession and spring will injects in a proxy to the HttpSession
`
@Autowired
private HttpSession httpSession;
`
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through