在Glassfish 3.1.2上部署ear并尝试从Web服务中的@PostConstruct方法查找EJB bean:
@PostConstruct
public void init()
{
try
{
InitialContext initialContext = new InitialContext();
browserService = (IBrowserService) initialContext.lookup("java:comp/env/BrowserService");
}
catch (NamingException ex)
{
Logger.getLogger(BrowserWS.class.getName()).log(Level.SEVERE, null, ex);
}
}
得到了
[#| 2012-04-25T10:15:54.768 + 0300 |严重| glassfish3.1.2 | browser.service.BrowserWS | _ThreadID = 22; _ThreadName =管理线程池-4848(4- ); | javax.naming.NamingException:SerialContext中的'java:comp / env / BrowserService'查找失败[myEnv = {java.naming.factory.initial = com.sun.enterpri se.naming.impl.SerialInitContextFactory,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,java.naming.factory.ur l.pkgs = com.sun.enterprise.naming} [根异常是javax.naming.NamingException:调用异常:得到null ComponentInvocation] 在com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518) 在com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
当我尝试从网络方法做同样的事情时,一切正常。
@WebMethod
public XmlLookupResult lookup(
@WebParam(name = "inLookupId", mode = WebParam.Mode.IN) String id,
@WebParam(name = "inLookupParams", mode = WebParam.Mode.IN) List<String> params,
@WebParam(name = "inFetchLimit", mode = WebParam.Mode.IN) int limit)
{
try
{
InitialContext initialContext = new InitialContext();
browserService = (IBrowserService) initialContext.lookup("java:comp/env/BrowserService");
}
catch (NamingException ex)
{
Logger.getLogger(BrowserWS.class.getName()).log(Level.SEVERE, null, ex);
}
return browserService.lookup(id, params, limit);
}
那么,为什么我不能从@PostConstruct方法查找bean?
从规范中获得JNDI访问java:comp / env from PostConstruct ...
由于
UPD:
BrowserService在web.xml中声明为
<ejb-local-ref>
<ejb-ref-name>BrowserService</ejb-ref-name>
<local>browser.service.IBrowserService</local>
</ejb-local-ref>