Old Branch

Spring Framework Documentation

woolbro 2019. 6. 13. 18:15
반응형

Spring Framework에 대해서 공부해 보려고 합니다(진지).

 

우선, 참고한 문서는 Spring Framework Document를 참고했구요, 참고하면서 연습한 코드를 토대로 작성해보려고 합니다:)

 

같이 공부하면서 스프링에 대해서 알아가면 좋겠습니다 :)

 


Spring Framework ?

Spring makes it easy to create Java enterprise applications. It provides everything you need to embrace the Java language in an enterprise environment, with support for Groovy and Kotlin as alternative languages on the JVM, and with the flexibility to create many kinds of architectures depending on an application’s needs. As of Spring Framework 5.1, Spring requires JDK 8+ (Java SE 8+) and provides out-of-the-box support for JDK 11 LTS.....

 

The Spring Framework is divided into modules. Applications can choose which modules they need. At the heart are the modules of the core container, including a configuration model and a dependency injection mechanism. Beyond that, the Spring Framework provides foundational support for different application architectures, including messaging, transactional data and persistence, and web. It also includes the Servlet-based Spring MVC web framework and, in parallel, the Spring WebFlux reactive web framework.

 

뭐라는지 잘은 모르겠지만, 간단하게 요약하면

 

  • 스프링 프레임워크를 사용하면 Java 엔터프라이즈 애플리케이션을 쉽게 만들 수 있습니다.

  • Groovy ,Kotlin을 JVM 대체 언어로 지원합니다.

  • Spring Framework 5.1은 JDK8+ (JDK8이상)이 필요하고, JDK 11 LTS에 대해 지원합니다.

  • 스프링 프레임워크는 여러개의 모듈로 나뉘고, 선택하여 구축 할 수 있습니다

  • 메시징, 트랜잭션 데이터 및 지속성, 웹 등 다양한 애플리케이션 아키텍처 지원

  • Spring MVC 웹 프레임 워크와 Spring WebFlux 대응 프레임워크를 포함

라는 얘기네요..

 

Spring Framework는 자바 엔터프라이즈 개발을 편하게 해주는 오픈소스 애플리케이션 프레임워크 입니다.

즉, 애플리케이션 개발의 전 과정을 빠르고 편리하며 효율적으로 진행하는데 목표를 두는 프레임 워크라고 할 수 있습니다.

 

Spring Framework - MVC Model

MVC란, Controller(서블릿)와, Model(Java Bean) 그리고 View(JSP...etc)가 서로 상호작용을 하는 구조 입니다.

 

Model, View, Controller로 이루어지지 않은 JSP와 JavaBean만을 이용한 모델은 웹브라우저가 요청을 하면, JavaBean 에서 자원을 받아와서 웹브라우저에 전달하는 형식이었습니다.

 

JSP와 JavaBean 만을 사용한 모델

 

 

아래는 Model, View, Controller로 이루어진 MVC 모델입니다.

 

 

 

구조는 비슷하지만 컨트롤러라는 서블릿 컨테이너가 새로 생겨났고 , MVC(Model, View, Controller)가 서로 상호작용을 하며 월활한 구조를 가지고 있는 모습을 볼 수있습니다.

 

JSP는 View로 화면에 보여지는 역할을 하고 JavaBean은 Model로 서비스와 DAO(Database에 Access하는 트랜잭션 객체)을 담당, 서블릿으로 사용되는 Controller는 MVC를 가운데에서 조율하는 역활을 합니다.여기서 Resource는 자원이라고도 불리는데 모두들 알고 계시는 DB가 바로 Resource입니다.

 

JSP와 JavaBean을 사용한 기존에 사용 모델 방식이 개발속도가 빨랐지만 단점으로 코드가 복잡하고, 컨트롤러와의 구분이 애매해서 유지보수가 어렵다는 점이 있습니다. 반면 스프링 프레임워크를 사용하게 되면 Model, View, Controller 3가지 조화로 로직이 확실하게 분리되어 유지보수가 쉬워지게 됩니다.

 

Spring Framework - WebFlux

WebFlux는 Reactive System으로써, 복수개의 서비스로 이루어진 분산시스템을 동작하도록 보장 해 주는 시스템입니다.

 

WebFlux에 대한 Documentation을 참고(https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html) 하면,

 

Web on Reactive Stack

The original web framework included in the Spring Framework, Spring Web MVC, was purpose-built for the Servlet API and Servlet containers. The reactive-stack web framework, Spring WebFlux, was added later in version 5.0. It is fully non-blocking, supports

docs.spring.io

Why was Spring WebFlux created?

Part of the answer is the need for a non-blocking web stack to handle concurrency with a small number of threads and scale with fewer hardware resources. Servlet 3.1 did provide an API for non-blocking I/O. However, using it leads away from the rest of the Servlet API, where contracts are synchronous (Filter, Servlet) or blocking (getParameter, getPart). This was the motivation for a new common API to serve as a foundation across any non-blocking runtime. That is important because of servers (such as Netty) that are well-established in the async, non-blocking space.

The other part of the answer is functional programming. Much as the addition of annotations in Java 5 created opportunities (such as annotated REST controllers or unit tests), the addition of lambda expressions in Java 8 created opportunities for functional APIs in Java. This is a boon for non-blocking applications and continuation-style APIs (as popularized by CompletableFuture and ReactiveX) that allow declarative composition of asynchronous logic. At the programming-model level, Java 8 enabled Spring WebFlux to offer functional web endpoints alongside annotated controllers...

 

역시 영어라서.... 너무너무 어렵지만 특징을 정리하자면,

  • 동시성 처리

  • 비동기 - 논블록킹 리엑티브 개발에 사용

  • 효율적으로 동작하는 고성능 웹 어플리케이션 개발

  • 마이크로서비스 아키텍처에 적합

등이라고 합니다.

 

WebFlux는 2가지 개발방식을 지원하는데, 기존의 MVC방식을 지원 할 뿐만 아니라 새로운 함수형 모델 또한 지원합니다

따라서, WebFlux는 두가지 프로그래밍 모델 중에 선택 할 수 있는데 Annotated Controllers, Functional Endpoints 모델이 있습니다.

 

  • Annotated Controller : Spring MVC와 Spring Web 모듈의 동일한 주석을 기반으로 합니다. 모두 반응형 반환 유형을 지원하므로 결과를 구분하는 것이 쉽지 않습니다. 
  • Functional Endpoints : 람다 기반, 가볍고 기능적인 프로그래밍 모델 입니다. 응용프로그램이 요청을 라우트 하고 처리하는 데 사용 할 수 있는 라이브러리 또는 유틸리티 세트라고 할 수 있습니다. 

Functional Endpoints ,  Annotated Controller 의 가장 큰 차이점은 Functional Endpoints는 요청의 Annotated Controller 에서 처리 하는 것 처럼 선언하고 호출 되는 것이 아니라, 요청의 처리를 처음부터 끝까지 담당한다는 점입니다.

 

 

Sprint MVC or WebFlux??

 

Spring MVC versus Spring WebFlux

WebFlux Documentation에서는 다음과 같이 애기하고 있습니다.

 

만약 현재 Spring MVC 모델로 작성한 애플리케이션의 동작이 잘 된다면 변경할 필요가 없고, Java 8 람다 혹은 Kotlin과 함께 사용하기위한 기능적인 웹 프레임 워크에 관심이 있다면, Spring WebFlux 기능을 사용 할 수 있다고 합니다.

 

 

 

개인적으로 그냥 글만 읽어서는 사실 도저히 와닿지가 않네요... 일단 정리를 하긴 했지만, 이후에 코드를 작성하고 비교하면서 좀 더 확인 해 보아야 할 것 같습니다..!!