3

I am new to Spring boot app. I am not sure about the correct naming and annotation for my app.

I have following service interface.

@Service
public interface StockService {
    public String getStockContent();
    public List<StockElement> parseStockContents(String stockContent);
}

I have following service implementation

@Service
public class StockServiceImpl implements StockService{

    public String getStockContent() {
        // IMPL removed because it is not relevant to this question. 
     }


    List<StockElement> parseStockContents(String stockContent) {
        // IMPL removed because it is not relevant to this question. 
     }   
 }

In terms of functionality, it is working nicely. I have seen there were many mvc naming conventions. Just as model, entity, repository, repositoryImpl, dao, daoImpl, service, serviceImp, controller, etc. They have also annotation accordingly.

I clearly understand model, entity, service, controller.

I have confusion about dao and repository. Also in my code, I have service interface and implementation. I annotated both as @Service and it is working. I have confusion about it. Will both be annotated as @Service. Also, I have seen many places annotation as @Component. What is the use of @Component?

Could you please explain in details with some code snippet?

masiboo
  • 4,537
  • 9
  • 75
  • 136

3 Answers3

1

I have confusion about dao and repository.

The Spring repository (design and usage) looks like more a DAO that a Repository in terms of DDD. So you may consider it as a DAO.

Also in my code, I have service interface and implementation. I annotated both as @Service and it is working. I have confusion about it. Will both be annotated as @Service.

An interface should be as much agnostic as possible of the technology used by the implementation behind. So annotating the interface with Spring is not very desirable, it is not considered by Spring either and at last it is useless as defining it in the implementation is enough.

Also, I have seen many places annotation as @Component. What is the use of @Component?

The @Component javadoc states :

Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the @Repository annotation or AspectJ's @Aspect annotation.

You can consider it as the most broad/general stereotype annotation as you will define a class as a Spring bean.
You annotate for example @Controller, @Service or @Repository stereotypes to a class as this class matches to one of them.
Sometimes, no one of them suits to your class, so you use @Component.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

@DAO is not Spring annotation (and naming convention), thus the confusion. It is legacy naming inherited from EJB. @Repository is the exact same thing - data access object - in Spring environement

Here is some details showing that its just matter of naming while functionality and purpose is exactly the same https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/dao.html

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

As far as the question of applying @Service annotation on Interface or its implementing class is concerned, annotating the implementing classes is enough. Spring doc link for Service annotation @Component you may want to use only for the POJOs and other model classes. For the repository classes, it is okay to use @Repository or any other extending data-source specific annotations for JPA repositories or Mongo Repositories etc.

References:

iamharish15
  • 1,760
  • 1
  • 17
  • 20