java spring 什么时候使用服务或组件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46272424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:08:25  来源:igfitidea点击:

When to use service or component in spring?

javaspringsystem-design

提问by ChiaChih wu

When to use service or component in spring?

spring 什么时候使用服务或组件?

For example, is a module responsible for sending email or common business logic a "service" or a "component"? and what's the difference?

例如,负责发送电子邮件或常见业务逻辑的模块是“服务”还是“组件”?有什么区别?

Is a service able to call the other services? Is there any transaction problem? or a service should call the components only?

一个服务可以调用其他服务吗?交易有问题吗?或者服务应该只调用组件?

Someone told me that a service should never call the other services and should only call the components instead, which means Controller->Service->Component->DAO, but I found many people share the concept of Controller->Service->DAO with no component.

有人告诉我,一个服务永远不应该调用其他服务,而应该只调用组件,这意味着 Controller->Service->Component->DAO,但我发现很多人分享 Controller->Service->DAO 的概念与没有组件。

Is there any system design criteria about this topic in Spring?

Spring 有没有关于这个话题的系统设计标准?

回答by Pelocho

In order to "configure" Spring so it can provide you with the instances of the classes you need, you are supposed to tell Spring whatobjects are involved and howthey are built. To do this you can use an xml configuration file or through annotations

为了“配置” Spring 以便它可以为您提供您需要的类的实例,您应该告诉 Spring涉及哪些对象以及它们是如何构建的。为此,您可以使用 xml 配置文件或通过注释

In case you take the annotation approach (IMHO a much better and simpler one) you can use @Componentto annotate the class. This is like telling Spring: "Hey! I want you to know that you may need an instance of this class. Maybe because I request it, maybe because something I requested needs it". So annotating a class with @Componentjust let Spring know that it exists

如果您采用注释方法(恕我直言,一种更好、更简单的方法),您可以@Component用来注释类。这就像告诉 Spring:“嘿!我想让你知道你可能需要这个类的一个实例。也许是因为我请求它,也许是因为我请求的东西需要它”。所以注释一个类@Component只让 Spring 知道它存在

There are other annotations that do the same:

还有其他注释可以做同样的事情:

  • @Controller(and @RestController)
  • @Service
  • @Repository
  • @Controller(和@RestController
  • @Service
  • @Repository

They all inform Spring that the class is involved in the DI context. Butthey also have semantic meaning:

它们都通知 Spring 该类涉及 DI 上下文。它们也有语义:

  • @Controller= @Componentbelonging to Presentation Layer
  • @Service= @Componentbelonging to Service/Use Case Layer
  • @Repository= @Componentbelonging to Persistence Layer
  • @Controller=@Component属于表示层
  • @Service=@Component属于服务/用例层
  • @Repository=@Component属于持久层

You can find more info in this question

您可以在此问题中找到更多信息

Should a service be able to call the other services?

服务是否应该能够调用其他服务?

I don't see any problem with that. If any of your services requires to do some actions that are already performed by other you surely want to avoid code duplication. As long as you respect the architecture layers dependency (never going up) you'll be fine.

我看不出有什么问题。如果您的任何服务需要执行其他服务已经执行的某些操作,您肯定希望避免代码重复。只要您尊重架构层的依赖性(永远不会上升),您就会没事。

About this you can check this articleabout Clean Architecture

关于这一点,您可以查看这篇关于 Clean Architecture 的文章

回答by Roman Danilov

@Componentis generic for other stereotypes.
So you can replace @Repository, @Service, @Controllerwith @Componentand nothing will change. But for better readability you should use @Repository, @Service, @Controller

@Component对于其他刻板印象是通用的。
所以你可以替换 @Repository, @Service, @Controller@Component,什么都不会改变。但为了更好的可读性,你应该使用@Repository, @Service, @Controller