如何将EJB注入抽象CDI类?

问题描述:

我将EJB注入到抽象类中是一个问题,该抽象类是我的JSF CDI bean的父类。在我的项目中,我正在使用MyFaces CODI 1.0.5(ViewScope),Omnifaces 1.3,PrimeFaces 3.4.2和GlassFish 3.1.2。

I have a problem with injecting an EJB into an abstract class which is the parent of my JSF CDI beans. In my project I am using MyFaces CODI 1.0.5 (ViewScope), Omnifaces 1.3, PrimeFaces 3.4.2 and GlassFish 3.1.2.

应用程序是EAR,抽象类在EJB模块中,而JSF CDI bean在WAR模块中:

The application is an EAR, the abstract class is in an EJB module and the JSF CDI beans are in a WAR module:

webframework-demo.ear
|__ webframework-war.war -> concrete JSF CDI bean
|__ webframework-ejb-lib.jar -> abstract class with EJB injection
|__ lib\
    |__ shared libs

我的抽象类:

public abstract class AbstractListPageAction<T extends AbstractViewBean<K>, K extends Serializable> {

    ...

    @EJB
    private CriteriaFacadeLocal<K> facade;

    @PostConstruct
    public void create() {
        ...

        facade.setEntityClass(getEntityClass());
        ...
    }

    ...

    public abstract Class<K> getEntityClass();

}

我的CDI bean:

My CDI bean:

import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped;

@Named
@ViewAccessScoped
public class UserListAction extends AbstractListPageAction<UserViewBean, UserEntity>
        implements Serializable {

    private static final long serialVersionUID = -1178878323047991855L;

    ...

    @Override
    public Class<UserEntity> getEntityClass() {
        return UserEntity.class;
    }

    ...
}

当我部署应用程序并访问JSF页面时,将创建UserListAction,但不会注入CriteriaFacadeLocal,并且在@PostConstruct方法中以NullPointerException结尾。

When I deploy the application and access a JSF page, UserListAction is created but CriteriaFacadeLocal is not injected and I end with a NullPointerException in the @PostConstruct method.

当我更改UserListAction时并添加一个空的@PostConstruct方法,然后注入CriteriaFacade,一切正常:

When I change UserListAction and add an empty @PostConstruct method then CriteriaFacade is injected and everything works fine:

@Named
@ViewAccessScoped
public class UserListAction extends AbstractListPageAction<UserViewBean, UserEntity>
        implements Serializable {

    private static final long serialVersionUID = -1178878323047991855L;

    ...

    @PostConstruct
    public void init() {
    }

    @Override
    public Class<UserEntity> getEntityClass() {
        return UserEntity.class;
    }

    ...
}

我在每个模块中都有beans.xml。但是,为什么我的CDI bean中必须有一个空的@PostConstruct方法?

I have beans.xml in every module. But why must I have an empty @PostConstruct method in my CDI bean? Is there a problem with an abstract class placed in a EJB module?

我是根据编辑后的问题创建的答案:

我认为EAR / EAR模块类加载器可能存在问题。我将webframework-ejb-lib.jar移到了webframework-war.war WEB-INF / lib文件夹中:

I figured that it was probably a problem with the EAR/EAR modules classloaders. I moved webframework-ejb-lib.jar into the webframework-war.war WEB-INF/lib folder:

webframework-demo.ear
|__ webframework-war.war -> concrete JSF CDI bean
|   |__ WEB-INF
|       |__lib
|          |__ webframework-ejb-lib.jar -> abstract class with EJB injection
|__ ... (other ejb modules)
|__ lib\
    |__ shared libs

突然一切都好了。