Spring

[Spring Boot] 서비스 추상화 (Portable Service Abstraction)

Joo.v7 2024. 11. 9. 14:38

1. 서비스 추상화 (PSA, Poratble Service Abstraction)

  • 개발자가 특정 환경이나 기술에 종속되지 않고, 일관된 방식으로 서비스를 사용할 수 있도록 추상화를 제공.
  • Interface로 사용하고, Interface의 구현부(implements)만 수정해서 사용.

2. Spring Boot Application의 내장 Web Server를 Tomat -> Jetty로 변경하기

(1) spring-boot-starter 에서 tomcat 관련 라이브러리를 제거.

(2) jetty 관련 라이브러리를 추가.

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.example</groupId>
    <artifactId>SpringBoot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>21</java.version>
    </properties>

<!-- starter 내부에서 직접 dependency 제거해도 됨 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

            <!--TODO-1 spring-boot-starter 에서 tomcat 관련 라이브러리를 제거해야한다. -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>

        </dependency>

        <!--TODO-2 jetty 관련 라이브러리를 추가해야한다. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 


3. 동작 원리

  • WebServer는 interface로 관리되고 있다.
  • 자동 구성: getWebServerFactory() 를 통해서 설정에 따른 적절한 Web Server를 가져온다.

 

ServletWebServerApplicationContext.java

public class ServletWebServerApplicationContext extends GenericWebApplicationContext
		implements ConfigurableWebServerApplicationContext {
...

	private volatile WebServer webServer;

...

	private void createWebServer() {
		WebServer webServer = this.webServer;
		ServletContext servletContext = getServletContext();
		if (webServer == null && servletContext == null) {
			StartupStep createWebServer = getApplicationStartup().start("spring.boot.webserver.create");
			ServletWebServerFactory factory = getWebServerFactory();
			createWebServer.tag("factory", factory.getClass().toString());
			this.webServer = factory.getWebServer(getSelfInitializer());
			createWebServer.end();
			getBeanFactory().registerSingleton("webServerGracefulShutdown",
					new WebServerGracefulShutdownLifecycle(this.webServer));
			getBeanFactory().registerSingleton("webServerStartStop",
					new WebServerStartStopLifecycle(this, this.webServer));
		}
		else if (servletContext != null) {
			try {
				getSelfInitializer().onStartup(servletContext);
			}
			catch (ServletException ex) {
				throw new ApplicationContextException("Cannot initialize servlet context", ex);
			}
		}
		initPropertySources();
	}
	
...

 


 

참고: 2024.11.06 - [NHN Java 백엔드 8기/Spring Boot] - [Spring Boot] 01. Spring Boot Core (2) - 자동 구성과 외부 구성

 

[Spring Boot] 01. Spring Boot Core (2) - 자동 구성과 외부 구성

5. Spring Boot 코드 살펴보기Spring Boot 동작 원리 알기 - 자동 생성 코드 살펴보기6. 자동 구성과 조건maven에 라이브러리 추가하기Auto Configuration(자동 구성) 동작 원리7. 외부 구성5. Spring Boot 코드 살펴

lightningtech.tistory.com

 

출처: https://nhnacademy.dooray.com/share/pages/ABFgz8KgRv62A85x9KClUw/3885034094817937429

 

Spring Boot Core

 

nhnacademy.dooray.com