|
| 1 | +package com.baeldung.config; |
| 2 | + |
| 3 | +import com.baeldung.data.BookRepository; |
| 4 | +import com.baeldung.model.Book; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.ApplicationArguments; |
| 7 | +import org.springframework.boot.ApplicationRunner; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | + |
| 10 | +import java.util.Random; |
| 11 | +import java.util.stream.IntStream; |
| 12 | + |
| 13 | +@Component |
| 14 | +public class DBLoader implements ApplicationRunner { |
| 15 | + |
| 16 | + private final BookRepository bookRepository; |
| 17 | + |
| 18 | + @Autowired |
| 19 | + DBLoader(BookRepository bookRepository){ |
| 20 | + this.bookRepository = bookRepository; |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + public void run(ApplicationArguments applicationArguments) throws Exception { |
| 25 | + |
| 26 | + String[] templates = { |
| 27 | + "Up and running with %s", |
| 28 | + "%s Basics", |
| 29 | + "%s for Beginners", |
| 30 | + "%s for Neckbeards", |
| 31 | + "Under the hood: %s", |
| 32 | + "Discovering %s", |
| 33 | + "A short guide to %s", |
| 34 | + "%s with Baeldung" |
| 35 | + }; |
| 36 | + |
| 37 | + String[] buzzWords = { |
| 38 | + "Spring REST Data", |
| 39 | + "Java 9", |
| 40 | + "Scala", |
| 41 | + "Groovy", |
| 42 | + "Hibernate", |
| 43 | + "Spring HATEOS", |
| 44 | + "The HAL Browser", |
| 45 | + "Spring webflux", |
| 46 | + }; |
| 47 | + |
| 48 | + String[] authorFirstName = { |
| 49 | + "John %s", |
| 50 | + "Steve %s", |
| 51 | + "Samantha %s", |
| 52 | + "Gale %s", |
| 53 | + "Tom %s" |
| 54 | + }; |
| 55 | + |
| 56 | + String[] authorLastName = { |
| 57 | + "Giles", |
| 58 | + "Gill", |
| 59 | + "Smith", |
| 60 | + "Armstrong" |
| 61 | + }; |
| 62 | + |
| 63 | + String[] blurbs = { |
| 64 | + "It was getting dark when the %s %s" , |
| 65 | + "Scott was nearly there when he heard that a %s %s", |
| 66 | + "Diana was a lovable Java coder until the %s %s", |
| 67 | + "The gripping story of a small %s and the day it %s" |
| 68 | + }; |
| 69 | + |
| 70 | + String[] blublMiddles = { |
| 71 | + "distaster", |
| 72 | + "dog", |
| 73 | + "cat", |
| 74 | + "turtle", |
| 75 | + "hurricane" |
| 76 | + }; |
| 77 | + |
| 78 | + String[] end = { |
| 79 | + "hit the school", |
| 80 | + "memorised pi to 100 decimal places!", |
| 81 | + "became a world champion armwrestler", |
| 82 | + "became a Java REST master!!" |
| 83 | + }; |
| 84 | + |
| 85 | + Random random = new Random(); |
| 86 | + |
| 87 | + IntStream.range(0, 100) |
| 88 | + .forEach(i -> { |
| 89 | + String template = templates[i % templates.length]; |
| 90 | + String buzzword = buzzWords[i % buzzWords.length]; |
| 91 | + String blurbStart = blurbs[i % blurbs.length]; |
| 92 | + String middle = blublMiddles[i % blublMiddles.length]; |
| 93 | + String ending = end[i % end.length]; |
| 94 | + String blurb = String.format(blurbStart, middle, ending); |
| 95 | + String firstName = authorFirstName[i % authorFirstName.length]; |
| 96 | + String lastname = authorLastName[i % authorLastName.length]; |
| 97 | + Book book = new Book(String.format(template, buzzword), String.format(firstName, lastname), blurb, random.nextInt(1000-200) + 200); |
| 98 | + |
| 99 | + bookRepository.save(book); |
| 100 | + |
| 101 | + System.out.println(book); |
| 102 | + |
| 103 | + }); |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + } |
| 108 | +} |
0 commit comments