In this quick article, we will discuss how to use @Service
annotation in
Spring ( Spring Boot) based applications.
The business logic of an application usually resides within the service layer – so we’ll use the @Service annotation to indicate that a class belongs to the service layer.
Well, @Service
annotation serves as a specialization of @Component, allowing for
implementation classes to be
autodetected through classpath scanning.
The below diagram shows a source code definition of @Service
annotation.
Spring context will autodetect these
classes when annotation-based configuration and classpath scanning is used because it is a specialization of
@Component as shown below
diagram.
Whenever we annotated a class with @Service annotation then Spring Container will automatically create a Spring bean for that class.
Let's create a simple Spring boot application to bootstrap quickly. Add the below dependencies to your
pom.xml
file.
<dependency>
<groupId>org.springframework.boot
<artifactId>spring-boot-starter-web
</dependency>
<dependency>
<groupId>org.springframework.boot
<artifactId>spring-boot-starter-data-jpa
</dependency>
Let's create a simple User
JPA entity that maps with the users
table in the database:
@Entity
@Table(name = "users")
class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Next, create a UserRepository
. interface and annotate with @Repository
annotation:
@Repository
interface UserRepository extends JpaRepository < User, Integer > {
}
Note Spring Data JPA automatically provides an implementation for the above interface.
interface UserService {
public void saveUser(User user);
}
@Service
class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public void saveUser(User user) {
userRepository.save(user);
}
}
Let's write a code to test UserRepository to save user objects into the database table:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
UserService userService = (UserService) applicationContext.getBean("userServiceImpl");
userService.saveUser(new User(10, "Ramesh"));
}
}
Here is the complete code for your reference:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
UserService userService = (UserService) applicationContext.getBean("userServiceImpl");
userService.saveUser(new User(10, "Ramesh"));
}
}
@Entity
@Table
class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Repository
interface UserRepository extends JpaRepository < User, Integer > {
}
interface UserService {
public void saveUser(User user);
}
@Service
class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public void saveUser(User user) {
userRepository.save(user);
}
}
Note that we have created ApplicationContext and retrieved bean using the getBean()
method:
ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
UserService userService = (UserService) applicationContext.getBean("userServiceImpl");
userService.saveUser(new User(10, "Ramesh"));
interface UserService {
public void saveUser(User user);
}
@Service
class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public void saveUser(User user) {
userRepository.save(user);
}
}