【问题标题】:EJB 3 Sessioncontext in Spring BeansSpring Beans 中的 EJB 3 会话上下文
【发布时间】:2013-03-27 23:54:50
【问题描述】:

背景:

我正在使用 java 安全性的编程身份验证(基本身份验证)。 我有一个无状态会话 bean(EJB 3)。我可以注入 Sessioncontext 以获取安全主体,以获取用户名。 在同一个项目中,我将 spring bean 用于 JDBC 和 Aspect。我想在其中一个方面(审计)中获取用户名,这也是一个 spring bean。

问题:

是否可以在 Spring bean 中访问 ejb sesssioncontext。如果是这样怎么做? 如果没有,如何从 Spring bean 中访问用户名,这也是一个方面。

谢谢, 阿米特

【问题讨论】:

  • 注意:我没有使用spring security...

标签: java spring jakarta-ee ejb-3.0


【解决方案1】:

这可以通过将 EJB 注入到 spring bean 中来完成。您可以通过手动 JNDI 查找(就像在任何其他 EJB 客户端中一样)或使用 Spring 的 JndiObjectFactoryBean 来完成。使用 Spring 的 JndiObjectFactoryBean:

  1. SessionContext 注入EJB

    @Resource
    private SessionContext ctxt;
    //getter and setter
    
  2. 在您的 bean 配置文件中配置工厂 bean。 postageService 是我们感兴趣的 EJBRef(配置来自 Apress Spring Recipes

       <bean id="postageService class="org.springframework.jndi.JndiObjectFactoryBean">
             <property name="jndiEnvironment">
                <props>
                   <prop key="java.naming.factory.initial">
                        org.apache.openejb.client.RemoteInitialContextFactory
                    </prop>
                   <prop key="java.naming.provider.url">
                         ejbd://localhost:4201
                   </prop>
               </props>
             </property>
            <property name="jndiName" value="PostageServiceBeanRemote"/>
      </bean>
    
  3. 将 ejb 引用连接到你的 spring bean

         public class PostalServiceClient{
    
           //wired EJB
           private PostageService service;
           //getter and setter
    
           private SessionContext retrieveSessionContext(){
               service.getCtxt(); //get the SessionContext from the EJB injection
           }
    
          } 
    

【讨论】:

    【解决方案2】:

    kolossus,感谢您的回复。 我可以找到另一种方法。以下是previous post 的链接。所以基本上我做了同样的事情: 1) 将以下行添加到 spring-context xml

    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
        <property name="alwaysUseJndiLookup" value="true" />
    </bean>
    

    2) 在 my spring bean 中添加以下行以访问 EJB 的会话上下文

    @Resource(mappedName = "java:global/TopoServices/MessageRestService!com.myrest.MessageRestService")
        private MessageRestService myBean;
    

    重要的是 Jboss 7.1 不再支持自定义 jndi,这与 Jboss 5 不同,所以我使用了默认的 jndi。

    我认为这是满足我的要求的更清洁的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多