简体   繁体   English

Spring Beans中的EJB 3 Sessioncontext

[英]EJB 3 Sessioncontext in Spring Beans

Background: 背景:

I am using programmatic authentication (basic authentication) using java security. 我正在使用Java安全性使用程序验证(基本验证)。 I have a Stateless session bean(EJB 3). 我有一个无状态会话bean(EJB 3)。 I can inject the Sessioncontext to get the security Principal, to get the user name. 我可以注入Sessioncontext以获取安全主体,以获取用户名。 In the same project I am using spring beans for JDBC and Aspect. 在同一项目中,我将Spring bean用于JDBC和Aspect。 I want to get the user name in one of the aspect(Audit) which is also a spring bean. 我想以aspect(Audit)之一获取用户名,它也是spring bean。

Question: 题:

Is it possible to access the ejb sesssioncontext in the Spring bean. 是否可以在Spring bean中访问ejb sesssioncontext。 If so how to do that? 如果是这样,该怎么做? If no, how can the username be accessed from the Spring bean which is also an aspect. 如果没有,如何从Spring bean访问用户名,这也是一个方面。

Thanks, Amit 谢谢阿米特

This can be done by injecting the EJB into the spring bean. 这可以通过将EJB注入spring bean中来完成。 You can do it through a manual JNDI lookup (like you would in any other EJB Client) or use Spring's JndiObjectFactoryBean. 您可以通过手动JNDI查找来完成此操作(就像在其他任何EJB客户端中一样)或使用Spring的JndiObjectFactoryBean。 With Spring's JndiObjectFactoryBean: 使用Spring的JndiObjectFactoryBean:

  1. Inject the SessionContext into the EJB SessionContext注入EJB

     @Resource private SessionContext ctxt; //getter and setter 
  2. Configure the factory bean in your bean configuration file. 在bean配置文件中配置工厂bean。 postageService being the EJBRef we're interested in (Configuration poached from Apress Spring Recipes ) postageService是我们感兴趣的EJBRef(从Apress Spring Recipes中 postageService进行了配置)

      <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. Wire the ejb reference into your spring bean 将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 } } 

kolossus, Thanks for the reply. kolossus,谢谢您的答复。 There is another way that I could found. 我可以找到另一种方法。 Following is the Link to the previous post . 以下是上一篇文章的链接。 So basically I did exactly the same thing : 1) Added the following line to the spring-context xml 所以基本上我做了完全一样的事情:1)将以下行添加到spring-context xml中

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

2) Added following line to the my spring bean to access the EJB's session context 2)在我的spring bean中添加了以下行以访问EJB的会话上下文

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

Important thing is Jboss 7.1 does not support custom jndi any more unlike Jboss 5 so I used the default jndi. 重要的是,Jboss 7.1与Jboss 5不同,不再支持自定义jndi,因此我使用了默认的jndi。

I think this is more cleaner way to address my requirement. 我认为这是解决我要求的更清洁的方法。

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

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