develog

[spring boot] 초기화 코드 본문

Dev/Spring Boot

[spring boot] 초기화 코드

냐옴 2020. 11. 1. 23:12
  • PostConstruct
@Component
@RequiredArgsConstructor
public class InitDB {

    private final InitService initService;

    @PostConstruct
    public void init() {
        initService.dbInit1();
    }
}
@Component
@RequiredArgsConstructor
public class InitDb {

    private final ItemRepository itemRepository;

    @PostConstruct
    public void init() {
        itemRepository.save(new Item("item1", 1000, 10));
        itemRepository.save(new Item("item2", 2000, 20));
        itemRepository.save(new Item("item3", 3000, 30));
    }

}

 

  • CommandLineRunner
@Component
@RequiredArgsConstructor
public class InitDB {

    private final InitService initService;

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            initService.dbInit1();
        };
    }
}

 

  • CommandLineRunner
@Component
public class MyRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("~~~~~~~~~~~~~~~~ MyRunner.run");
    }
}
Comments