有 Java 编程相关的问题?

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

java通过远程接口将EJB注入servlet

我正在查看一些Jave EE 6代码,对其正确性有疑问:

@WebServlet
public class FooServlet {
  @EJB
  private transient BarRemoteInterface bar;
}

@Remote
public interface BarRemoteInterface {
}

@Stateless
public class BarBean implements BarRemoteInterface {
}
  • 我想知道transient关键字是否真的没有任何意义,因为注入的代理不会被序列化。(或将是,但没有效果?)

  • 我认为,如果我们不注入远程接口,因为它使用通过复制参数传递,那么它会更有效。相反,我们宁愿用@EJB BarBean注入EJB,以便自动生成它(?)将使用本地接口。我说得对吗

  • 在这种情况下,甚至可以用@Inject替换@EJB,因为这更一般

如果我的想法是正确的,你能评论一下吗


共 (1) 个答案

  1. # 1 楼答案

    I am wondering if that is true that the transient keyword does not make any sense as the injected proxy will not be serialized. (Or will be, but without effect?)

    不需要临时的。EJB规范规定远程代理必须是可序列化的。如果需要,可以让接口扩展为可序列化

    I think that it would be more effective if we didn't inject the remote interface as that uses pass-by-copy parameter passing. Instead we'd rather inject the EJB with @EJB BarBean so that it's automagically generated (?) local interface will be used. Am I right?

    是的,@Remote本质上是“按拷贝传递”(不是真的,但这已经足够接近了)。一些应用程序容器无论如何都可以对此进行优化。只有在实际远程处理时才应该使用@Remote。你应该不惜一切代价避免远程通信。类似SOA的架构速度很慢,而且众所周知不可能扩展。只要坚持@Local,除非你有一个非常好的理由不这么做

    In this case even the @EJB could be replaced with @Inject as that is more general

    是的,请随意@Inject您的EJB。你的JEE6容器(TomEE、JBoss7、GlassFish等)将能够解决这个问题。事实上,你应该问问自己,我真的需要EJB吗?你能用纯CDI完成你的任务吗?EJB只有在您必须担心事务语义时才有用,否则只需使用CDI即可

    我希望这有帮助