0
0
Spring Bootframework~20 mins

@Service annotation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Service Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the primary role of a class annotated with @Service in Spring Boot?

Consider a Spring Boot application where a class is annotated with @Service. What is the main purpose of this annotation?

AIt marks the class as a repository to interact with the database.
BIt marks the class as a controller to handle HTTP requests.
CIt marks the class as a service provider to hold business logic and makes it a Spring bean for dependency injection.
DIt marks the class as a configuration class to define beans.
Attempts:
2 left
💡 Hint

Think about where business rules and logic usually reside in a Spring Boot app.

state_output
intermediate
1:30remaining
What happens when you forget to annotate a service class with @Service?

Given a Spring Boot app where a class contains business logic but is not annotated with @Service or any stereotype, what will happen when another component tries to inject it?

ASpring will throw a <code>NoSuchBeanDefinitionException</code> because the class is not registered as a bean.
BThe class will be injected successfully because Spring scans all classes automatically.
CSpring will inject a proxy instead of the actual class.
DThe application will compile but fail at runtime with a <code>NullPointerException</code>.
Attempts:
2 left
💡 Hint

Think about how Spring knows which classes to manage as beans.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly defines a service class with @Service and constructor injection?

Choose the correct way to define a Spring Boot service class using @Service and constructor-based dependency injection for a repository.

Spring Boot
public interface UserRepository { /* methods */ }

// Choose the correct service class below:
A
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserRepository repo;
}
B
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private final UserRepository repo;
    public UserService(UserRepository repo) {
        this.repo = repo;
    }
}
C
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private UserRepository repo;
    public void UserService(UserRepository repo) {
        this.repo = repo;
    }
}
D
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private UserRepository repo;
    public UserService() {}
}
Attempts:
2 left
💡 Hint

Constructor injection requires a constructor with parameters and final fields for immutability.

🔧 Debug
advanced
2:00remaining
Why does this @Service class cause a circular dependency error?

Given two service classes annotated with @Service that inject each other via constructor injection, what is the cause of the circular dependency error?

Spring Boot
import org.springframework.stereotype.Service;

@Service
public class ServiceA {
    private final ServiceB serviceB;
    public ServiceA(ServiceB serviceB) {
        this.serviceB = serviceB;
    }
}

@Service
public class ServiceB {
    private final ServiceA serviceA;
    public ServiceB(ServiceA serviceA) {
        this.serviceA = serviceA;
    }
}
ABoth services depend on each other via constructor injection, causing Spring to fail creating beans due to circular dependency.
BThe services should use field injection instead of constructor injection to avoid circular dependency.
CThe @Service annotation is missing on one of the classes, so Spring cannot inject properly.
DSpring Boot does not support multiple @Service classes in the same package.
Attempts:
2 left
💡 Hint

Think about how Spring creates beans and resolves dependencies.

🧠 Conceptual
expert
2:30remaining
How does Spring Boot treat a class annotated with @Service differently from one annotated with @Component?

Both @Service and @Component annotations register a class as a Spring bean. What is the key conceptual difference between them?

A<code>@Component</code> automatically enables transaction management, but <code>@Service</code> does not.
B<code>@Component</code> is only for UI components, while <code>@Service</code> is for backend logic.
C<code>@Service</code> classes are not eligible for dependency injection, unlike <code>@Component</code> classes.
D<code>@Service</code> is a specialized stereotype indicating the class holds business logic, while <code>@Component</code> is a generic stereotype without semantic meaning.
Attempts:
2 left
💡 Hint

Think about the purpose of stereotype annotations in Spring.