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 楼答案
不需要临时的。EJB规范规定远程代理必须是可序列化的。如果需要,可以让接口扩展为可序列化
是的,
@Remote
本质上是“按拷贝传递”(不是真的,但这已经足够接近了)。一些应用程序容器无论如何都可以对此进行优化。只有在实际远程处理时才应该使用@Remote
。你应该不惜一切代价避免远程通信。类似SOA的架构速度很慢,而且众所周知不可能扩展。只要坚持@Local
,除非你有一个非常好的理由不这么做是的,请随意
@Inject
您的EJB。你的JEE6容器(TomEE、JBoss7、GlassFish等)将能够解决这个问题。事实上,你应该问问自己,我真的需要EJB吗?你能用纯CDI完成你的任务吗?EJB只有在您必须担心事务语义时才有用,否则只需使用CDI即可我希望这有帮助