-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathBootstrapData.java
More file actions
62 lines (42 loc) · 2.08 KB
/
BootstrapData.java
File metadata and controls
62 lines (42 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package guru.springframework.spring6webapp.bootstrap;
import guru.springframework.spring6webapp.domain.Author;
import guru.springframework.spring6webapp.domain.Book;
import guru.springframework.spring6webapp.repositories.IAuthorRepositroy;
import guru.springframework.spring6webapp.repositories.IBookRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component // This annotation indicates that this class is a Spring component and will be automatically detected by Spring's component scanning
public class BootstrapData implements CommandLineRunner {
private final IAuthorRepositroy iAuthorRepositroy;
private final IBookRepository iBookRepository;
public BootstrapData(IAuthorRepositroy iAuthorRepositroy, IBookRepository iBookRepository) {
this.iAuthorRepositroy = iAuthorRepositroy;
this.iBookRepository = iBookRepository;
}
@Override
public void run(String... args) throws Exception {
Author author1 = new Author();
author1.setFirstName("Nabil");
author1.setLastName("Boutachrafine");
Book book1 = new Book();
book1.setTitle("Spring Framework 6");
book1.setIsbn("1234567890");
Author authorSaved1 = iAuthorRepositroy.save(author1);
Book bookSaved1 = iBookRepository.save(book1);
Author author2 = new Author();
author2.setFirstName("Tawfiq");
author2.setLastName("Boutachrafine");
Book book2 = new Book();
book2.setTitle("Spring Boot 3");
book2.setIsbn("0987654321");
Author authorSaved2 = iAuthorRepositroy.save(author2);
Book bookSaved2 = iBookRepository.save(book2);
authorSaved1.getBooks().add(bookSaved1);
authorSaved2.getBooks().add(bookSaved2);
iAuthorRepositroy.save(authorSaved1);
iAuthorRepositroy.save(authorSaved2);
System.out.println("Bootstrap Data Loaded");
System.out.println("Number of Authors: " + iAuthorRepositroy.count());
System.out.println("Number of Books: " + iBookRepository.count());
}
}