spring - Spring Boot应用中service层有什么用?

标签 spring spring-boot model-view-controller

我是 Spring Boot 的新手,我正在创建一个没有 UI 的 RESTful API。

我在想是否应该使用业务服务并从那里调用存储库,还是直接从我的 REST Controller 调用存储库?

最佳答案

服务层不是 Spring Boot 独有的概念。它是一个软件架构术语,经常被称为模式。简单的应用程序可以跳过服务层。实际上,没有什么能阻止您从 Controller 层调用存储库方法。

但是,我强烈建议使用服务层,因为它主要用于定义应用程序边界。服务层职责包括(但不限于):

  • 封装业务逻辑实现;
  • 集中数据访问;
  • 定义事务开始/结束的位置。

  • 引用 Service Layer pattern来自 Martin Fowler 的 Catalog of Patterns of Enterprise Application Architecture :

    A Service Layer defines an application's boundary and its set of available operations from the perspective of interfacing client layers. It encapsulates the application's business logic, controlling transactions and coor-dinating responses in the implementation of its operations.

    关于spring - Spring Boot应用中service层有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58234187/

    相关文章:

    SpringMVC : Could not instantiate property type [java. lang.Double]自动增长嵌套属性路径

    spring - 如何使用 Spring Boot 和 @FeignClient 发送 Bearer 授权 token

    spring-boot - Spring boot 多启动器

    java - 如何在 Android/Java 中保存数据对象?

    java - Spring 单元测试

    spring - 不在 Spring boot、Thymeleaf 和 A​​ngularJs 应用程序中加载静态资源

    java - Spring MVC 测试

    java - 使用 MVC Java 创建简单的示例

    ruby-on-rails - 正如 Ruby Guides 要求我考虑的那样,我真的应该使用观察者吗?

    java - Spring应用程序上下文无法加载配置文件