java - 用于在 Spring 和 JSF 中检索 session bean 的静态类

标签 java spring jsf

我需要一个可用于服务和数据访问层的 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/

相关文章:

JSF2复合和cc的区别

java - 在 CL Java 程序中显示多个消息对话框

Java正则表达式解析/robots.txt

java - Spring MVC - 获取错误的消息包

java - 为什么 INSERT 查询无限期挂起并锁定 PostgreSQL 数据库和 pgAdmin

java - 如何将与提交的表单对应的项目ID传递给action/actionlistener方法?

html - href ="RES_NOT_FOUND"使用 h :outputStylesheet

java - MySQL 查询通过 Java 语法错误

java - 如何在用户按下 ^Z 时退出循环

java - 调整独立应用程序以使用 Spring