Spring @Service Annotation Examples
- Details
- Written by Nam Ha Minh
- Last Updated on 06 July 2023   |   Print Email
import org.springframework.stereotype.Service; @Service public class UserService { public List<User> list() { User user1 = new User(1, "Natalie Kidsman"); User user2 = new User(2, "John Doe"); return List.of(user1, user2); } }In this example, the UserService class provides some business logics related to user management domain.By default, the bean name will be userService. You can specify name of the managed bean explicitly like this:
@Service("userBean") public class UserService { }Here, Spring will create a bean of type UserService and name it as userBean in the application context.
@RestController public class UserController { @Autowired UserService userService; @GetMapping("/users") public List<User> listUsers() { return userService.list(); } }For testing purpose, you can use the following program that gets a UserService object (bean) from the application context, and then invoke its list() method:
package net.codejava; import java.util.List; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringApplication { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.scan("net.codejava"); context.refresh(); UserService service = (UserService) context.getBean("userService"); List<User> listUsers = service.list(); listUsers.forEach(System.out::println); } }In case you’re using Spring Boot, the following example program does the same:
package net.codejava; import java.util.List; import org.springframework.beans.BeansException; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @SpringBootApplication public class SpringAnnotationsApplication implements ApplicationContextAware { static ApplicationContext context; public static void main(String[] args) { SpringApplication.run(SpringAnnotationsApplication.class, args); UserService service = (UserService) context.getBean("userService"); List<User> listUsers = service.list(); listUsers.forEach(System.out::println); } @Override public void setApplicationContext(ApplicationContext context) throws BeansException { this.context = context; } }I hope you now understand the purpose and usage of the @Service annotation in Spring framework. It can be used at class level only. And also note that this annotation is a specialization of @Component - both are technically the same. They differ only in semantic purpose.Watch the following video to see the coding in action:
Reference:
Annotation Interface Service (Spring Docs)
Other Spring Annotations:
- Spring @Component Annotation Examples
- Spring @Repository Annotation Examples
- Spring @Configuration Annotation Examples
- Spring @Controller Annotation Examples
- Spring @RestController Annotation Examples
Comments