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