部署在WebSphere 7(Java EE 5)上的OmniFaces CDI @ViewScoped bean使用@EJB注入的所有EJB为空:NPE / NullPointerException

时间:2017-07-13 15:28:31

标签: cdi websphere-7 omnifaces view-scope openwebbeans

我在WebSphere 7(7.0.50)上有一个简单的OmniFaces 1.8.3视图范围的bean 成功部署以及OpenWebBeans 1.2.8(和Mojarra 2.1.27 BTW):

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;

import org.omnifaces.cdi.ViewScoped;

...

@Named
@ViewScoped
public class CashValueCalculationManager implements Serializable {

    private static final long serialVersionUID = 1L;

    @EJB
    private CashValueCalculationService cashValueCalculationService;

    // the list of entities to be shown in a datatable
    private List<CashValueCalculation> entities;

    @PostConstruct
    public void init() {

        // displays "this.cashValueCalculationService = null" in the log
        System.out.println("this.cashValueCalculationService = " + this.cashValueCalculationService);

        // load list of entities on init
        try {
            this.loadViewData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<CashValueCalculation> getEntities() {
        return this.entities;
    }

    protected CashValueCalculationService getEntityService() {
        return this.cashValueCalculationService;
    }

    protected void loadViewData() throws Exception {

        // NPE here!
        List<CashValueCalculation> cashValueCalculations = this.getEntityService().findAll();

        this.entities = cashValueCalculations;
    }
}

然后打开bean的视图时,bean被初始化,运行@PostConstruct方法来加载所有实体(只是少数几个),但这在loadViewData()中失败并带有NullPointerException。返回的服务为空...这基本上由日志确认,我也找到了行

this.cashValueCalculationService = null

CashValueCalculationService界面注释为@Local,实施CashValueCalculationServiceBean注明@Stateless

@Local
public interface CashValueCalculationService extends EntityService<Integer, CashValueCalculation> {
    // super interface has findAll()
}

豆:

@Stateless
public class CashValueCalculationServiceBean extends BaseEntityServiceBean<Integer, CashValueCalculation> implements CashValueCalculationService {
    // super class has findAll() implementation
}

在WebSphere 7热潮上尝试使用OmniFaces / CDI / OpenWebBeans之前,我已成功将其注入JSF托管bean。

当在服务器上部署应用程序时,将显示以下日志条目:

.
.
.
[13.07.17 16:56:13:889 CEST] 00000013 WebContainerL I   OpenWebBeans Container is starting...
[13.07.17 16:56:13:894 CEST] 00000013 PluginLoader  I   Adding OpenWebBeansPlugin : [OpenWebBeansJsfPlugin]
[13.07.17 16:56:13:899 CEST] 00000013 AbstractMetaD I   added beans.xml marker: wsjar:file:/C:/IBM/WebSphere7.0/AppServer/profiles/CLDSrv7050/installedApps/srv-cld-helNode03Cell/CLD.ear/lib/omnifaces-1.8.3.jar!/META-INF/beans.xml
[13.07.17 16:56:13:901 CEST] 00000013 AbstractMetaD I   added beans.xml marker: file:/C:/IBM/WebSphere7.0/AppServer/profiles/CLDSrv7050/installedApps/srv-cld-helNode03Cell/CLD.ear/cld-web.war/WEB-INF/beans.xml
[13.07.17 16:56:15:051 CEST] 00000013 AbstractMetaD W   Ignoring class [org.omnifaces.facesviews.FacesViewsInitializer] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.ServletContainerInitializer 
[13.07.17 16:56:15:322 CEST] 00000013 AbstractMetaD W   Ignoring class [org.omnifaces.component.output.cache.CacheInitializerListener] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.FilterRegistration 
[13.07.17 16:56:15:328 CEST] 00000013 AbstractMetaD W   Ignoring class [org.omnifaces.util.Platform] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.ServletRegistration 
[13.07.17 16:56:15:581 CEST] 00000013 AbstractMetaD W   Ignoring class [org.omnifaces.facesviews.FacesViewsInitializer] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.ServletContainerInitializer 
[13.07.17 16:56:15:586 CEST] 00000013 AbstractMetaD W   Ignoring class [org.omnifaces.component.output.cache.CacheInitializerListener] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.FilterRegistration 
[13.07.17 16:56:15:588 CEST] 00000013 AbstractMetaD W   Ignoring class [org.omnifaces.util.Platform] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.ServletRegistration 
[13.07.17 16:56:16:438 CEST] 00000013 AbstractMetaD W   Ignoring class [org.omnifaces.facesviews.FacesViewsInitializer] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.ServletContainerInitializer 
[13.07.17 16:56:16:443 CEST] 00000013 AbstractMetaD W   Ignoring class [org.omnifaces.component.output.cache.CacheInitializerListener] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.FilterRegistration 
[13.07.17 16:56:16:446 CEST] 00000013 AbstractMetaD W   Ignoring class [org.omnifaces.util.Platform] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.ServletRegistration 
[13.07.17 16:56:17:199 CEST] 00000013 BeansDeployer I   All injection points were validated successfully.
[13.07.17 16:56:17:236 CEST] 00000013 WebContainerL I   OpenWebBeans Container has started, it took [3345] ms.
[13.07.17 16:56:17:291 CEST] 00000013 config        I   Mojarra 2.1.27 ( 20140108-1632 https://svn.java.net/svn/mojarra~svn/tags/2.1.27@12764) für Kontext '/cld' wird initialisiert.
[13.07.17 16:56:17:739 CEST] 00000013 application   I   JSF1048: PostConstruct/PreDestroy-Annotationen vorhanden.  Verwaltete Bean-Methoden, die mit diesen Annotationen markiert sind, lassen die entsprechenden Annotationen verarbeiten.
[13.07.17 16:56:19:334 CEST] 00000013 config        W   JSF1067: Ressource /WEB-INF/common-ui.taglib.xml, die von der Konfigurationsoption javax.faces.CONFIG_FILES angegeben wird, kann nicht gefunden werden.  Die Ressource wird ignoriert.
[13.07.17 16:56:19:336 CEST] 00000013 config        W   JSF1067: Ressource /WEB-INF/common-functions.taglib.xml, die von der Konfigurationsoption javax.faces.CONFIG_FILES angegeben wird, kann nicht gefunden werden.  Die Ressource wird ignoriert.
[13.07.17 16:56:19:475 CEST] 00000013 PostConstruct I   Running on PrimeFaces 6.0.15
[13.07.17 16:56:19:478 CEST] 00000013 VersionLogger I   Using OmniFaces version 1.8.3
[13.07.17 16:56:19:497 CEST] 00000013 lifecycle     I   JSF1027: [null Die ELResolvers für JSF wurden nicht im JSP-Container registriert.
.
.
.

条目

Ignoring class [org.omnifaces.facesviews.FacesViewsInitializer] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.ServletContainerInitializer 
Ignoring class [org.omnifaces.component.output.cache.CacheInitializerListener] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.FilterRegistration 
Ignoring class [org.omnifaces.util.Platform] because it could not be loaded: java.lang.NoClassDefFoundError: javax.servlet.ServletRegistration 

似乎是最有趣的。

但他们究竟是什么意思?

当然,它有点清楚,servlet.api类缺失。

<dependency org="javax.servlet" name="servlet-api" rev="2.5" />

(抱歉这是Ant / Ivy语法)

然而,在部署时向部署中添加javax-servlet-api-2.5.jar会完全破坏应用程序,并说&#34; FacesServlet不是Servlet类&#34;:

在HTML中:

Original Exception:
Error message: javax.servlet.UnavailableException: SRVE0201E: Servlet [javax.faces.webapp.FacesServlet] is not a servlet class.
Error code: 404
Target servlet: Faces Servlet
Stacktrace:
javax.servlet.UnavailableException: SRVE0201E: Servlet [javax.faces.webapp.FacesServlet] is not a servlet class.
     at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:535)
     at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:503)
     at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)
     at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3954)
     at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
     at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:942)
     at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
     at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:453)
     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:515)
     at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:306)
     at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
     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:1646)

我必须尽可能地翻译错误消息。 WebSphere 7 BTW是一个Servlet 2.5容器,因此问题可能与classloader相关......?

大问题:

为什么此设置无法通过@EJB在基本可用的场景(没有javax.servlet依赖项)上注入EJB? bean和bean管理器似乎存在,那么为什么它不能通过@EJB注入?

你怎么可能治愈这个?它可以完成吗?

2 个答案:

答案 0 :(得分:0)

您是否在应用程序中嵌入了自己的JSF(Myfaces)和CDI(OpenWebBeans)实现? 如果您这样做,请阅读有关如何配置WAS以使用您自己的JSF实现的文档。您无法在WAS中覆盖/使用CDI实现 你也可以用CDI方式(@Inject)而不是EJB方式注入EJB ..(@EJB)?

答案 1 :(得分:0)

WAS-7实现了JavaEE 5标准。这还不包括CDI。这意味着EJB和CDI之间也没有集成。

您可以做的是编写一个使用ProcessAnnotatedType的CDI扩展,并且为每个找到的@EJB创建一个自定义编程Bean的所有CDI类。 这个自定义Bean只是通过JNDI查找EJB,而不是它。

如果您只需要这几次,那么您也可以使用@PostConstruct方法直接从类中的JNDI上下文中获取EJB。