<bean id="userFacade" class="com.test.facade.UserFacadeImpl">
<property name="userDao" ref="userDao"/>
<property name="currentUser" ref="user"/>
</bea开发者_如何学JAVAn>
<bean id="user" class="com.test.beans.User" scope="session">
<aop:scoped-proxy/>
</bean>
UserDao and user are passed to it - user being a scope and facade a singleton. So any request to userfacade is going to return a same object but user will be different for each session - the concept session inside a singleton confuses me. Can someone explain?
The "scoped proxy" is a transparent wrapper around your User
bean. When a method on that proxy is invoked, it will look up the current HttpSession
using Spring's thread-local mechanism (called the RequestContextHolder
), and then fetch the User
object from inside the session's attributes. If none exists in that session, a new one is created and stored in the session. The "real" method on that User
is then invoked.
The big caveat with scoped proxies is that the proxy's methods can only be invoked if the scope is "active", e.g. if the currently executing thread is a servlet request.
The instance of User
injected into UserFacadeImpl
is a proxy which delegates method calls to the actual session-scoped instances of User
.
See also:
- Proxy pattern
精彩评论