Spring 注解@Repository 和@Service

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6256331/
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-09-08 04:32:01  来源:igfitidea点击:

Spring annotations @Repository and @Service

spring

提问by BlueSky

What are the advantages of using @Repositoryand @Service?

使用@Repository@Service 有什么好处?

Don't tell me about component scanning etc., I am looking forward to something extra benefits or features that are there if at all.

不要告诉我有关组件扫描等的信息,我很期待有一些额外的好处或功能,如果有的话。

what happens if I don't use it? what is that I would be missing?

如果我不使用它会怎样?我会错过什么?

回答by Donal Fellows

There are a few reasons:

有几个原因:

  1. It's easier to target pointcuts with more specific annotations. Do notunderestimate the usefulness of this!
  2. The @Repositoryannotation carries with it some additional functionality: it causes exceptions to be wrapped up as DataAccessExceptions.
  3. The @Serviceannotation may gain additional semantics in the futurebut it's not happened yet…
  1. 使用更具体的注释更容易定位切入点。千万不能小看这个用处!
  2. @Repository注释与它携带了一些附加功能:它会导致异常被包裹起来作为DataAccessException小号
  3. @Service注释可能获得额外的语义在未来,但它没有发生过......

回答by Luciano Fiandesio

The @Repositoryannotation (introduced in Spring 2.0) and @Serviceannotation (introduced in Spring 2.5) are specialization of the @Componentannotation.

@Repository注释(在Spring 2.0引入)和@Service注释(在Spring 2.5引入)是专业化@Component注释。

The main advantage of using @Repositoryor @Serviceover @Componentis that it's easy to write an AOP pointcut that targets, for instance, all classes annotated with @Repository.

using @Repositoryor @Serviceover的主要优点@Component是很容易编写一个 AOP 切入点,例如,所有用@Repository.

Also, the specialized annotations help to clearly demarcate application layers (in a standard 3 tiers application).

此外,专门的注释有助于明确划分应用程序层(在标准的 3 层应用程序中)。

回答by nicholas.hauschild

I believe that there are two things Spring had in mind when they added these Component annotations.

我相信 Spring 在添加这些 Component 注解时考虑了两件事。

  1. They can be used to specify point cuts across all objects annotated with that sub-component. (All @Repository or all @Service pointcuts)
  2. I think Spring also mentioned that they would be adding some future functionality to them. (I have no citation for this, I just think I read this somewhere)
  1. 它们可用于在使用该子组件注释的所有对象上指定点切割。(所有@Repository 或所有@Service 切入点)
  2. 我认为 Spring 还提到他们将向他们添加一些未来的功能。(我没有引用这个,我只是想我在某处读过这个)

回答by lrkwz

If you TDDit would be useful to carefully design (an test of course) @Services because they will benefit of full injection of all the @Autowired components you have defined.

如果您使用TDD,仔细设计(当然是测试)@Services 会很有用,因为它们将受益于您定义的所有 @Autowired 组件的完全注入。

You could implement your functionalities directly into a @Controller for example, but in this case testing will be harder and you need to define all the mock elements (quite easier in springframework 3.1 but still "bad design").

例如,您可以将您的功能直接实现到 @Controller 中,但在这种情况下,测试将更加困难,您需要定义所有模拟元素(在 springframework 3.1 中相当容易,但仍然是“糟糕的设计”)。