从普通类访问EJB

时间:2016-04-11 14:13:24

标签: java-ee ejb

我正在使用Facade模式来访问数据库实体。我编写了一个包装器来访问Facade EJB,如下所示。正如我从异常中理解的那样,似乎EJB尚未初始化。在阅读论坛上的异常后,我明白它应该解决@PostConstruct符号,但仍然没有帮助。可能是我错了,任何指针都会非常感激

public class PatientSearchHelper {
    @EJB
    private PatientFacade patientFacade;

    private final Patient patient;
    private ResponseHeader respHeader;
    private SearchResponse searchResponse;
    private List<Patient> resultSet;


    public PatientSearchHelper (Patient patient) {
        this.patient = patient;
    }

    @PostConstruct
    public void initialize() {
        this.respHeader = new ResponseHeader();
        this.searchResponse = new SearchResponse();
    }

    public SearchResponse getById() {

        System.out.println("Patient Id: " + patient.getPatientid());
        //patientFacade = (PatientFacade) new InitialContext().lookup("java:global/Aarogayam2/PatientFacade!common.facades.PatientFacade");
        resultSet = patientFacade.findById(patient.getPatientid());


        if (resultSet.size() > 0) {
           formatFoundResponse();
        } else {
            formatNotFoundResponse();
        }
        return searchResponse;
    }

    private void formatFoundResponse() {
        searchResponse.setPayload(resultSet);
        respHeader.setSuccess(true);
        searchResponse.setHeader(respHeader);
    }

    private void formatNotFoundResponse() {
        respHeader.setSuccess(false);
        respHeader.setMessage("No Patient found");
        searchResponse.setHeader(respHeader);
        searchResponse.setPayload(null);
    }
}

然而,我在下面调用它时会得到异常

PatientSearchHelper searchHelper = new PatientSearchHelper(patient);
searchHelper.initialize();
return searchHelper.getById();

异常

SEVERE:   java.lang.NullPointerException
    at common.helpers.PatientSearchHelper.getById(PatientSearchHelper.java:48)
    at common.services.PatientService.getById(PatientService.java:57)
    at common.services.PatientService$Proxy$_$$_WeldSubclass.getById(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

3 个答案:

答案 0 :(得分:2)

如果要创建容器来创建EJB实例,则需要使用JNDI或@EJB注释进行访问。为了使上述代码有效,使PatientSearchHelper类成为EJB并在客户端代码中使用@EJB来获取实例,然后才能访问任何方法。

答案 1 :(得分:0)

您可以使用@Named注释来使用CDI bean,而不是使新的无状态EJB包装另一个(如果您的方法不需要事务支持),您可以使用@Named public class PatientSearchHelper { @EJB private PatientFacade patientFacade; private final Patient patient; private ResponseHeader respHeader; private SearchResponse searchResponse; private List<Patient> resultSet; ... 注释来使用CDI bean。

{{1}}

答案 2 :(得分:0)

另一种方法是使用以下通用代码在Beanmanger中查找bean,如果找到则只返回所需类的实例:

private static <T> T lookUpClassInBeanManager(Class<T> clazz) {
    BeanManager bm = CDI.current().getBeanManager();
    Bean<T> bean = (Bean<T>) bm.getBeans(clazz).iterator().next();
    CreationalContext<T> ctx = bm.createCreationalContext(bean);
    return (T) bm.getReference(bean, clazz, ctx);
}

public static PatientFacade lookUpPatientFacade() {
        return lookUpClassInBeanManager(PatientFacade.class);
}

这样,您始终可以获得EJB类的现有实例。这在@FacesConverter或任何其他无法声明为@Stateless的类中派上用场。