Injection of EJB3 into Annotation Based JSF2 Backi

2020-03-06 20:42发布

问题:

I have seen other thread with similar issue but was not able to work out direct link with my problem . Hopefully I am not repeating a question.My issue is this:
I am using Webpsphere 7 with JSF2 as Shared Isolated Library .
When I try to inject an EJB3 into in an annotated JSF2 backing bean I get this error

Caused by: javax.naming.NameNotFoundException: Name comp/env/helloworld.HelloBean not found in context "java:".

but It works fine if we configure JSF2 using face.xml and not use annotation. However without EJB3 injection we can use the application with JSF2 annotations and not see any issue only when we try to inject ejb3 we get issues. Code from a simple trial app is below

Service interface:

package au.com.test;

import javax.ejb.Local;

@Local
public interface SampleService {
    public String getServiceName();
}

Service Implementation:

package au.com.test;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;


@Stateless
@TransactionAttribute (TransactionAttributeType.NOT_SUPPORTED)
public class SampleSession implements SampleService {

    @Override
    public String getServiceName() {
        return "Sample Service";
    }

}

JSF managed bean:

package helloworld;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import au.com.example.service.SampleService;

@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String name;    

    @EJB
    private SampleService sampleService;


    public String getName() {       
        return  sampleService.getServiceName()+ name;       
    }

    public void setName(String name) {
        this.name = name;
    }

}

The Stack trace is below

[2/08/11 7:52:20:394 EST] 00000034 HtmlImageRend W   ALT attribute is missing for : waveImg
[2/08/11 7:52:20:488 EST] 00000034 FaceletViewDe E   Error Rendering View[/hello.xhtml]
                                 javax.faces.FacesException: Could not retrieve value of component with path : {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /hello.xhtml][Class: javax.faces.component.html.HtmlBody,Id: j_id327415688_1383f7c7][Class: javax.faces.component.html.HtmlForm,Id: helloForm][Class: javax.faces.component.html.HtmlInputText,Id: username]}
    at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getValue(RendererUtils.java:347)
    at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getStringValue(RendererUtils.java:291)
    at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.renderInputBegin(HtmlTextRendererBase.java:169)
    at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.renderInput(HtmlTextRendererBase.java:158)
    at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.encodeEnd(HtmlTextRendererBase.java:75)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:519)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:672)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:668)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:668)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:668)
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1483)
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:281)
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:179)
    at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:353)
    at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:784)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3933)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1583)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1604)
Caused by: javax.faces.FacesException: javax.naming.NameNotFoundException: Name comp/env/helloworld.HelloBean not found in context "java:".
    at org.apache.myfaces.config.ManagedBeanBuilder.buildManagedBean(ManagedBeanBuilder.java:225)
    at org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.createManagedBean(ManagedBeanResolver.java:332)
    at org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.getValue(ManagedBeanResolver.java:295)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:143)
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
    at org.apache.myfaces.el.VariableResolverImpl.resolveVariable(VariableResolverImpl.java:65)
    at org.apache.myfaces.el.convert.VariableResolverToELResolver.getValue(VariableResolverToELResolver.java:116)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:143)
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:45)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:91)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:263)
    at org.apache.myfaces.view.facelets.el.TagValueExpression.getValue(TagValueExpression.java:85)
    at javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:246)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:71)
    at javax.faces.component.UIInput.getValue(UIInput.java:142)
    at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getValue(RendererUtils.java:343)
    ... 39 more
Caused by: javax.naming.NameNotFoundException: Name comp/env/helloworld.HelloBean not found in context "java:".
    at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1837)
    at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1166)
    at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1095)
    at com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1233)
    at com.ibm.ws.naming.java.javaURLContextImpl.lookup(javaURLContextImpl.java:395)
    at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:214)
    at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:154)
    at javax.naming.InitialContext.lookup(InitialContext.java:436)
    at org.apache.myfaces.config.annotation.ResourceAnnotationLifecycleProvider.lookupFieldResource(ResourceAnnotationLifecycleProvider.java:127)
    at org.apache.myfaces.config.annotation.AllAnnotationLifecycleProvider.checkFieldAnnotation(AllAnnotationLifecycleProvider.java:83)
    at org.apache.myfaces.config.annotation.ResourceAnnotationLifecycleProvider.checkAnnotation(ResourceAnnotationLifecycleProvider.java:78)
    at org.apache.myfaces.config.annotation.ResourceAnnotationLifecycleProvider.processAnnotations(ResourceAnnotationLifecycleProvider.java:55)
    at org.apache.myfaces.config.annotation.NoInjectionAnnotationLifecycleProvider.newInstance(NoInjectionAnnotationLifecycleProvider.java:46)
    at org.apache.myfaces.config.ManagedBeanBuilder.buildManagedBean(ManagedBeanBuilder.java:155)
    ... 55 more

[2/08/11 7:52:20:503 EST] 00000034 ErrorPageWrit E   An exception occurred
                                 javax.faces.FacesException: javax.naming.NameNotFoundException: Name comp/env/helloworld.HelloBean not found in context "java:".
    at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.wrap(ExceptionHandlerImpl.java:241)
    at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:156)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:258)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:179)
    at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:353)
    at com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:784)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3933)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1583)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1604)
Caused by: javax.naming.NameNotFoundException: Name comp/env/helloworld.HelloBean not found in context "java:".
    at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1837)
    at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1166)
    at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1095)
    at com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1233)
    at com.ibm.ws.naming.java.javaURLContextImpl.lookup(javaURLContextImpl.java:395)
    at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:214)
    at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:154)
    at javax.naming.InitialContext.lookup(InitialContext.java:436)
    at org.apache.myfaces.config.annotation.ResourceAnnotationLifecycleProvider.lookupFieldResource(ResourceAnnotationLifecycleProvider.java:127)
    at org.apache.myfaces.config.annotation.AllAnnotationLifecycleProvider.checkFieldAnnotation(AllAnnotationLifecycleProvider.java:83)
    at org.apache.myfaces.config.annotation.ResourceAnnotationLifecycleProvider.checkAnnotation(ResourceAnnotationLifecycleProvider.java:78)
    at org.apache.myfaces.config.annotation.ResourceAnnotationLifecycleProvider.processAnnotations(ResourceAnnotationLifecycleProvider.java:55)
    at org.apache.myfaces.config.annotation.NoInjectionAnnotationLifecycleProvider.newInstance(NoInjectionAnnotationLifecycleProvider.java:46)
    at org.apache.myfaces.config.ManagedBeanBuilder.buildManagedBean(ManagedBeanBuilder.java:155)
    at org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.createManagedBean(ManagedBeanResolver.java:332)
    at org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.getValue(ManagedBeanResolver.java:295)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:143)
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
    at org.apache.myfaces.el.VariableResolverImpl.resolveVariable(VariableResolverImpl.java:65)
    at org.apache.myfaces.el.convert.VariableResolverToELResolver.getValue(VariableResolverToELResolver.java:116)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:143)
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:45)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:91)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:263)
    at org.apache.myfaces.view.facelets.el.TagValueExpression.getValue(TagValueExpression.java:85)
    at javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:246)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:71)
    at javax.faces.component.UIInput.getValue(UIInput.java:142)
    at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getValue(RendererUtils.java:343)
    at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getStringValue(RendererUtils.java:291)
    at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.renderInputBegin(HtmlTextRendererBase.java:169)
    at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.renderInput(HtmlTextRendererBase.java:158)
    at org.apache.myfaces.shared_impl.renderkit.html.HtmlTextRendererBase.encodeEnd(HtmlTextRendererBase.java:75)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:519)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:672)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:668)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:668)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:668)
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1483)
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:281)
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
    ... 26 more


Can any one help me with this .

UPDATE
These are two separate modules sitting inside an EAR which gets deployed WAS7

回答1:

Shared libraries are not considered for locating component-defining annotations (like @ManagedBean), so the component is not found, which means its @EJB is not found while building the java:comp namespace. Your options are:

  1. Mention the JSF bean in faces-config.xml in the WAR.
  2. Place the JSF bean in the WAR.
  3. Define the <ejb-ref> in web.xml.
  4. Define the ejb-ref with annotations in a dummy "resource producer" bean in the WAR.

For #4, an example would be:

@ManagedBean
@EJBs({
   @EJB(name="helloworld.HelloBean", type=SampleService.class)
   // ... etc
})
public class ResourceProducerBean { }