728x90
반응형
작성일자 : 2020.02.23
목표 : Spring App 구동 후 특정 작업 실행
1. ApplicationRunner 사용 (Parameter 활용 가능)
package com.example.demo.test;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class RunAfterApplicationStart implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
System.out.println("ApplicationRunner Example");
System.out.println(Arrays.toString(args.getSourceArgs()));
}
}
2. CommandLineRunner 사용 (Parameter 활용 가능)
package com.example.demo.test;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class RunAfterApplicationStart implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("CommandLineRunner Example");
System.out.println(Arrays.toString(args));
}
}
3. EventListener 사용
package com.example.demo.test;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class RunAfterApplicationStart {
@EventListener(ApplicationReadyEvent.class)
public void test() {
System.out.println("ApplicationReadyEventListener Example");
}
}
728x90
반응형
'Java' 카테고리의 다른 글
[Spring] Web Cache 적용 (0) | 2020.12.19 |
---|---|
[SpringBoot] H2 연동 (0) | 2020.07.21 |
Stream Example (0) | 2020.01.05 |
리스트 순환 중 아이템 삭제 (0) | 2019.12.08 |
[Spring] Cache 적용 (0) | 2019.09.28 |