我需要一个可用于服务和数据访问层的 session bean,但我不想将它注入(inject)到每个对象中。
我不想要这个:
<!-- 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>
是否可以创建一个静态类来检索当前请求的 session 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")
}
}
最佳答案
我不确定这是否有效,我现在也没有办法尝试,但是这个怎么样:
public static UserPreferences getUserPreferences(){
return (UserPreferences) ContextLoader.getCurrentWebapplicationContext()
.getBean("userPreferences");
}
关于java - 用于在 Spring 和 JSF 中检索 session bean 的静态类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8869092/