이 데이터들을 스프링 부트 시작시 넣을수 있도록 하자.
@SpringBootApplication
public class BlogRestApiApplication implements CommandLineRunner {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
public static void main(String[] args) {
SpringApplication.run(BlogRestApiApplication.class, args);
}
@Autowired
private RoleRepository roleRepository;
@Override
public void run(String... args) throws Exception {
//CommandLineRunner 구현으로 main이 실행되면 코드를 실행함
Role adminRole = new Role();
adminRole.setName("ROLE_ADMIN");
roleRepository.save(adminRole);
Role userRole = new Role();
userRole.setName("ROLE_USER");
roleRepository.save(userRole);
}
}
참고
https://madplay.github.io/post/run-code-on-application-startup-in-springboot