在java中开发服务层的最佳方法

问题描述:

我想使用java为我的应用程序开发服务层。同时服务层也可以暴露给webservice。

I wants to develop service layer for my application using java. At the same time the service layer can also be exposed to webservice also.

我的想法是为数据库操作创建一个通用抽象类,所有其他服务类扩展抽象类,并通过抽象类方法进行数据库操作。

My idea is to create one Generic Abstract Class for database operations , and all other service classes extend that abstract class and do the DB operation through that abstract class methods.

这是一个好主意吗?
请帮助我

Is this a good idea to do it? Please help me

很难说这么少的细节,甚至不知道你会用什么访问数据库(JDBC?JPA?Hibernate?)。但

It's hard to say with so few details, and without even knowing what you'll use to access the database (JDBC? JPA? Hibernate?). But


  • 服务层和持久层不是一回事。为了简化解耦和可测试性,我更喜欢拥有纯服务层和数据访问层

  • 继承通常不是重用代码的最佳方式。使用设计良好的API,而不是委托而不是继承。

另外,不要重新发明轮子。 EJB3,Spring和其他框架可以很好地支持开发服务并将它们作为Web服务公开。

Also, don't reinvent the wheel. EJB3, Spring and other frameworks have good support to develop services and expose them as web services.