- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想写这段代码:
@Stateless
public class MyEjb
{
@EJB
private static MyOtherEjbWhichIWantStatic myOtherEjb;
}
出于各种原因,我想在我的类中注入(inject)一个 EJB 作为静态元素,这对我来说很有意义。
不幸的是,Java 对此不是很满意
com.sun.enterprise.container.common.spi.util.InjectionException: Illegal use of static field private static MyOtherEjbWhichIWantStatic myOtherEjb on class that only supports instance-based injection
我不明白,为什么我不能将静态 EJB 注入(inject)到另一个 EJB 中?
最佳答案
正如其他人所指出的,规范不允许这样做,简短的版本是 @EJB
注释仅支持具有 main()< 的类中的静态成员
函数(请参阅 EJB 3.0 规范和应用程序客户端容器)。
为什么会这样?首先,在 EJB 中完全禁止读/写静态字段(这是 EJB 限制的一部分)。来自 Why can't I use nonfinal static fields in my enterprise bean?
Nonfinal static class fields are disallowed in EJBs because such fields make an enterprise bean difficult or impossible to distribute. Static class fields are shared among all instances of a particular class, but only within a single Java Virtual Machine (JVM). Updating a static class field implies an intent to share the field's value among all instances of the class. But if a class is running in several JVMs simultaneously, only those instances running in the same JVM as the updating instance will have access to the new value. In other words, a nonfinal static class field will behave differently if running in a single JVM, than it will running in multiple JVMs. The EJB container reserves the option of distributing enterprise beans across multiple JVMs (running on the same server, or on any of a cluster of servers). Nonfinal static class fields are disallowed because enterprise bean instances will behave differently depending on whether or not they are distributed.
It is acceptable practice to use static class fields if those fields are marked as
final
. Since final fields cannot be updated, instances of the enterprise bean can be distributed by the container without concern for those fields' values becoming unsynchronized.
但是虽然允许使用只读静态字段,但这不适合 EJB。无状态 EJB 可能被合并,容器可能决定销毁它们(这是特定于实现的)并且您希望让容器选择您要使用的实例,尤其是在分布式环境中。换句话说,永远不要假设你被绑定(bind)到一个特定的实例。
所以最后,是的,这是胡说八道。
关于java - 注入(inject)静态EJB,废话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3195365/
我不是 OOP 大师,也许还有一些更深层次的东西我没有理解,但这是让我很困扰的: 我不明白为什么 Selenium WebDriver 的所有示例和教程都使用 WebDriver 对象,例如: Web
有一天,我在 Verilog 中学到了一个很酷的技巧。当你需要重复做某事时。您可以使用移位寄存器来计算递增的次数。只需将 1 从 LSB 移到 MSB,当它到达 MSB 时就完成了。 在 C 中它会是
有时 Agda 会给我一些无意义的“不在范围内”错误,让我不知道该怎么做。下面是一个例子: open import Data.Product open import Data.Bool open im
嗯,我是 Quartz 的新手,我正在关注 this tutorial 并且我配置了我的调度程序实例和 quartz 以使用此属性: properties["quartz.jobStore.lockH
我想写这段代码: @Stateless public class MyEjb { @EJB private static MyOtherEjbWhichIWantStatic myOt
“停止”Tomcat 和终止进程之间的区别(在 Tomcat 术语中)是什么? 如果 Tomcat 的 pid 为 500,那么它们之间有什么区别: kill -9 500 和 service tom
我对 Python 还很陌生 我正在尝试从其网站获取打印机计数器值。该网站是用 Javascript 编写的,我无法从中获取任何内容。 网站元素如下所示,我需要突出显示元素上的值。 inspect e
这个问题在这里已经有了答案: cmath header confusion (4 个答案) 关闭 9 年前。 我之前遇到过一个问题,因为函数在没有 std:: 的情况下被重载了。而且诅咒仍然不时发生
有没有办法在托管 C++ 中使用逐字字符串文字?类似于 C# 的 String Docs = @"c:\documents and settings\" 最佳答案 在 C++11 中,有原始字符串文字
我在 Mac OS X 10.6 上编译了 GCC 4.6.2,因为 Xcode 4.2 不附带 GCC。我还没有在 Xcode 中完全运行它,因为 Xcode 坚持将 -fconstant-cfst
我是一名优秀的程序员,十分优秀!