To answer your first question: yes, Spring MVCs are single by default. The object field will be shared and visible for all requests and all sessions forever.
However, without any synchronization, you may encounter all types of concurrency problems (race conditions, visibility). Thus, your field must have a volatile (and private volatile , by the way) to avoid visibility problems.
Back to the main question: in Spring, you can use request- (see 4.5.4.2 request scope ) and session scope (see: 4.5.4.3 Session scope ) beans. You can enter them to controllers and any other beans (even singlots!), But Spring ensures that each request / session has an independent instance.
The only thing to remember when introducing beans into a single package with a query and session is to wrap them in a cloud proxy (example from 4.5.4.5 Scoped beans as dependencies ):
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"> <aop:scoped-proxy/> </bean>
Tomasz Nurkiewicz Jun 21 2018-12-12T00: 00Z
source share