Spring:如何在不同应用程序中共享服务
我们目前正在开发一个丰富的应用程序,在客户端使用 Eclipse,在服务器端使用 Spring Framework。
我的挑战是通过在不同的应用程序上共享相同的业务功能(服务)来减少实施工作。这些业务功能(例如,文档管理上下文中的签入/签出服务)可以具有不同的特征(例如,签入本身是相同的,但在一个应用程序中,文件在签入后被删除,另一应用程序将不执行任何操作)。 在这种情况下,我可以复制 &粘贴所有代码并使其适应新的要求,否则我会找到另一个不错的解决方案(例如通过回调重用代码等)以减少定位和维护工作。
您有类似场景的经验吗? Spring 是否为一个平台上的这种多应用程序方法提供了任何解决方案?
we're currently developing a rich application using Eclipse on client side and Spring Framework on serverside.
My challenge is to reduce implementation efforts by sharing the same business functions (services) over different applications. These business functions (e.g. a checkin/checkout service in document management context) can have different characteristics (e.g. checkin itself is equal, but in one application the file gets deleted after checkin, the other application will do nothing).
In this case I could copy & paste all code an adapt it to the new requirements, or i will find another nice solution (e.g. code reusing with callbacks, etc.) to reduce loc and maintenance efforts.
Do you have any experience in such scenarios? Does Spring provide any solutions for such a multiple application approach on one platform?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,事实上Spring(一般来说是IoC)就是为这样的解决方案而设计的。首先是技术步骤:将要共享的服务提取到一个库中,您可以将其拖放到不同的项目中。
现在有趣的部分是:如果某些逻辑部分不同而其他逻辑部分保持不变,请使用依赖注入和策略模式!
现在创建
CheckInCleanup
接口的两个独立实现,对于每个应用程序都是不同的。一种是删除文件,一种是不执行任何操作。在一个应用中,前者将被注入,后者将被注入第二个应用中。CheckInCleanup
是包括CheckInService
在内的库的一部分,但实现是本地。 Spring 将在启动时获取它并自动注入。此外,您刚刚引入了良好的关注点分离和增强的可测试性。
Of course, in fact Spring (IoC in general) is made for such solutions. First the technical step: extract the services that are suppose to be shared into a library that you can drop to different projects.
Now the fun part: if some pieces of logic are different while others remain the same, use dependency injection and strategy pattern!
And now create two independent implementations of
CheckInCleanup
interface, distinct to every application. One deleting the file and one doing nothing. In one application the former will be injected and the latter in the second one.CheckInCleanup
is part of the library includingCheckInService
, but the implementation is local. Spring will pick it up on startup and inject it automatically.Besides you have just introduced nice separation of concerns and increased testability.