@Component @Controller @Service and @Repository annotations example using spring boot

In this post, we will see @Component @Controller @Service and @Repository annotations example using spring boot. First, we will see some basics about these annotations. Later we will have an example where we will see how to use these annotations.

Basic points about @Component @Controller @Service and @Repository annotations.

@Component annotation.

  • This annotation introduced in spring 2.5,  available in org.springframework.stereotype package.
  • This annotation used with classes and interfaces.
  • When we use this annotation with class, that class will be available for auto detection(automatically bean will be created) while using component scan based annotation configuration.
  • internal implementation.

 

 

@Service annotation.

  • This annotation introduced in spring 2.5,  available in org.springframework.stereotype package.
  • This annotation used with classes and interfaces.
  • This annotation behaves similarly to @Component. The only difference it helps our classes more readable, using this annotation with class indicates this is our service class(mostly our business logic exist in these classes).
  • internal implementation.

 

@Repository annotation – 

  • This annotation introduced in spring 2.0,  available in org.springframework.stereotype package.
  • This annotation used with DAO interfaces.
  • internal implementation.

 

@Controller annotation.

  • This annotation introduced in Spring 2.5, available in org.springframework.stereotype package.
  • We use this annotation with the class, in case of this annotation we need to use @ResponseBody explicitly.
  • This annotation used with presentation layer i.e controller classes(people also prefer to call endpoint).
  • internal implementation.

 

 

Note – In the interview, you may be asked what is the difference between  @Component, @Service, and @Repository.  @Service and @Repository is a special form of @Component. @Service annotation used with Service layer classes and @Repository used with the persistent layer. @Component we can use either service layer or persistent layer but we generally avoid since we have separate annotations for both(@Service and @Repository). If we look declaration of @Service and @Repository both are internally annotated with @Component. Again what is the difference between these annotations, functionally there is no difference. If you use @Component instead of @Repository and @Service it will work fine but since we have difference annotations for a different purpose why we should not use to proper place. It will make our classes more readable. Apart from that @Repository supports exception translation functionality.

 

How to use these annotations.

@Component – Suppose we have a class in our application EncryptDecryptPassword.java where we have logic for encryption and decryption. This class we can annotate with @Component.

@Service – Any service layer class where we have business logic. For example BookServiceImpl.java

@Repository – Any persistent layer which is going to communicate with the database. For example – BookRepository.java

 

@Controller – Any presentation layer i.e controller classes/endpoint.

 

Note  – @Controller, @Service, and @Repository derived from @Component.

@Component @Controller @Service and @Repository annotations example using spring boot

 

@Component @Controller @Service and @Repository annotations example using spring boot example.

Create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – pathvariableexample, ArtifactId – pathvariableexample and name – pathvariableexample) and click on finish. Keep packaging as the jar.

 

Modify the pom.xml with below code.

 

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

Let maven download all necessary jar. Once it is done we will able to see maven dependency folder which contains different jar files.

We can start to write our controller classes, ServiceImpl and Repository. The folder structure of application-

 

Step 6 – Define main class SpringDataExampleUsingOracle.java

SpringDataExampleUsingOracle.java

 

Step 7 – Define entity class i.e Book.java

Book.java

Step 8 – Define repository interface extending CrudRepository.

BookRepository.java

 

Step 9 – Define service interface i.e BookService.java

BookService.java

Step 10 – Define service impl class.

BookServiceImpl.java

 

Step 11 – Define controller class.

BookController.java

 

Step 12 – Define JpaConfig class.

JpaConfig.java

 

Step 13 – Define application.properties file

application.properties

 

That’s all about @Component @Controller @Service and @Repository annotations example using spring boot.

You may like.

 

Check @Service annotation docs.

 

 

Top