Определить вспомогательный класс, например UserPrefHelper.java
public class UserPrefHelper {
private static com.foo.UserPreferences userPrefs;
private void setUserPrefs(com.foo.UserPreferences userPrefs) {
this.userPrefs = userPrefs;
}
private static UserPreferences getUserPrefs() {
return userPrefs;
}
}
<!-- a HTTP Session-scoped bean exposed as a proxy -->
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
<!-- this next element effects the proxying of the surrounding bean -->
<aop:scoped-proxy/>
</bean>
<!-- a singleton-scoped bean injected with a proxy to the above bean -->
<bean id="userPrefHelper" class="com.foo.UserPrefHelper">
<!-- a reference to the proxied 'userPreferences' bean -->
<property name="userPreferences" ref="userPreferences"/>
</bean>
Тогда используйте этот вспомогательный класс в ваших классах напрямую, и все. Он будет превращать вас в прокси-объект UserPreferences каждый раз, и выполнение метода будет делегировано сессионному компоненту.
public void test() {
UserPreferences userPrefs = UserPrefHelper.getUserPrefs();
//That's all. Don't worry about static access and thread safety. Spring is clever enough.
}