是否可以在一个 EJB 3 bean 中一起使用 @WebService、@Stateless 和 @Singleton? [英] Is it possible to use @WebService, @Stateless and @Singleton altogether in one EJB 3 bean?

查看:25
本文介绍了是否可以在一个 EJB 3 bean 中一起使用 @WebService、@Stateless 和 @Singleton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 EJB 3 和 JBoss AS 6.0.0.Final.我有一个带有注释 @Stateless@WebService 的无状态会话 bean.当我添加注释 @Singleton 时,部署出错,显示消息:

I'm using EJB 3 and JBoss AS 6.0.0.Final. I have a stateless session bean with the annotations @Stateless and @WebService. When I add the annotation @Singleton, the deployment is in error showing the message:

...name=ServiceBean, service=ejb3 已经安装

...name=ServiceBean, service=ejb3 is already installed

如何避免部署错误?

推荐答案

您可以在同一个 bean 中使用 @WebService 和 @Stateless 或 @WebService 和 @Singleton,如果您想将 POJO 公开为 Web服务和 EJB.

You can use @WebService and @Stateless or @WebService and @Singleton in the same bean which makes perfectly sense if you want to expose a POJO as both a web service and EJB.

在同一个 bean 中使用 @Stateless 和 @Singleton 没有多大意义.当您使用@Singleton 时,您正在创建一个与@Stateless 完全一样的具有所有EJB 功能(事务管理、安全性等)的EJB.唯一的区别是容器如何管理 EJB 生命周期:

Don't see much sense in using @Stateless and @Singleton in the same bean. When you use @Singleton, you are creating an EJB with all the EJB capabilities (transaction management, security, etc) exactly as with @Stateless. The only difference is how the container manages the EJB lifecycle:

  • @Stateless:在第一个请求之后立即创建 EJB 实例,当请求结束时,EJB 被池化并准备好在另一个请求到来时重用.但是,如果所有被池化的实例正在使用同一 bean 的另一个请求时,容器创建相同的新实例来为该新请求提供服务.
  • @Singleton:EJB 实例是在第一个请求(默认情况下 - 请参阅@Startup 以覆盖此行为)传入后创建的,这将是容器创建的唯一实例.如果另一个请求想要使用同一个 EJB,容器将永远不会创建它的新实例 - 将使用先前创建的实例.它就像一个池大小为 1 的 @Stateless EJB :) 使用这些时,并发性等方面很重要,但这可能超出了本文的范围.
  • @Stateless: the EJB instance is created immediately after the first request and when the request ends, the EJB is pooled and ready for reuse if another request comes in. However, if all the pooled instances are being used in the moment another request comes in for the same bean, the container creates a new instance of the same to serve that new request.
  • @Singleton: the EJB instance is created after the first request (by default - see @Startup to override this behavior) comes in and that will be the only instance created by the container. If another request wants to use the same EJB, the container will never create a new instance of it - the instance previously created will be used. It is like a @Stateless EJB with a pool size of 1 :) Aspects like concurrency are important when using these, but this is probably out of the scope of this post.

这篇关于是否可以在一个 EJB 3 bean 中一起使用 @WebService、@Stateless 和 @Singleton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆