I have a stateless SOAP Web service(Java, Spring, Tomcat).
I want to keep some global variables per session for easy access and avoiding using static variables cause they will have a开发者_StackOverflow container scope.
How can I do that? if only way to do it is with spring bean scopes then which scope is better prototype or singleton scope and why?
thanks
Spring bean scope could be used for this .
session scope
example:
<bean id="customerService" class="com.customer.services.CustomerService"
scope="session"/>
(This is not an answer to the original question but tries to answer some additional questions from the comment area)
A Session bean may be stateless or stateful. If it is stateless, it takes a request, sends a response (maybe) and is destroyed afterwards. A stateful session stores some parameters (a state) that can be reused for other method calls - like a bean that simply counts internally how often it has been invoked.
A synchronous web service can be implemented with a stateless session bean. It takes a request, does some calculation, returns a response and is finished (the bean can be destroyed). In that case the client waits for the answer, it's like calling a simple Java method.
That's different for asynchronous web services: The service receives a request and either
- receives a callback URL or
- responds with a session ID
In any case - the client won't wait for the response but continue, until it receives the response, either because
- the service has sent the response to the clients callback method or
- the client has polled several times using the session ID and eventually polled the service response.
Asynchronous service is like starting a Java thread: you start it, continue with your work and at some point you get the result (or a notification, that the result is ready)
And an asynchronous service needs some kind of persistence store to store the session ID or the callback URL while the service session is active. The service session is stateful, the session beans that are needed for the service can be stateless.
精彩评论