java 在 Weblogic 10.xx 上使用 InitialContext 查找 EJB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7090512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lookup EJB using InitialContext on Weblogic 10.x.x
提问by kardanov
Could you please tell me how to lookup EJB on Weblogic?
I have following bean:
你能告诉我如何在 Weblogic 上查找 EJB 吗?
我有以下豆子:
@Stateless
@EJB(name = "DataAccess", beanInterface = DataAccessLocal.class)
public class DataAccess implements DataAccessLocal {
...
}
I need this bean in other class which is not part of managed content (just simple class), so I guess it should be done like this:
我在其他不属于托管内容的类(只是简单的类)中需要这个 bean,所以我想应该这样做:
DataAccessLocal dataAccess = DataAccessLocal.class.cast((new InitialContext()).lookup("%SOME_JNDI_NAME%"));
The question is what should be used as %SOME_JNDI_NAME% in case of Weblogic 10.x.x AS?
Any help will be appreciated.
问题是在 Weblogic 10.xx AS 的情况下应该使用什么作为 %SOME_JNDI_NAME%?
任何帮助将不胜感激。
回答by Jeff West
I would update your EJB class to look like this:
我会将您的 EJB 类更新为如下所示:
@Stateless(name="DataAccessBean", mappedName="ejb/DataAccessBean")
@Remote(DataAccessRemote.class)
@Local(DataAccessLocal.class)
public class DataAccess implements DataAccessLocal, DataAccessRemote {
...
}
Looking up the EJB from a class deployed in the same EAR (using the local interface):
从部署在同一 EAR 中的类中查找 EJB(使用本地接口):
InitialContext ctx = new InitialContext(); //if not in WebLogic container then you need to add URL and credentials.
// use <MAPPED_NAME>
Objet obj = ctx.lookup("java:comp/env/ejb/DataAccessBean");
EJB injection is usually preferred, and you can do it as follows:
EJB 注入通常是首选,您可以按如下方式进行:
@EJB(name="DataAccessBean")
DataAccessLocal myDataAccessBean;
If you are trying to use the EJB remotely then you will need to use the remote interface and the following JNDI name:
如果您尝试远程使用 EJB,那么您将需要使用远程接口和以下 JNDI 名称:
DataAccessBean#<package>.DataAccessRemote