For spring auto detection, what's the difference between a component and a service?

I think that @Component and @Service can be used to automatically detect bean, can anyone show me the difference between these two annotations?

+7
source share
4 answers

The main difference between the two annotations is that @Service is a specialization of @Component .

See also spring documentation for @Service :

Indicates that the annotated class is a "Service" (for example, a business service facade).

This annotation serves as a specialization of @Component, which allows implementation classes to be auto-detected through class scans.

Component specialization is also @Repository and a @Controller

Additional information can be found, for example. here .

+4
source

As of Spring 3.1, there is no difference in how Spring processes them. The docs say this, but in a rather obscure way :

Spring 2.5 introduces additional stereotype annotations: @Component , @Service and @Controller . @Component is a common stereotype for any component that contains Spring. @Repository , @Service and @Controller are specializations of @Component for more specific use cases, for example, in persistence, service and presentation levels, respectively. Therefore, you can annotate your component classes with @Component , but instead annotating them with @Repository , @Service or @Controller , your classes are more suitable for processing with tools or binding to aspects. For example, these stereotype annotations make ideal targets for pointcuts. It is also possible that @Repository , @Service and @Controller may carry additional semantics in future versions of the Spring Framework . Thus, if you use between @Component or @Service for your level of service, @Service is definitely the best choice. Similarly, as stated above, @Repository already supported as a marker for automatically converting exceptions at your persistence level.

So, now @Service will be handled by Spring just like @Component , but @Service can be seen as a form of documentation.

I'm not sure why @Service was included in Spring 2.5 at all, as it does not seem to have any purpose.

+2
source

check source code

  @Target ({ElementType.TYPE})
 @Retention (RetentionPolicy.RUNTIME)
 @Documented
 @Component
 public @interface Service {

     / **
      * The value may indicate a suggestion for a logical component name,
      * to be turned into a Spring bean in case of an autodetected component.
      * @return the suggested component name, if any
      * /
     String value () default "";

 }

Service annotations are in turn annotated with @Component. There is nothing significant in this.

0
source

here is an explanation why we need such specialization ...

In Spring 2.0 and later, the @Repository annotation is a marker for any class that plays the role or stereotype of a repository (also known as a data access object or DAO). Among the uses of this token are automatic exception translation.

Spring 2.5 introduces additional annotations to stereotypes: @Component, @Service and @Controller. @Component is a common stereotype for any Spring component. @Repository, @Service and @Controller are specializations of @Component for more specific use cases, for example, in persistence, service, and presentation levels, respectively.

That way, you can annotate your component classes with @Component, but instead annotate them with @Repository, @Service or @Controller, your classes are more suitable for tooling or aspect binding. For example, these stereotype annotations make ideal targets for pointcuts.

Thus, if you choose between @Component or @Service for your service level, @Service is definitely the best choice. Similarly, as stated above, @Repository is already supported as a marker for automatically converting exceptions at your persistence level.

0
source

All Articles