|
4 | 4 | import lombok.RequiredArgsConstructor; |
5 | 5 | import lombok.Value; |
6 | 6 | import lombok.With; |
| 7 | +import lombok.extern.log4j.Log4j2; |
| 8 | +import org.springframework.boot.ApplicationRunner; |
| 9 | +import org.springframework.context.annotation.Bean; |
7 | 10 | import org.springframework.context.annotation.Configuration; |
8 | 11 | import org.springframework.data.annotation.Id; |
9 | 12 | import org.springframework.data.annotation.PersistenceConstructor; |
10 | 13 | import org.springframework.data.relational.core.mapping.Table; |
11 | 14 | import org.springframework.data.repository.CrudRepository; |
12 | 15 |
|
| 16 | +import java.util.Iterator; |
13 | 17 | import java.util.Objects; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import java.util.stream.Stream; |
14 | 20 |
|
15 | 21 | @With |
16 | 22 | @Value |
@@ -49,5 +55,30 @@ public static UserRef of(User user) { |
49 | 55 |
|
50 | 56 | interface MessageRepository extends CrudRepository<Message, Long> {} |
51 | 57 |
|
| 58 | +@Log4j2 |
52 | 59 | @Configuration |
53 | | -public class MessagingApp { /* stub */ } |
| 60 | +public class MessagingApp { |
| 61 | + /* stub */ |
| 62 | + @Bean |
| 63 | + ApplicationRunner applicationRunner(UserRepository userRepository, |
| 64 | + MessageRepository messageRepository) { |
| 65 | + return args -> { |
| 66 | + Iterable<User> users = userRepository.saveAll( |
| 67 | + Stream.of("lola", "bola") |
| 68 | + .map(name -> new User(null, name)) |
| 69 | + .collect(Collectors.toList())); |
| 70 | + log.info("saved: {}", users); |
| 71 | + |
| 72 | + Iterator<User> iterator = userRepository.findAll() |
| 73 | + .iterator(); |
| 74 | + User from = iterator.next(); |
| 75 | + User to = iterator.next(); |
| 76 | + |
| 77 | + Iterable<Message> messages = messageRepository.saveAll( |
| 78 | + Stream.of("ololo", "trololo") |
| 79 | + .map(body -> new Message(null, body, UserRef.of(from), UserRef.of(to))) |
| 80 | + .collect(Collectors.toList())); |
| 81 | + log.info(messages); |
| 82 | + }; |
| 83 | + } |
| 84 | +} |
0 commit comments