【问题标题】:Static class for retrieve session bean in Spring and JSF在 Spring 和 JSF 中检索会话 bean 的静态类
【发布时间】: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 配置中完成,但我更喜欢配置而不是代码,当然。

标签: java spring jsf


【解决方案1】:

我不确定这是否可行,我现在也没有办法尝试,但是如何:

public static UserPreferences getUserPreferences(){

    return (UserPreferences) ContextLoader.getCurrentWebapplicationContext()
                                            .getBean("userPreferences");
}

【讨论】:

  • 我会试试的。谢谢!至少,我现在知道如何从我的任何代码中检索 webApplicationContext。 XD
  • @user551263 但我认为它可能不适用于请求或会话范围。我认为这些范围只能在 Web 控制器内工作。但值得一试。
【解决方案2】:

定义一个像 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 对象,并且方法执行将委托给会话范围的 bean。

public void test() {
     UserPreferences userPrefs = UserPrefHelper.getUserPrefs();
    //That's all. Don't worry about static access and thread safety. Spring is clever enough.                      
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多