Where should the @Service annotation be stored? Interface or implementation?

I am developing an application using Spring. I have to use the @Service annotation. I am ServiceI and ServiceImpl such that ServiceImpl implements ServiceI . I am confused here where I should keep the @Service annotation.

Should I annotate an interface or implementation using the @Service annotation? What are the differences between the two approaches?

+55
spring service
May 03 '13 at 4:51
source share
6 answers

Basically annotations like @ Service , @Repository , @ Component , etc. they all serve the same purpose:

automatic detection when using an annotation configuration and scan class path.

From my experience, I always use @Service annotation on interfaces or abstract classes and annotations like @Component and @Repository to implement them. @Component annotation that I use on those classes that serve the main purpose, simple Spring beans, nothing more. @Repository annotation that I use in the DAO layer, for example. if I need to contact the database, have some transactions, etc.

Therefore, I would suggest annotating your interface using @Service and other layers depending on functionality.

+23
May 03 '13 at 7:42
source share

I never put @Component (or @Service , ...) in an interface because it makes the interface useless. Let me explain why.

Application 1: If you have an interface, you want to use this interface for the injection point type.

Statement 2: The purpose of an interface is that it defines a contract that can be implemented by several implementations. On the other hand, you have an injection point ( @Autowired ). Having only one interface and only one class that implements it is (IMHO) useless and violates YAGNI .

fact: when you put:

  • @Component (or @Service , ...) in the interface,
  • have several classes that implement it,
  • at least two classes become spring beans and
  • have an injection point that uses an interface for type-based input,

then you will get a NoUniqueBeanDefinitionException (or you have a very special setting, with the environment, profiles or qualifiers ...)

Conclusion: If you use @Component (or @Service , ...) in an interface, you must break at least one of the two glue. Therefore, I believe that it is not useful (except for some rare scenarios) to put @Component at the interface level.




Spring -Data-JPA Repository interfaces are something completely different

+51
Nov 04 '15 at 7:32
source share

I used @Component, @Service, @Controller and @Repository annotations only for implementation classes, not for the interface. But the @Autowired annotation with the interfaces still worked for me.

+9
Mar 30
source share

The advantages of adding annotations to @Service is that it gives a hint that this is a service. I do not know if any default implementation class will inherit this annotation.

Side

Con is that you associate your interface with a specific structure, i.e. Spring using spring specific annotation. Since interfaces should be separate from the implementation, I would not suggest using any annotations or the object part of your interface.

+4
Dec 16 '13 at 18:32
source share

Simply put:

@Service is a stereotype annotation for a service layer.

@Repository is a stereotype annotation for the level of perseverance .

@Component is a generic stereotypical annotation used to tell Spring to instantiate an object in an application context. You can define any name for the instance; by default, this is the name of the class as the case of a camel.

+2
Sep 18 '17 at 3:09 on
source share

There are 5 annotations you can use to create spring beans. List below answers.

Do you really need an interface? If you have one implementation for each service interface, just avoid it, use only the class. Of course, if you do not have RMI or when a proxy server is required.

@Repository - use to enter dao level classes.

@Service - use to enter service level classes. At the service level, you may also need the @Transactional annotation to manage your db transactions.

@Controller - Use interface level controllers for yours, such as JSF managed beans, injecting like spring beans.

@RestController - use spring for break controllers, this will help you avoid any time to put @ResponseBody and @RequestBody annotations in your rest methods.

@Component - use it in any other case when you need to enter a spring bean that is not a controller, service or tao class

-one
Nov 24 '16 at 6:42
source share



All Articles