本文介绍了JSF 2.x @EJB依赖注入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,正如通常在编程中所做的那样,我问了一个问题和一个潜在的解决方案导致了更多的问题(和错误。)我是新手使用servlet,JSF和EJB并且遇到了依赖注入的错误。

So, as it usually goes in programming, I asked one question and a potential solution led to more questions (and bugs.) I'm new to using servlets, JSF, and EJBs and have run into an error with dependency injection.

这是我原来的问题:

我被告知我需要问一个新问题,粘贴我的托管bean代码和注入错误导致的堆栈跟踪。任何帮助将不胜感激。

I was told I needed to ask a new question, pasting my managed bean code and the stack trace that resulted from the injection error. Any help would be greatly appreciated.

ManagedBean

ManagedBean

@ManagedBean(name ="draftSavvyController")
@RequestScoped
public class DraftSavvyController
{
    // ======================================
    // =             Attributes             =
    // ======================================
    @EJB
    private DraftSavvyBean draftBean;
    private Person person;
    private String name;
    private String password;
    private String userType;
    private String searchTerm;
    private static final String apiKey = "4765b95b1bb4ca5248261b7b9af443db";

    // ======================================
    // =           Public Methods           =
    // ======================================
    public String doRegisterUser()
    {
        draftBean.registerUser(name, password, userType);
        return "chooseBeers.xhtml";
    }

    public String doCheckForUser()
    {
        if(draftBean.checkForUser(name, password))
        {
            return "chooseBeers.xhtml";
        }
        else return "loginerror.xhtml";
    }

    public String searchForBeers() throws MalformedURLException, IOException, JSONException
    {
        draftBean.searchForBeers(searchTerm, apiKey);

        return "displaybeers.js";
    }

Stack Trace

Stack Trace

com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean draftSavvyController
    at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:211)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:103)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:179)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)
Caused by: com.sun.faces.spi.InjectionProviderException: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=com.draftsavvy.frontend.DraftSavvyController/draftBean,Remote 3.x interface =com.draftsavvy.frontend.DraftSavvyBean,ejb-link=null,lookup=,mappedName=,jndi-name=com.draftsavvy.frontend.DraftSavvyBean,refType=Session into class com.draftsavvy.frontend.DraftSavvyController: Lookup failed for 'java:comp/env/com.draftsavvy.frontend.DraftSavvyController/draftBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
    at org.glassfish.faces.integration.GlassFishInjectionProvider.inject(GlassFishInjectionProvider.java:194)
    at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:205)
    ... 56 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=com.draftsavvy.frontend.DraftSavvyController/draftBean,Remote 3.x interface =com.draftsavvy.frontend.DraftSavvyBean,ejb-link=null,lookup=,mappedName=,jndi-name=com.draftsavvy.frontend.DraftSavvyBean,refType=Session into class com.draftsavvy.frontend.DraftSavvyController: Lookup failed for 'java:comp/env/com.draftsavvy.frontend.DraftSavvyController/draftBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:703)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:470)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:171)
    at org.glassfish.faces.integration.GlassFishInjectionProvider.inject(GlassFishInjectionProvider.java:184)
    ... 57 more
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/com.draftsavvy.frontend.DraftSavvyController/draftBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=com.draftsavvy.frontend.DraftSavvyController/draftBean,Remote 3.x interface =com.draftsavvy.frontend.DraftSavvyBean,ejb-link=null,lookup=,mappedName=,jndi-name=com.draftsavvy.frontend.DraftSavvyBean,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' [Root exception is javax.naming.NamingException: Lookup failed for 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean not found]]]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:599)
    ... 60 more
Caused by: javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=com.draftsavvy.frontend.DraftSavvyController/draftBean,Remote 3.x interface =com.draftsavvy.frontend.DraftSavvyBean,ejb-link=null,lookup=,mappedName=,jndi-name=com.draftsavvy.frontend.DraftSavvyBean,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' [Root exception is javax.naming.NamingException: Lookup failed for 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean not found]]
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:191)
    at com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$EjbReferenceProxy.create(ComponentEnvManagerImpl.java:1109)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:776)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:744)
    at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:169)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
    ... 64 more
Caused by: javax.naming.NamingException: Lookup failed for 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean not found]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:186)
    ... 69 more
Caused by: javax.naming.NameNotFoundException: com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean not found
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:248)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:215)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77)
    at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:119)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:505)
    ... 73 more

无国籍豆

@Stateless
public class DraftSavvyBean implements DraftSavvyBeanRemote {

    // ======================================
    // =             Attributes             =
    // ======================================

    @PersistenceContext(unitName = "DraftSavvyFrontEndPU")
    private EntityManager em;

    // ======================================
    // =           Public Methods           =
    // ======================================
    public void registerUser(String username, String password, String userType)
    {
        //code here
    }

    public boolean checkForUser(String username, String password)
    {
        //code here
    }

    public void searchForBeers(String searchTerm, String apiKey) throws MalformedURLException, IOException, JSONException
    {
        //code here
    }

}


推荐答案

正如Petr Mensik所说,您可能不需要远程EJB(除非您计划从不同的服务器调用该ejb的方法)。您可以使用 @Local 界面,甚至根本不使用界面。只需声明bean @Stateless并删除实现DraftSavvyBeanRemote

As Petr Mensik said, you probably don't need a remote EJB (unless you plan to call methods of that ejb from a different server). You can use a @Local interface, or even not interface at all. just declare the bean @Stateless and remove the implements DraftSavvyBeanRemote .

如果你使用接口(本地或远程),你必须注入接口的引用,而不是bean。所以你的注入应该是这样的。

If you do use an interface (either local or remote) you have to inject a reference to the interface, not to the bean. So your inject should look like this.

@EJB
private DraftSavvyBeanRemote draftBean;

这篇关于JSF 2.x @EJB依赖注入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:40