๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring Boot] ApplicationRunner ๋“ฑ๋ก ๋ฐฉ๋ฒ• ์ •๋ฆฌ

by Leica 2020. 3. 26.
๋ฐ˜์‘ํ˜•

[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 -> {

			// ์ฝ”๋“œ ์ž‘์„ฑ
        };
    }
}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€