在EJB方法/ @RequestScoped和@Stateless之间传递状态 [英] Passing state between EJB methods / @RequestScoped and @Stateless

查看:212
本文介绍了在EJB方法/ @RequestScoped和@Stateless之间传递状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个@RequestScoped CDI bean,我想变成一个EJB来获得声明性的事务。 (我在EJB 3.1,Java EE 6)



目前,我在子例程之间传递状态,假设实例仅用于单个请求。如果我现在添加 @Stateless ,那么假设会改变。



例如,我想做一些类似



  @Stateless 
@Named
@RequestScoped
public class Foo {
private String VAR1; //无法在@Stateless中使用实例vars?
private String var2;

public void transactionForRequest(){
var1 = value;
var2 = value;
....
子例程();
}
}

我认为上述不行 - 那是正确?



我正在考虑两种选择:




  • 使用@Stateful而不是@Stateless ,以及@Named和@RequestScoped。

  • 保持@Stateless,并使用 EJBContext.getContextData 映射替换实例变量。



哪个更好?还有另外一些我不想的呢? (除了等待Java EE 7或切换到Spring。: - ))

解决方案

虽然 @Stateless @Singleton @MessageDriven 可以通过 @Inject ,他们不能 @RequestScoped 或任何其他范围。只有 @Stateful 模型是足够灵活的支持范围。换句话说,您可以将 @Stateful bean类本身注释为 @RequestScoped @ SessionScoped 等。



简单来说, @Stateless @Singleton 已经有固定的范围。 @Singleton 本质上是 @ApplicationScoped @Stateless 作为一些构成范围,如 @InvocationScoped (如果存在)。 @MessageDriven bean的生命周期完全取决于驱动它的连接器,因此也不允许有用户定义的范围。



另请参见 https://stackoverflow.com/a/8720148/190816


I have a @RequestScoped CDI bean that I want to turn into an EJB to get declarative transactions. (I'm on EJB 3.1, Java EE 6)

Currently, I am passing state between subroutines, under the assumption that the instance is only used in a single request. If I add @Stateless now that assumption would change.

For example, I want to do something like

@Stateless
@Named
@RequestScoped
public class Foo {
  private String var1; // can't use instance vars in @Stateless?
  private String var2;

  public void transactionForRequest() {
    var1 = value; 
    var2 = value;
    ....
    subroutine();
  }
}

I assume the above doesn't work- is that correct?

I am contemplating two alternatives:

  • Use @Stateful instead of @Stateless, along with @Named and @RequestScoped.
  • Keep @Stateless and use EJBContext.getContextData map to replace instance variables.

Which is better? And is there some other alternative I'm not thinking of? (Besides wait for Java EE 7 or switch to Spring. :-))

解决方案

While @Stateless, @Singleton and @MessageDriven can have scoped references injected via @Inject, they cannot be @RequestScoped or any other scope. Only the @Stateful model is flexible enough to support scopes. In other words, you can annotate the @Stateful bean class itself as @RequestScoped, @SessionScoped, etc..

In simple terms @Stateless, @Singleton have fixed "scopes" already. @Singleton is essentially @ApplicationScoped and @Stateless would perhaps be some made-up scope like @InvocationScoped, if that existed. The lifecycle of an @MessageDriven bean is entirely up to the Connector that drives it and is therefore also not allowed to have user-defined scope.

See also https://stackoverflow.com/a/8720148/190816

这篇关于在EJB方法/ @RequestScoped和@Stateless之间传递状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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