EJB's - when to use Remote and/or local interfaces?

前端 未结 4 1404
温柔的废话
温柔的废话 2020-12-02 03:30

I\'m very new to Java EE and I\'m trying to understand the concept of Local interfaces and Remote interfaces. I\'ve been told that one of the big advantages of Java EE is th

4条回答
  •  执念已碎
    2020-12-02 04:31

    While I agree with most of what is written above, I would like to refine the "how to start" ideas a bit.

    My suggestion to you is to never ever program directly to EJB interfaces within your code. Always use a regular, business-oriented interface, program to it (meaning, have your code call methods on the business-oriented interface) and provide the EJB "glue" code as a pluggable implementation. Your program should be focused on business logic, and not on implementation details such as EJB.

    That way, you can easily switch between remote and local implementations - and if you use an IoC container such as Spring, you can do it by means of configuration only.

    A special note about switching from local to remote: note that there are a few semantic differences between the two. For example, calling an EJB method via its "remote interface" results in arguments being passed by-value, while calling through the "local interface" results in arguments being passed by-reference. This is a major difference; so if you "start with local", make sure that you design your system in a way that it takes "remote" semantics into consideration as well.

    If your design relies on EJB methods changing passed-in objects, then it would be tricky for you to "switch to remote" later; perhaps even impossible.

    Good luck.

提交回复
热议问题