📖 Scenario: You are building a simple Spring Boot application that manages a list of books. You want to separate the business logic from the controller by using a service class.
🎯 Goal: Create a service class annotated with @Service that provides a method to return a list of book titles.
📋 What You'll Learn
Create a class named BookService annotated with @Service.
Inside BookService, create a method getBookTitles() that returns a list of strings.
Initialize a list of book titles inside the service.
Use the service in a controller (not required to implement here, but keep the service ready).
💡 Why This Matters
🌍 Real World
In real applications, @Service classes hold business logic and data operations separate from controllers and repositories.
💼 Career
Understanding @Service is essential for building clean, maintainable Spring Boot applications and is a common requirement in Java backend developer roles.
Progress0 / 4 steps
1
Create the BookService class with a list of book titles
Create a class named BookService and inside it, create a private list of strings called bookTitles initialized with these exact values: "Spring Basics", "Java Fundamentals", "REST APIs".
Spring Boot
Hint
Use Arrays.asList to create the list inside the class.
2
Add the @Service annotation to BookService
Add the @Service annotation above the BookService class declaration. Also, import org.springframework.stereotype.Service.
Spring Boot
Hint
The @Service annotation marks the class as a service component in Spring.
3
Create the getBookTitles() method
Inside the BookService class, create a public method named getBookTitles() that returns the bookTitles list.
Spring Boot
Hint
The method should be public and return the list variable.
4
Complete the service class for Spring Boot use
Ensure the BookService class is public, annotated with @Service, and contains the bookTitles list and the getBookTitles() method as defined.
Spring Boot
Hint
Check that the class is annotated and all parts are present.
Practice
(1/5)
1. What is the main purpose of the @Service annotation in Spring Boot?
easy
A. To create a REST controller
B. To define a database entity
C. To mark a class as a service layer component for business logic
D. To configure application properties
Solution
Step 1: Understand the role of @Service
The @Service annotation is used to mark classes that hold business logic in the service layer.
Step 2: Differentiate from other annotations
It is not used for database entities (@Entity), REST controllers (@RestController), or configuration (@Configuration).
Final Answer:
To mark a class as a service layer component for business logic -> Option C
Quick Check:
@Service = service layer marker [OK]
Hint: Service annotation marks business logic classes [OK]
Common Mistakes:
Confusing @Service with @Entity or @Controller
Thinking @Service configures properties
Assuming @Service creates REST endpoints
2. Which of the following is the correct way to declare a service class using @Service in Spring Boot?
easy
A. public class MyService() @Service {}
B. public class MyService @Service {}
C. public @Service class MyService {}
D. @Service public class MyService {}
Solution
Step 1: Recall Java annotation syntax
Annotations must be placed before the class declaration without parentheses unless parameters are needed.
Step 2: Check each option's syntax
@Service public class MyService {} correctly places @Service before the class declaration. Options B, C, and D have incorrect placement or syntax.
Final Answer:
@Service public class MyService {} -> Option D
Quick Check:
Annotation before class = correct syntax [OK]
Hint: Put @Service right before class keyword [OK]
Common Mistakes:
Placing annotation after class name
Adding parentheses without parameters
Using annotation inside class declaration
3. Given the following code, what will be the output when myService.greet() is called?
@Service
public class MyService {
public String greet() {
return "Hello from Service!";
}
}
medium
A. "Hello from Service!"
B. null
C. Compilation error
D. Runtime exception
Solution
Step 1: Understand the method behavior
The greet() method returns the string "Hello from Service!" when called.
Step 2: Check for errors or exceptions
There is no syntax error or runtime exception in the code snippet.
Final Answer:
"Hello from Service!" -> Option A
Quick Check:
Method returns string = "Hello from Service!" [OK]
Hint: Method returns string directly, no errors [OK]
Common Mistakes:
Assuming null return without initialization
Thinking @Service causes errors
Confusing method output with annotation effect
4. Identify the error in the following Spring Boot service class:
public class UserService {
@Service
public void saveUser() {
// save logic
}
}
medium
A. @Service should annotate the class, not the method
B. Method saveUser must return a value
C. Class must extend a Spring base class
D. Missing @Autowired on saveUser method
Solution
Step 1: Check annotation placement
The @Service annotation is meant for classes, not methods.
Step 2: Verify method and class requirements
The method can be void and does not require @Autowired. The class does not need to extend any base class.
Final Answer:
@Service should annotate the class, not the method -> Option A
Quick Check:
@Service on class only [OK]
Hint: @Service decorates classes, not methods [OK]
Common Mistakes:
Putting @Service on methods
Expecting methods to return values always
Thinking @Autowired is needed on service methods
5. You want to create a service class that depends on a repository class. How should you use @Service and @Autowired together to follow Spring Boot best practices?
@Service
public class OrderService {
private final OrderRepository orderRepository;
// Constructor here
}
hard
A. Use @Service on the class and inject OrderRepository via field with @Autowired without constructor
B. Use @Service on the class and inject OrderRepository via constructor with @Autowired
C. Use @Service on the class and create OrderRepository manually inside methods
D. Use @Service on the class and no injection needed if repository is public
Solution
Step 1: Understand dependency injection best practice
Constructor injection with @Autowired is preferred for mandatory dependencies.
Step 2: Apply @Service and constructor injection
Annotate the class with @Service and create a constructor with @Autowired to inject OrderRepository.
Final Answer:
Use @Service on the class and inject OrderRepository via constructor with @Autowired -> Option B
Quick Check:
Constructor injection + @Service = best practice [OK]
Hint: Use constructor injection with @Autowired in @Service class [OK]
Common Mistakes:
Using field injection instead of constructor injection
Manually creating repository instances
Skipping injection assuming public access suffices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.