我需要一個會話bean可訪問服務和數據訪問層,但我不想將它注入到每個對象中。在Spring和JSF中檢索會話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中,因爲我認爲它不能在春季配置中完成,但我更喜歡使用配置代替代碼。 – jlvaquero 2012-01-15 11:33:53