Java Spring Framework: Component, Repository, Service-Differences


Explain the difference between: Component, Repository, Service in the Java Spring Framework.

Give simple usage examples.

Author: ragmon, 2015-11-25

1 answers

@Component - this is any arbitrary bin that Spring can find via autoscan.

Example: you have implemented a class and want to make its instance available via @Autowired.


The remaining annotations represent the layers of a typical multi-layer architecture:

  • @Repository - this is @Component with the semantics of the DAO layer. The place where you go to the database. Spring will wrap the exceptions thrown by the class in DataAccessException.

  • @Service - this is @Component with semantics business logic layer.

  • @Controller - this is @Component with the semantics of the web layer. Spring will turn such beans into servlets.

 14
Author: Nofate, 2015-11-25 13:38:20