[英]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.