每次在 servlet 上完成请求时,EJB 都会注入到 servlet 上吗?
我正在尝试复制 MVC 设计模式。这就是我所做的,我有一个处理所有请求的 servlet。其中,是注入的ejb。因此,在此控制器/Servlet 中使用 @EJB 注释声明了 7 到 15 个 ejb。
我只想问,例如,1 个用户是否调用主控制器/servlet。 ejb 是否仅在该调用时被注入,或者每次用户调用 servlet 时它们每次都会被注入吗?或者第一次创建 servlet 时注入 ejb?
I am trying to copy the MVC design pattern. And this is what I do, I have a servlet which handles all the requests. In it, are the injected ejbs. So in this controller/servlet is declared 7 to 15 ejbs with the @EJB annotations.
I just want to ask if for example, 1 user calls the maincontroller/servlet. do the ejbs get injected only on that call or everytime a user calls the servlet do they get injected everytime? or the ejbs are injected the first time the servlet is created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注入是在创建对象时完成的,并且由于 Servlet 容器通常(但不是强制性的 - 在单线程模型的情况下)创建 servlet 的单个实例,然后在单独的线程中服务请求,因此您的情况下的 EJB 应该在如果您没有使用 SingleThreadModel。
Injection is done when creating the object and since Servlet Container usually (but not mandatory - in case of single thread model) creates a single instance of a servlet and then serves the request in separate thread, the EJB in your case should be created once in case you are not using SingleThreadModel.
资源在对象创建时注入(因为您不能注入静态字段,而只能注入成员变量)。
对于 servlet,servlet 类由 servlet 容器加载,然后创建一个实例。此时,
@EJB或@Resource
被注入。然后容器调用 init() 方法Resources are injected at the time of object creation ( since you cannot inject into static fields, but just member variables ).
In the case of a servlet, the servlet class is loaded by the servlet container and then an instance is created. At this time, the
@EJB or @Resource
is injected. The container then calls the init() method