J2EE设计模式-如何使应用程序独立于后端服务
我正在尝试开发一些 RESTful Web 服务,这些服务必须访问某些后端系统来获取数据。这些后端源可以是以下任何一种:
通过 JDBC 的数据库
ERP,通过 SOAP 通信
另一个 J2EE 应用程序,通过SOAP 或者可能是 REST。
在我的级别上,我能做些什么才能在我的业务层和 DAO 之间实现松散耦合?有一些设计模式可以帮助我吗?
非常感谢您的帮助!
I am trying to develop some RESTful web services which have to access some backend system for data. These backend sources can be any one of the folowing :
Database through JDBC
ERP, communicates through SOAP
Another J2EE application, communicating through SOAP or maybe REST.
What best can I do at my level to have a loose coupling between my business layer and the DAO ? IS there some design pattern which may help me ?
Thanks a lot for the help !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
希望这有帮助:
对系统进行分层:正如您在答案中提到的,始终拆分 bz逻辑来自数据访问逻辑。我建议在不同的二进制模块中实现不同的层,这将使您能够灵活地在不同的层中部署不同的层(如果您确实需要的话)。
存储库模式:业务层对如何通过某些特定方式执行数据访问操作进行抽象定义bz 逻辑中的实体。存储库中的方法始终使用 bz 逻辑中定义的实体作为参数(存储库始终使用 bz 语言“交谈”)。在 bz 层中定义存储库接口后,您可以在单独的层中实现它们:数据访问层。
依赖注入:在应用程序中层(GUI、应用程序服务器等),在初始化逻辑(引导)中,您可以使用依赖项容器将存储库实现(在数据访问层中定义的实现)注入 bz 逻辑中。有很多带有依赖注入容器的开源框架。
通过这 3 种模式,您可以将 DA 逻辑与 bz 逻辑分开。
请注意,这是一个非常简短的答案,我建议您花一些时间阅读以下书籍:
谢谢,
Juanjo
Hope this helps:
Layer your system: as you mention in your answer, always split bz logic from the data access logic. I would recommend to implement different layers in different binary modules, this will give you the flexibility to deploy different layers in different tiers (if you really need it).
Repository pattern: the business layer has abstract definitions of how to perform data access operations with certain entities in your bz logic. The methods in the repositories always use entities defined in your bz logic as parameters (the repositories always "talk" in the the bz language ). Once the repository interfaces are defined in the bz layer, you implement them in a separate layer: the data access layer.
Dependency injection: in the application layer (GUI, application server, ...), in the initialization logic (bootstrapping) you can use a dependency container to inject the repository implementations (the ones defined in the data access layer) in your bz logic. There a lot of open source frameworks with dependency injection containers.
With these 3 patterns, you can separate the DA logic from your bz logic.
Note that this is a very short answer, I would recomend you to take some time and read the following books:
Thanks,
Juanjo
我希望我正确理解了你的问题。
我目前正在开发一个使用 JAX-WS RESTful Web 服务来访问数据的应用程序。数据可以来自各种不同的来源(在我们的例子中是两个不同的数据库和一个 Lucene 索引)。
我认为,在不了解您的具体情况的情况下,我只能推荐我们使用的典型方法:
虽然这是一本老书,但大部分信息都在 专家一对一 J2EE 中设计和开发。但我想说这些是标准的 Enterprise Java 最佳实践。
I hope I've understood your question correctly.
I'm working on an application at the moment which uses JAX-WS RESTful web services to access data. The data can come from a variety of different sources (two different databases and a Lucene index in our case).
I think, without knowing the specifics of your situation, I can only recommend the typical approach we use:
Although it's an old book, most of this info is in Expert One-on-One J2EE Design and Development. But I'd say these are standard Enterprise Java best practices.