1

我在使用无状态 EJB 时遇到了一个问题。我希望在那个 EJB 中应该使用一个特定的静态方法,但是这个方法非常重要并且它具有静态依赖性。

正如我们所知,无状态会话 bean 的实例是根据需求(一个或多个)创建的。那么我如何确定所有的 EJB 都在使用该静态方法的一个副本。我不确定,但我认为每个使用静态方法的不同类都会加载不同的类副本,然后执行静态方法的不同副本。

而且我不能依赖单例 EJB,因为它不能保证只保留一份副本,因为如果服务器需要多个 JVM。不同的单例 EJB 副本将存在于不同的 JVM 中。

提前致谢。

4

1 回答 1

1

Static methods are one per class, even if you create thousands of instance of that class all of them will see just one copy of your static method.

Now as per Spec you should not have static methods in your EJB, you should consider moving this as part of utility if you want it static, or else make it non static.

From the Spec:

EE.5.2.3 Annotations and Injection

As described in the following sections, a field or method of certain container-managed component classes may be annotated to request that an entry from the application component’s environment be injected into the class. Any of the types of resources described in this chapter may be injected. Injection may also be requested using entries in the deployment descriptor corresponding to each of these resource types. The field or method may have any access qualifier (public, private, etc.). For all classes except application client main classes, the fields or methods must not be static.

于 2012-05-02T03:16:09.503 回答