In application development based on Spring framework, you will see the @Service annotation used to annotate some classes. So what is it? Why and how to use the @Service annotation?

In this short post, I’m going to explain the meaning and purpose of using @Service annotation with some code examples.

You can use the @Service annotation to indicate that an annotated class is a “Service” which implements business logics relate to a particular domain of the application. And a service doesn’t maintain states.

Semantically, use the @Service annotation to mark a class as a service.

Technically, the @Service annotation allows the annotated class to be autodetected through classpath scanning which is performed by Spring framework upon application’s startup. That means Spring framework will find and create an instance of the service class, and put the object as a managed bean in the application context. Then the managed bean will be available for dependency injection in other components, such as via @Autowired field.

For example, the following class is annotated with @Service annotation:

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.

Then it’s possible to inject the managed bean of this service class in another component, e.g. a controller, as shown below:

@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:

 


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment