IT/SpringBoot
[Spring Boot] 서버 포트 변경하기
hi.anna
2023. 6. 25. 18:57
- application.properties 파일 수정하기
- Command Line 옵션 추가하기
- WebServerFactoryCustomizer 인터페이스 구현하기
- SpringApplication의 setDefaultProperties
1. application.properties 파일 수정하기
server.port = 8081
2. Command Line 옵션 추가하기
java -jar hello-spring.jar --server.port=8081
또는
java -jar -Dserver.port=8081 hello-spring.jar
3. WebServerFactoryCustomizer 인터페이스 구현하기
@Component
public class MyWebServerFactoryCustomizer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(7777);
}
}
프로그램으로 servlet container 정보를 설정하기 위해서는,
WebServerFactoryCustomizer 인터페이스를 bean으로 등록해야 합니다.
이, WebServerFactoryCustomizer를 통해서 ConfigurableServletWebServerFactory에 접근할 수 있고,
이를 통해 서버의 포트 번호를 포함한 Servlet Container의 정보를 변경할 수 있습니다.
4. SpringApplication의 setDefaultProperties
@SpringBootApplication
public class HelloSpringApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(HelloSpringApplication.class);
app.setDefaultProperties(Collections.singletonMap("server.port", "8081"));
app.run(args);
}
}
SpringApplication의 setDefaultProperties를 사용하여 port를 변경할 수 있습니다.
반응형