In this quick article, we will discuss how to use @Repository annotation in Spring-based applications.
DAO or Repository classes usually represent the database access layer in an application and should be annotated with @Repository.
As of Spring 2.5, this annotation also serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.
@Repository annotation internally annotated with @Component annotation as shown in below diagram:
@Repository Annotation Example
Let's create a simple Spring boot application to bootstrap quickly. Add below dependencies to your pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Complete Code
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 getBean() method:
ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
UserService userService = (UserService) applicationContext.getBean("userServiceImpl");
userService.saveUser(new User(10, "Ramesh"));
Usage of @Service annotation
@Repository
interface UserRepository extends JpaRepository < User, Integer > {
}
One advantage of using this annotation is that it has automatic persistence exception translation enabled. When using a persistence framework such as Hibernate, native exceptions thrown within classes annotated with @Repository will be automatically translated into subclasses of Spring’s DataAccessExeption.
To enable exception translation, we need to declare our own PersistenceExceptionTranslationPostProcessor bean:
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Note, that in most cases, Spring does the step above automatically. Or, via XML configuration:
<bean class=
"org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
Related Annotations Posts
- Spring Boot Annotations
- 15 Spring Core Annotations
- Spring Scheduling Annotations
- The Spring @Controller and @RestController Annotations with Examples
- Spring @RequestBody and @ResponseBody Annotations
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring @Component Annotation
- Spring @Service Annotation
Comments
Post a comment