๋ฐ์ํ
[Spring Boot] ApplicationRunner ๋ฑ๋ก ๋ฐฉ๋ฒ ์ ๋ฆฌ
1. ๋ณ๋์ ํด๋์ค์ @Component ๋ถ์ฌ์ ๋ฑ๋กํ๊ธฐ
import org.springframework.boot.ApplicationArguments;
import org.springframework.stereotype.Component;
@Component
public class ApplicationRunner implements org.springframework.boot.ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// ์ฝ๋ ์์ฑ
}
}
2. @Configuration ํด๋์ค์ @Bean์ผ๋ก ์ง์ ๋ฑ๋กํ๊ธฐ
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class ExceptionhandlerExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExceptionhandlerExampleApplication.class, args);
}
@Bean
public ApplicationRunner applicationRunner() {
return new ApplicationRunner() {
@Override
public void run(ApplicationArguments args) throws Exception {
// ์ฝ๋ ์์ฑ
}
};
}
}
3. Lambda๋ฅผ ์ด์ฉํด์ ๋ฑ๋กํ๊ธฐ
2๋ฒ ๋ฐฉ๋ฒ์์ lambda์์ผ๋ก ๋ณ๊ฒฝํ ๋ฐฉ๋ฒ์ด๋ค.
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class ExceptionhandlerExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExceptionhandlerExampleApplication.class, args);
}
@Bean
public ApplicationRunner applicationRunner() {
return args -> {
// ์ฝ๋ ์์ฑ
};
}
}
๋ฐ์ํ
๋๊ธ