【发布时间】:2012-01-15 10:44:13
【问题描述】:
我需要一个可用于服务和数据访问层的会话 bean,但我不想将它注入到每个对象中。
我不想要这个:
<!-- 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="userService" class="com.foo.SimpleUserService">
<!-- a reference to the proxied 'userPreferences' bean -->
<property name="userPreferences" ref="userPreferences"/>
</bean>
是否可以创建一个静态类来检索当前请求的会话 bean?
类似这样的:
<!-- 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>
Public Class sessionResolver{
public static UserPreferences getUserPreferences(){
//Not real code!!!
return (UserPreferences)WebApplicationContex.getBean("userPreferences")
}
}
【问题讨论】:
-
我把模拟代码放在 getUserPreferences 中是因为我认为它不能在 spring 配置中完成,但我更喜欢配置而不是代码,当然。