简体   繁体   English

有状态会话Bean与无状态会话Bean,实例变量困境

[英]Stateful session beans vs Stateless session beans, Instance variable dilemma

I have a stateless session bean, but I want to add a reference to another bean in order to do some process. 我有一个无状态会话Bean,但是我想添加对另一个Bean的引用以便执行某些过程。 So if I add reference to another bean it shows up at instance level variable like this, 因此,如果我添加对另一个bean的引用,它将显示在这样的实例级别变量中,

@Stateless
public class AccountFacade extends AbstractFacade<Account> implements AccountFacadeRemote         {
    @EJB
    private BillerFacadeRemote billerFacade;

    public AccountFacade() {
    super(Account.class);
    }

    ... all other methods...
}

Question

Now, by definition stateless bean should not have any instance level variables. 现在,根据定义,无状态Bean不应该包含任何实例级变量。 So I am quite confuse about where to put this private BillerFacadeRemote billerFacade; 因此,我对于将这个private BillerFacadeRemote billerFacade;在哪里很困惑private BillerFacadeRemote billerFacade; ?

Your code is fine. 您的代码很好。

The @EJB annotation is injecting the bean into your class, and the application server manages its lifetime. @EJB批注将Bean注入您的类中,并且应用程序服务器管理其生存期。

I recommend reading, or skimming the pretty long Java EE tutorial . 我建议阅读或浏览很长的Java EE 教程

"The EJB container typically creates and maintains a pool of stateless session beans, beginning the stateless session bean's lifecycle. The container performs any dependency injection and then invokes the method annotated @PostConstruct, if it exists. The bean is now ready to have its business methods invoked by a client." “ EJB容器通常创建并维护一个无状态会话Bean池,开始无状态会话Bean的生命周期。该容器执行任何依赖项注入,然后调用注释为@PostConstruct的方法(如果存在)。该Bean现在准备开始其业务客户端调用的方法”。

It's okay for a stateless beans to have instance variables that represent dependencies. 对于无状态bean,可以使用表示依赖关系的实例变量是可以的。

In fact, this is even encouraged. 实际上,甚至鼓励这样做。 Without instance variables you could in many situations just use static method in a utility class instead. 没有实例变量,在许多情况下,您可以只在实用工具类中使用静态方法。

What however is NOT encouraged, is have instance variables representing client observeable state. 但是,不鼓励使用代表客户端可观察状态的实例变量。 That is wrong, but dependencies like the entity manager, jms queues, JDBC connections, and thus other services that your stateless service delegates (part of) its work to, is absolutely okay. 是错误的,但是诸如实体管理器,jms队列,JDBC连接之类的依赖关系以及无状态服务将其工作委托给其一部分的其他服务绝对是可以的。

Notice that the injections are true instance injections in instance variables. 注意,注入是实例变量中的真实实例注入。 It are not class level (static) injections. 不是类级别(静态)注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM