有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java等价于局部变量的@EJB注释?

Hibernate中的EJB。有什么等效于依赖注入吗

@EJB(mappedName = FoobarServiceInterface.JNDI_NAME_LOCAL)
private FoobarServiceInterface foo;

foo是局部变量时

我设法对类成员使用@EJB注释,或者

FoobarServiceInterface bar = (FoobarServiceInterface)InitialContext.doLookup(FoobarServiceInterface.JNDI_NAME_LOCAL);

bar是局部变量,则执行查找

然而,我想知道是否有任何注释可以用于局部变量,比如类成员变量的@EJB


共 (2) 个答案

  1. # 1 楼答案

    使用它作为局部变量有什么好处
    EJB可以由许多请求/线程共享,并且查找不是一个自由进程,因此通常建议将其用作类成员而不是局部变量

  2. # 2 楼答案

    不,没有这样的注释。根据ejb3。1规格:

    4.3.2 Dependency Injection ... If a session bean makes use of dependency injection, the container injects these references after the bean instance is created, and before any business methods are invoked on the bean instance...

    因此,容器用于注入依赖项(@EJB、@Resources、@inject)的注释仅适用于某些Targets。e、 g.:@Target(值={TYPE,方法字段