0 day streak
java / beginner
Snippet

Organizing Logic with @Service

The @Service annotation is a specialized form of @Component. It tells Spring that the class contains business logic, making it easier to organize your application into logical layers.

snippet.java
java
1
2
3
4
5
6
7
8
import org.springframework.stereotype.Service;
@Service
public class CalculatorService {
public int add(int a, int b) {
return a + b;
}
}
spring
Breakdown
1
@Service
Marks the class as a service bean that Spring will automatically detect and manage.
2
public class CalculatorService
A standard Java class that now benefits from Spring's lifecycle management.