Last active
March 5, 2024 02:29
-
-
Save msbaek/077297f38b63019fe2aa50bec651ad0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// loading load runner | |
@Bean | |
public ApplicationRunner runner(BalanceRepository balanceRepository) { | |
return args -> { | |
// ... | |
}; | |
} | |
@Component | |
public class DataLoader implements CommandLineRunner { | |
@Override | |
public void run(String... args) throws Exception { | |
// ... | |
} | |
} | |
@RequiredArgsConstructor | |
@Component | |
@Profile("post") | |
public class PostDataLoader implements CommandLineRunner { | |
private final ObjectMapper objectMapper; | |
private final PostRepository repository; | |
@Override | |
public void run(String... args) throws Exception { | |
// src/main/resources/data/posts.json exists | |
try (InputStream inputStream = this.getClass().getResourceAsStream("/data/posts.json")) { | |
Posts response = objectMapper.readValue(inputStream, Posts.class); | |
repository.saveAll(response.posts()); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
repository.findAll().forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment