In this quick article, we will discuss how to use the Spring or Spring Boot @Repository annotation with an example.
YouTube Video
DAO or Repository classes usually represent the database access layer in an application and should be annotated with @Repository annotation.
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 the below diagram: Whenever we annotated a class with @Repository annotation then Spring Container will automatically create a Spring bean for that class.
@Repository Annotation Example
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</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Create User JPA Entity
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; } }
Create UserRepository
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.
Create UserService and UserServiceImpl
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); } }
Testing
Let's write a code to test UserRepository to save user objects into 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")); } }
Complete Code
Here is the complete code for your reference:
import jakarta.persistence.*;
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"));
Usage of @Repository annotation in Spring Data JPA Repository
@Repository
interface UserRepository extends JpaRepository < User, Integer > {
}
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Comments
Post a Comment
Leave Comment