스프링부트 SpringApplication 커스터마이징과 Admin

최기곤·2021년 1월 12일
0

스프링부트

목록 보기
1/4

springApplication.run 실행순서

Application events are sent in the following order, as your application runs:

  1. An ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.

  2. An ApplicationEnvironmentPreparedEvent is sent when the Environment to2. be used in the context is known but before the context is created.

  3. An ApplicationContextInitializedEvent is sent when the ApplicationContext is prepared and ApplicationContextInitializers have been called but before any bean definitions are loaded.

  4. An ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions have been loaded.

  5. An ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.

  6. An AvailabilityChangeEvent is sent right after with LivenessState.CORRECT to indicate that the application is considered as live.

  7. An ApplicationReadyEvent is sent after any application and command-line runners have been called.

  8. An AvailabilityChangeEvent is sent right after with ReadinessState.ACCEPTING_TRAFFIC to indicate that the application is ready to service requests.

  9. An ApplicationFailedEvent is sent if there is an exception on startup.

Web Environment

스프링은 어플리케이션에따라 디폴트값으로 웹이아니면 AnnotationConfigApplicationContextg, 웹어플리케이션이면 AnnotationConfigServletWebServerApplicationContext를 사용된다.

어플리케이션 타입 설정방법

SpringApplication application = new SpringApplication(Application.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);

Type은 web-application이면 SERVELET or REACTIVE, web application이 아니라면 NONE.
를 사용할 수도 있다.

Accessing Application Arguments

애플리케이션 아규먼트 사용방법.
ApplicationArgument라는 빈을 통해 사용할 수 있다.
1번 방법.

@Autowired
Application Arguments arguments;

/**
* --hello=Hello -hello=World => ["Hello", World"] => "Hello, World"
* @return
*/
public String getMessage() {
	List<String> values = arguments.getOptionValues("hello");
    return values.stream().collect(Collectors.joining(",")); 
}

2번 방법.

@Value("${hello}")
String[] values;

* --hello=Hello -hello=World => ["Hello", World"] => "Hello, World"
* @return
*/
public String getMessage() {
	return Arrays.stream(values).collect(Collectors.joining(","));
}

Customizing the Banner with a gif image

resource폴더 밑에 banner.gif로 파일 추가.

렉심하니 그냥 쓰지말자~

ApplicationRunner or CommandLineRunner 사용하기

SpringApplication.run()에서 run이 끝나기 전에 호출하여 준다.

CommandLineRunner의 경우 아규먼트를 String 그대로 가져온다.

@Componet
@Order(0)
public class FirstRunner impolements CommandLineRunner {
	public void run(String... args) {
    	//Do Something...
    }
}

굳이 String args 날것을 쓰나...? ApplicationArguments라는 걸 쓰면 되지!
그래서 나온게 ApplicationRunner

@Component
@Order(0)
public class FirstRunner impolements ApplicationRunner {
	public void run(ApplicationArguments args) {
    	//Do Something...
    }
}

그리고 Runner가 여러개일 경우 각 클래스에 @Order()를 붙임으로 Order를 정할 수 있다.
수가 낮을 수록 먼저 실행됨.

Application Exit

ExitCodeGenerator 라는게 있따라.. 잘은 모르겠다.

Admin Features

어드민기능을 application.properties의 spring.application.admin.enable를 통해 키고 끌수있다.
spring.application.admin.enable=true
SpringApplicationAdminMXBean을 활성화 해준다. -> MBeanServer platform에 노출시켜준다.

그러면 feature를 jconsole을 통해서 볼 수 있다.
thread, memory, class, cpu등 애플리케이션의 매니지먼트를 볼 수 있다.

profile
놀면서 일하고 일하면서 놀고~ 해삐~

0개의 댓글