[英]EJB 3 Sessioncontext in Spring Beans
背景:
我正在使用Java安全性使用程序验证(基本验证)。 我有一个无状态会话bean(EJB 3)。 我可以注入Sessioncontext以获取安全主体,以获取用户名。 在同一项目中,我将Spring bean用于JDBC和Aspect。 我想以aspect(Audit)之一获取用户名,它也是spring bean。
题:
是否可以在Spring bean中访问ejb sesssioncontext。 如果是这样,该怎么做? 如果没有,如何从Spring bean访问用户名,这也是一个方面。
谢谢阿米特
这可以通过将EJB注入spring bean中来完成。 您可以通过手动JNDI查找来完成此操作(就像在其他任何EJB客户端中一样)或使用Spring的JndiObjectFactoryBean。 使用Spring的JndiObjectFactoryBean:
将SessionContext
注入EJB
@Resource private SessionContext ctxt; //getter and setter
在bean配置文件中配置工厂bean。 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>
将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,谢谢您的答复。 我可以找到另一种方法。 以下是上一篇文章的链接。 所以基本上我做了完全一样的事情:1)将以下行添加到spring-context xml中
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
<property name="alwaysUseJndiLookup" value="true" />
</bean>
2)在我的spring bean中添加了以下行以访问EJB的会话上下文
@Resource(mappedName = "java:global/TopoServices/MessageRestService!com.myrest.MessageRestService")
private MessageRestService myBean;
重要的是,Jboss 7.1与Jboss 5不同,不再支持自定义jndi,因此我使用了默认的jndi。
我认为这是解决我要求的更清洁的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.