0 day streak
java / beginner
Snippet

Separating Business Logic with @Service

The @Service annotation is a specialization of @Component used to mark classes that hold business logic. It helps maintain a clean architecture by keeping calculations and data processing separate from web controllers.

snippet.java
java
1
2
3
4
5
6
@Service
public class UserService {
public String formatGreeting(String name) {
return "Welcome, " + name + "!";
}
}
spring
Breakdown
1
@Service
Tells Spring this class is a 'Service' bean to be managed in the context.
2
public class UserService
The class definition for our business logic container.
3
return "Welcome, " + name + "!";
The actual logic performed by the service.