@Service is a Spring stereotype annotation used for the service layer.
It marks a class that contains business logic.
Technically, @Service works the same as @Component for bean registration, but it adds semantic meaning that makes the code easier to understand.
Spring applications are usually organized into layers:
- Controller layer
- Service layer
- Repository layer
The service layer sits in the middle and coordinates business operations.
Using @Service makes it clear that a class is part of that layer.
@Service
public class OrderService {
public void placeOrder() {
System.out.println("Processing order");
}
}Spring detects this class during component scanning and creates a bean for it.
A service class usually contains:
- business rules
- orchestration logic
- validation logic related to the use case
- coordination between repositories and other components
- transactional boundaries
It should not contain HTTP-specific logic or SQL-specific logic.
@Service
public class PaymentService {
private final PaymentRepository paymentRepository;
public PaymentService(PaymentRepository paymentRepository) {
this.paymentRepository = paymentRepository;
}
public void processPayment(Payment payment) {
if (payment.getAmount() <= 0) {
throw new IllegalArgumentException("Amount must be positive");
}
paymentRepository.save(payment);
}
}Here the service:
- validates the request
- applies business logic
- delegates persistence to the repository
Technically, both register a Spring bean.
But @Service is better when the class performs business logic because it communicates intent.
| Aspect | @Component |
@Service |
|---|---|---|
| Bean registration | Yes | Yes |
| Semantic meaning | Generic | Business layer |
| Readability | Medium | High |
| Best use | General bean | Service logic |
Service classes are often the right place for transactional behavior.
@Service
public class AccountService {
@Transactional
public void transferMoney(Long fromId, Long toId, BigDecimal amount) {
// business logic
}
}This works well because the service layer represents a business operation boundary.
-
keep business rules in services
-
keep controllers thin
-
keep repositories focused on persistence
-
avoid putting HTTP request code in services
-
avoid putting SQL code directly in services
A good service class should be easy to test without the web layer.
-
using
@Serviceon classes that are not business logic -
putting too much controller logic in the service layer
-
letting service classes become giant god classes
-
directly mixing database queries with business workflows
-
forgetting that
@Serviceis still a Spring bean and can be injected normally
It marks a class as part of the business/service layer.
No, both register beans, but @Service carries semantic meaning.
In the service layer.
It improves readability and makes the application structure clearer.
-
@Serviceis a specialized stereotype annotation -
It is used for business logic classes
-
It behaves like
@Componentbut is more expressive -
Services sit between controllers and repositories
-
Transactional workflows often belong here