Is it safe to inject an EJB into a servlet as an instance variable?

前端 未结 4 1256

We all know that in the web tier there is the possibility that only a single instance of a given Servlet exists which services multiple requests. This can lead to threading issu

4条回答
  •  遇见更好的自我
    2021-02-07 22:07

    Your reference "Don't inject to servlets" mentions nothing about ejbs or @ejb annotation. It talks about not thread safe objects such as PersistenceContext.

    Per EJB spec you can access ejbs from variety of remote clients including servlets (EJB 3.0 Specification (JSR-220) - Section 3.1). Injecting ejb using @EJB annotation is a method of obtaining EJB interface via dependency injection (section 3.4.1) which is alternative to looking up ejb objects in the JNDI namespace. So there is nothing special about @EJB annotation with respect to EJBs obtained.

    So, based on EJB 3.0 Spec, it's a standard practice to obtain ejbs from servlets using @EJB annotation.

提交回复
热议问题