spring 什么是spring服务注解?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47668871/
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
What is a spring service annotation?
提问by webpersistence
I understand that @Serviceis used to mark a class as a business logic class. Why is it useful for spring? I can already use @Componentto mark a class as a bean. I understand that @Serviceis more specific than @Component, but how?
我知道它@Service用于将类标记为业务逻辑类。为什么它对春天有用?我已经可以@Component用来将类标记为 bean。我知道这@Service比 更具体@Component,但是如何?
回答by Dhaval Simaria
Consider @Componentannotation as a Swiss Knife. It can act as cutting knife, as an opener, as a scissor, etc.
将@Component注释视为一把瑞士刀。它可以充当切刀、开瓶器、剪刀等。
Similarly, your Component can act as a Repository, as Business Logic class or as a controller.
同样,您的组件可以充当存储库、业务逻辑类或控制器。
Now, @Serviceis a just one of the versions of @Component, say a knife.
现在,@Service只是一个版本@Component,比如说一把刀。
Spring process @Servicesimilar to @Component, since @Serviceinterface itself is annotated with @Component.
Spring 过程@Service类似@Component,因为@Service接口本身是用@Component.
From Spring docs.:
来自 Spring 文档:
@Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Component public @interface ServiceIndicates that an annotated class is a "Service" (e.g. a business service facade).
@Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Component public @interface Service表示带注释的类是“服务”(例如,业务服务外观)。
Why to differentiate both of them?
为什么要区分两者?
To apply the basic rule of programming: Your code should be easily readable.
应用编程的基本规则:您的代码应该易于阅读。
Yes, you can use @Componentannotation everywhere and it will work fine but for the better understanding of the code, it is preferred to use the respective special types of annotations like @Servicein our case.
是的,你可以@Component在任何地方使用注解,它会工作得很好,但为了更好地理解代码,最好使用各自的特殊类型的注解,就像@Service我们的例子一样。
The other benefit is ease of debugging. Once you know the error, you need not hope from one component class to another, checking it time whether that class is service, repository or controller.
另一个好处是易于调试。一旦你知道错误,你就不需要希望从一个组件类到另一个组件类,检查它是服务、存储库还是控制器的时间。
回答by Sankar
@Service, @Controller, @Repository = {@Component + some more special functionality}
@Service, @Controller, @Repository = {@Component + 一些更特殊的功能}
Click the below link for more details
点击以下链接了解更多详情
What's the difference between @Component, @Repository & @Service annotations in Spring?
Spring 中的 @Component、@Repository 和 @Service 注解有什么区别?
The @Component annotation marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. The @Service annotation is also a specialization of the component annotation. It doesn't currently provide any additional behavior over the @Component annotation, but it's a good idea to use @Service over @Component in service-layer classes because it specifies intent better. Additionally, tool support and additional behavior might rely on it in the future.
@Component 注解将一个 java 类标记为一个 bean,因此 spring 的组件扫描机制可以将它拾取并拉入应用程序上下文中。@Service 注解也是组件注解的特化。它目前没有提供任何额外的行为而不是 @Component 注解,但是在服务层类中使用 @Service 而不是 @Component 是个好主意,因为它可以更好地指定意图。此外,工具支持和其他行为将来可能会依赖于它。
回答by Ramesh Fadatare
The @Service annotation is a specialization of the component annotation. It doesn't currently provide any additional behavior over the @Component annotation, but it's a good idea to use @Service over @Component in service-layer classes because it specifies intent better. Additionally, tool support and additional behavior might rely on it in the future.
@Service 注解是组件注解的特化。它目前没有提供任何额外的行为而不是 @Component 注解,但是在服务层类中使用 @Service 而不是 @Component 是个好主意,因为它可以更好地指定意图。此外,工具支持和其他行为将来可能会依赖于它。


