Skip to content

Instantly share code, notes, and snippets.

@msbaek
Last active March 5, 2024 02:29
Show Gist options
  • Save msbaek/077297f38b63019fe2aa50bec651ad0c to your computer and use it in GitHub Desktop.
Save msbaek/077297f38b63019fe2aa50bec651ad0c to your computer and use it in GitHub Desktop.
// 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