我正在审查一些 Jave EE 6 代码并对其正确性表示怀疑:
@WebServlet
public class FooServlet {
@EJB
private transient BarRemoteInterface bar;
}
@Remote
public interface BarRemoteInterface {
}
@Stateless
public class BarBean implements BarRemoteInterface {
}
我想知道
transient
关键字是否没有任何意义,因为注入(inject)的代理不会被序列化。 (或者将会,但没有效果?)我认为如果我们不注入(inject)远程接口(interface)会更有效,因为它使用传递复制参数传递。相反,我们宁愿使用
@EJB BarBean
注入(inject) EJB,以便使用它自动生成的(?)本地接口(interface)。我说得对吗?在这种情况下,甚至可以将
@EJB
替换为@Inject
,因为这更通用
如果我的想法正确的话,你能评论一下吗?
最佳答案
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?)
不需要 transient 。 EJB 规范规定远程代理必须是可序列化的。如果需要,您可以让您的接口(interface)扩展 Serialized。
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。
希望对您有所帮助!
关于java - 通过远程接口(interface)将 EJB 注入(inject) servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15974533/