Difference Between @Controller and @Service Annotation in Spring
Last Updated :
04 May, 2022
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 GeeksForGeeks";
}
}
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. |
Similar Reads
How to Create Your First View in Spring MVC? Spring MVC is a powerful Web MVC Framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components:Model: Represents the data of the application. It can be a single object or a collection of objects.View
5 min read
Spring MVC CRUD with Example In this article, we will explore how to build a Spring MVC CRUD application from scratch. CRUD stands for Create, Read/Retrieve, Update, and Delete. These are the four basic operations to create any type of project. Spring MVC is a popular framework for building web applications. Spring MVC follows
7 min read
Create and Run Your First Spring MVC Controller in Eclipse/Spring Tool Suite Spring MVC framework enables the separation of modules, namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain Java classes. The model object can be passed between the view and the controller
5 min read
What is Dispatcher Servlet in Spring? Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made
6 min read
Spring - Shortcut to Create Dispatcher Servlet in Eclipse/Spring Tool Suite Eclipse is an Integrated Development Environment (IDE) used in computer programming. It includes a base workspace and an extensible plug-in system for customizing the environment. It is the second-most-popular IDE for Java development. Eclipse is written mostly in Java and its primary use is for dev
4 min read
WebApplicationContext in Spring MVC Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. The model object can be passed between view and controller using maps. T
7 min read
Spring - Multi Action Controller with Example Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made
4 min read
Spring MVC - Exception Handling Prerequisites: Spring MVC When something goes wrong with your application, the server displays an exception page defining the type of exception, the server-generated exception page is not user-friendly. Spring MVC provides exception handling for your web application to make sure you are sending your
6 min read
Spring - How to Load Literal Values From Properties File Literals in Java are a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named ââ/count is assigned an integer value in the following statement. int x = 100; // Here 100 is a constant/li
4 min read
Spring MVC - Multiple View Page A view page is redirected to another view page in this example. Let's look at a simple Spring Web MVC framework sample. The procedure is as follows: In the case of Maven, load the spring jar files or add dependencies.Make your controller class.Provide a controller entry in the web.xml file.In a sepa
3 min read