EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Service
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot Service

Spring Boot Service

Introduction to Spring Boot Service

Spring boot service component is defined as a class file that includes the @Service annotation and allows developers to add business functionalities. The annotation is used with the classes that provide these business functionalities. These classes are auto-detected by the spring context when the annotation-based configuration is used along with class path scanning as it provides a specialization of @Component annotation. The operation performed by the annotated component is offered as an interface and stands alone in the model with no state of encapsulation. This annotation is a general purposed stereotype, and developers may choose to narrow the semantics and make the use of the same as felt appropriate.

Syntax of Spring Boot Service

The spring-boot service component, as mentioned, is a class file for putting in designated areas of adding business functionalities. Here we will see about the spring boot service component from the syntax perspective so that while we see about working of spring boot service component and its features, mapping back to the syntax will enable looking at the complete picture.

1. Decorator needed for working with service component in Spring Boot application.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

@Service

2. Importing of function that is required for service component working.

import org.springframework.stereotype.Service;

How does Service Component work in Spring Boot?

  • Here we will see the working of the service component in Spring Boot, but before that, we need to know not only the significance of the service component but also the significance of the placement of a @Spring component. It is very important for us to know that typical applications contain distinct layers, for example, access of data, presenting the data, services and business, and in each of these individual layers, there are various beans which enable automatic detection of the layers. @Service annotation helps annotating the classes at the service layer. As we have earlier mentioned, that @Service component provides a specialization of @Component. Now, this makes it even more interesting for us to understand that @Component annotation is used across applications so that the beans are marked as Spring’s managed component, whereas beans marked with @Service happens to house section of the business logic and hence used only in the service layer because of no additional speciality.
  • The service component ensures that the class file which incorporates business logic in a different layer and keeps it separated from the @RestController class file. For the functioning of @Service annotation, we need to add the spring core dependency. During the early release of Spring, all beans were declared in an XML file, but with the pointer of scalability in mind, this becomes a herculean task, and hence the @Component annotation provides annotation based injection with the configuration being Java-based. Thus, having the annotation-based injection reduces the declaration of the bean as a <bean> tag. Apart from @Service, which is mainly used in the service layer, @Controller is mainly used for the presentation layer and @Repository for the Data Access Layer or the DAO or Persistence layer.
  • Now we will look at the working of the service component using an example. We will declare a service to calculate the interest in case of a deposit made to a bank. At first, we will build the service layer, which will contain the service class that will contain the required function. These functions can be simple interest and/or compound interest functions. The class will be annotated as @Service so that the spring context is able to auto-detect it, and the instance can be instantiated from the context. Now we would define a new class that will contain the instance of the service class. The application context is instantiated. Now the contaxt.scan () function will look for all the annotation, and the required annotation will be registered. Now, as per the @Service annotation, we form the Bean of service class made. This bean will be an instance which will be used for calling any function within the class. Once the required operations are performed, the application context is closed. The instantiation of the bean can also be achieved through explicit casting.

Example of Spring Boot Service

Given below is the example of Spring Boot Service:

Understanding the flow of requests.

Syntax:

InterestCalculator.java:

package com.educba.demo;
import org.springframework.stereotype.Service;
import java.lang.Math;
@Service("ic")
public class InterestCalculator {
public double CI(double P, double R, double T) {
double rate = 1+(R/100);
return P*Math.pow(rate,T)-P;
}
public double SI(double P, double R, double T) {
return P*R*T/100;
}
}

DemoserviceApplication.java:

package com.educba.demo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DemoserviceApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.educba.demo");
context.refresh();
InterestCalculator ic = context.getBean(InterestCalculator.class);
double ci = ic.CI(1000, 8, 2);
System.out.println("Compound Interest with Principal = 1000, R=8%, T=2 Yrs = " + ci);
double si = ic.SI(1000, 8, 2);
System.out.println("Simple Interest with Principal = 1000, R=8%, T=2 Yrs = " + si);
// Always close the Spring context in order to avoid leakage
context.close();
}
}

Pom.xml (add the below dependency to the existing pom.xml):

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>

Output:

Spring Boot Service

Conclusion

To conclude, this article has provided innumerable insights into how the service component works, along with a short snippet of how it differs from the @Component annotation. In addition, we have gone through a hands-on example to get the idea clearer. One thing to keep in mind for the developers is to always close the application context in order to avoid any leakage.

Recommended Articles

This is a guide to Spring Boot Service. Here we discuss the introduction, how the service component works in spring boot? And example. You may also have a look at the following articles to learn more –

  1. Maven Repository Spring
  2. Spring Boot DevTools
  3. Spring AOP
  4. Spring Cloud Components
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more