Stateless Session Beans Identity with @EJB and @Inject

做~自己de王妃 提交于 2019-12-12 09:07:05

问题


I have been looking into section 3.4.7.2 of the EJB3.2 specifications lately und done some tests.

The specifications:

@EJB Cart cart1;
@EJB Cart cart2;
… if (cart1.equals(cart1)) { // this test must return true ...}
… if (cart1.equals(cart2)) { // this test must also return true ...}

The equals method always returns true when used to compare references to the same business interface type of the same stateless session bean.

The specifications cite explicitly the @EJB Annotation so I did some tests and I could confirm - if (cart1.equals(cart2)) return always true - the identities assumption.

Because very often I see @Inject as meant to work the same as @EJB, I tried the same example above but with @Inject. In that case the if (cart1.equals(cart2)) return always false.

I was wondering whether there are some comments on that.

The code for test purposes:

public abstract class FormatOutputWithBeansIdentity extends HttpServlet {

    protected void formatOutput(final PrintWriter out, SLSBLocalView beanA, SLSBLocalView beanB) throws IllegalStateException {
        ...;
        out.println("<br>beanA and beanB are equal : " + checkIfEqual(beanA, beanB) + "<br>");
        out.println("<br>beanA and beanA are equal : " + checkIfEqual(beanA, beanA) + "<br>");
    }


    private Boolean checkIfEqual(SLSBLocalView beanA, SLSBLocalView beanB) {
        // The equals method always returns true when used to compare references to the same business interface type of the same stateless session bean.
        return beanA.equals(beanB);
    }
}

@WebServlet(name = "ServletDemo1", urlPatterns = {"/ServletDemo1"})
public class ServletDemo1 extends FormatOutputWithBeansIdentity {

    @EJB
    SLSBLocalView beanA;

    @EJB
    SLSBLocalView beanB;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try (PrintWriter out = response.getWriter()) {

            ...

            out.println("<h1>Test Session Object Identity Using @EJB</h1>");

            formatOutput(out, beanA, beanB);

            ...
        }
    }

}

来源:https://stackoverflow.com/questions/45460383/stateless-session-beans-identity-with-ejb-and-inject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!