Difference Between @Controller and @Service Annotation in Spring
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program.
Spring @Controller Annotation
Spring @Controller annotation is a specialization of @Component annotation. The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It’s used to mark a class as a web request handler. It’s mostly used with Spring MVC applications. This annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.
Implementation: Project
Step 1: Create a Simple Spring Boot Project
Refer to this article Create and Setup Spring Boot Project in Eclipse IDE and create a simple spring boot project.
Step 2: Add the spring-web dependency in your pom.xml file. Go to the pom.xml file inside your project and add the following spring-web dependency.
XML
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > |
Step 3: In your project create one package and name the package “controller”. In the controller, the package creates a class and name it as DemoController. This is going to be our final project structure.
Java
// Java Program to Illustrate DemoController File // Importing package in this code module package com.example.demo.controller; // Importing required classes import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; // Annotation @Controller // Main class public class DemoController { @RequestMapping ( "/hello" ) @ResponseBody // Method public String helloGFG() { return "Hello w3wiki" ; } } |
We have used the below annotations in our controller layer. Here in this example, the URI path is /hello.
- @Controller: This is used to specify the controller.
- @RequestMapping: This is used to map to the Spring MVC controller method.
- @ResponseBody: Used to bind the HTTP response body with a domain object in the return type.
Step 4: Now, our controller is ready. Let’s run our application inside the DemoApplication.java file. There is no need to change anything inside the DemoApplication.java file.
Example
Java
// Java Program to Illustrate DemoApplication File // Importing package in this code module package com.example.demo; // Importing required classes import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; // Annotation @SpringBootApplication // Main class public class DemoApplication { // Main driver method public static void main(String[] args) { SpringApplication.run(DemoApplication. class , args); } } |
Output:
Tip: Try Tomcat URL depicted in below media, which is running on http://localhost:8989/hello
Spring @Service Annotation
In an application, the business logic resides within the service layer so we use the @Service Annotation to indicate that a class belongs to that layer. It is also a specialization of @Component Annotation like the @Repository Annotation. One most important thing about the @Service Annotation is it can be applied only to classes. It is used to mark the class as a service provider. So overall @Service annotation is used with classes that provide some business functionalities. Spring context will autodetect these classes when annotation-based configuration and classpath scanning is used.
Example: Project
Step 1: Create a Simple Spring Boot Project.
tip: Do refer to this article prior to adhering forward on how to create and setup Spring Boot Project in Eclipse IDE and create a simple spring boot project.
Step 2: Add the spring-context dependency in your pom.xml file. Go to the pom.xml file inside your project and add the following spring-context dependency.
File: pom.xml
XML
< dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >5.3.13</ version > </ dependency > |
Step 3: In your project create one package and name the package as “service”. In the service, the package creates a class and name it “MyServiceClass”.
File: MyServiceClass
Java
// Java Program to Illustrate MyServiceClass // Importing package module to code module package com.example.demo.service; // Importing required classes import org.springframework.stereotype.Service; // Annotation @Service // Class public class MyServiceClass { // Method // To compute factorial public int factorial( int n) { // Base case if (n == 0 ) return 1 ; // Returning factorial of input number return n * factorial(n - 1 ); } } |
Code explanation:
From the above code, we can easily perceive that it’s a simple java class that provides functionalities to calculate the factorial of a number. So we can call it a service provider. We have annotated it with @Service annotation so that spring-context can autodetect it and we can get its instance from the context.
Step 4: Spring Repository Test
So now our Spring Repository is ready, let’s test it out. Go to the DemoApplication.java file and refer to the below code.
File: DemoApplication.java
Java
// Java Program to Illustrate DemoApplication // Importing package module to code fragment package com.example.demo; // Importing required classes import com.example.demo.service.MyServiceClass; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.AnnotationConfigApplicationContext; // Annotation @SpringBootApplication // Main class public class DemoApplication { // Main driver method public static void main(String[] args) { // Creating context of // AnnotationConfigApplicationContext class AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.scan( "com.example.demo" ); context.refresh(); MyServiceClass myServiceClass = context.getBean(MyServiceClass. class ); // Testing the factorial method int factorialOf5 = myServiceClass.factorial( 5 ); System.out.println( "Factorial of 5 is: " + factorialOf5); // Closing the spring context // using close() method context.close(); } } |
Output:
Lastly, let us do discuss the differences between @Controller annotation and @Service annotation.
@Controller Annotation |
@Service Annotation |
---|---|
@Controller annotation indicates that a particular class serves the role of a controller. | @Service annotation is used with classes that provide some business functionalities. |
@Controller annotation is a specialization of @Component annotation. | @Service Annotation is also a specialization of @Component Annotation. |
It can be applied to classes only. | It can be applied to classes only. |
It’s used to mark a class as a web request handler. | It is used to mark the class as a service provider. |
It is a stereotype for the presentation layer (spring-MVC). | It is a stereotype for the service layer. |
We cannot switch this annotation with any other like @Service or @Repository. | Switch can be possible. |