简体   繁体   English

Spring和JSF中检索会话bean的静态类

[英]Static class for retrieve session bean in Spring and JSF

I need a session bean accesible for service and data access layers but I don't want inject it in every object. 我需要一个可用于服务和数据访问层的会话bean,但我不想将其注入每个对象中。

I don't want this: 我不想要这个:

 <!-- 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>

Is it posible to create a static class for retrieving the session bean of the current request? 是否可以创建一个静态类来检索当前请求的会话bean?

Something like this: 像这样的东西:

 <!-- 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")
  }

}

I'm not sure this works and I don't have a way to try it right now, but how about this: 我不确定这是否有效,我现在没有办法尝试,但是这个怎么样:

public static UserPreferences getUserPreferences(){

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

Define a helper class like UserPrefHelper.java 定义一个辅助类,如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>

Then use that helper class in your classes directly and that's all. 然后直接在您的类中使用该助手类,这就是全部。 It will turn you a proxied UserPreferences object every time and method execution will be delegated to session scoped 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.                      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM