[번역] Spring Boot 웹 애플리케이션 구성

in spring •  3 years ago 

baeldung.com/spring-boot-application-configuration/를 번역함.



1. 개요

Spring Boot는 많은 일을 할 수 있습니다. 이 자습서에서는 Boot에서 더 흥미로운 몇 가지 구성 옵션을 살펴보겠습니다.

2. 포트 번호

주요 독립 실행형 응용 프로그램에서 기본 HTTP 포트는 기본적으로 8080입니다. 다른 포트를 사용하도록 Boot를 쉽게 구성할 수 있습니다 .

server.port=8083

그리고 YAML 기반 구성의 경우:

server:
port: 8083

또한 프로그래밍 방식으로 서버 포트를 사용자 지정할 수 있습니다.

@Component
public class CustomizationBean implements
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

@Override
public void customize(ConfigurableServletWebServerFactory container) {
container.setPort(8083);
}
}

3. 컨텍스트 경로

기본적으로 컨텍스트 경로는 "/"입니다. 그것이 이상적이지 않고 변경해야 하는 경우 - / app_name 과 같은 것으로 속성을 통해 이를 수행하는 빠르고 간단한 방법은 다음과 같습니다.

server.servlet.contextPath=/springbootapp

그리고 YAML 기반 구성의 경우:

server:
servlet:
contextPath:/springbootapp

마지막으로 – 프로그래밍 방식으로도 변경을 수행할 수 있습니다.

@Component
public class CustomizationBean
implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

@Override
public void customize(ConfigurableServletWebServerFactorycontainer) {
container.setContextPath("/springbootapp");
}
}

4. 화이트 라벨 오류 페이지

Spring Boot BasicErrorController는 구성에서 사용자 정의 구현을 지정하지 않으면 자동으로 빈을 등록 합니다.

그러나 이 기본 컨트롤러는 물론 구성할 수 있습니다.

public class MyCustomErrorController implements ErrorController {

private static final String PATH = "/error";

@GetMapping(value=PATH)
public String error() {
return "Error haven";
}
}

5. 오류 메시지 사용자 정의

Boot는 기본적으로 /error 매핑을 제공 하여 합리적인 방식으로 오류를 처리합니다.

보다 구체적인 오류 페이지를 구성하려는 경우 오류 처리를 사용자 정의하기 위해 균일한 Java DSL을 잘 지원합니다.

@Component
public class CustomizationBean
implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

@Override
public void customize(ConfigurableServletWebServerFactorycontainer) {
container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
container.addErrorPages(new ErrorPage("/errorHaven"));
}
}

여기서는 /400 경로와 일치하도록 잘못된 요청 을 처리 하고 공통 경로와 일치하도록 다른 모든 것을 처리했습니다.

그리고 매우 간단한 /errorHaven 구현:

@GetMapping("/errorHaven")
String errorHeaven() {
return "You have reached the haven of errors!!!";
}

산출:

You have reached the haven of errors!!!

6. 프로그래밍 방식으로 부팅 응용 프로그램 종료

SpringApplication을 사용하여 프로그래밍 방식으로 Boot 앱을 종료할 수 있습니다 여기에는 ApplicationContext 및 ExitCodeGenerator 의 두 인수 를 사용하는 정적 exit() 메서드가 있습니다 .

@Autowired
public void shutDown(ExecutorServiceExitCodeGenerator exitCodeGenerator) {
SpringApplication.exit(applicationContext, exitCodeGenerator);
}

이 유틸리티 방법을 통해 앱을 종료할 수 있습니다.

7. 로깅 수준 구성

Boot 응용 프로그램에서 로깅 수준을 쉽게 조정할 수 있습니다 . 버전 1.2.0부터 기본 속성 파일에서 로그 수준을 구성할 수 있습니다.

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

그리고 표준 Spring 앱과 마찬가지로 클래스 경로에 사용자 정의된 XML 또는 속성 파일을 추가하고 pom에 라이브러리를 정의하여 Logback , log4j , log4j2 등과 같은 다양한 로깅 시스템을 활성화할 수 있습니다 .

8. 새 서블릿 등록

임베디드 서버의 도움으로 애플리케이션을 배포하는 경우 기존 구성에서 빈으로 노출 하여 부트 애플리케이션  새 서블릿을 등록할 수 있습니다 .

@Bean
public HelloWorldServlet helloWorld() {
return new HelloWorldServlet();
}

또는 ServletRegistrationBean을 사용할 수 있습니다 .

@Bean
public SpringHelloServletRegistrationBean servletRegistrationBean() {

SpringHelloServletRegistrationBean bean = new SpringHelloServletRegistrationBean(
new SpringHelloWorldServlet(), "/springHelloWorld/*");
bean.setLoadOnStartup(1);
bean.addInitParameter("message", "SpringHelloWorldServlet special message");
return bean;
}

9. 부트 애플리케이션에서 Jetty 또는 Undertow 구성

Spring Boot 스타터는 일반적으로 Tomcat을 기본 임베디드 서버로 사용 합니다 . 변경해야 하는 경우 Tomcat 종속성을 제외하고 대신 Jetty 또는 Undertow를 포함할 수 있습니다.

부두 구성

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
JettyEmbeddedServletContainerFactory jettyContainer =
new JettyEmbeddedServletContainerFactory();

jettyContainer.setPort(9000);
jettyContainer.setContextPath("/springbootapp");
return jettyContainer;
}

Undertow 구성

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
UndertowEmbeddedServletContainerFactory factory =
new UndertowEmbeddedServletContainerFactory();

factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(io.undertow.Undertow.Builder builder) {
builder.addHttpListener(8080, "0.0.0.0");
}
});

return factory;
}

10. 결론

이 빠른 기사에서는 더 흥미롭고 유용한 Spring Boot 구성 옵션에 대해 살펴보았습니다 .

물론 참조 문서에는 Boot 앱을 구성하고 필요에 맞게 조정할 수 있는 훨씬 더 많은 옵션이 있습니다. 이것들은 제가 찾은 더 유용한 것 중 일부일 뿐입니다.



Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!