
Java Spring을 간단하게 공부하여 API를 구현해봤지만,
개념을 제대로 숙지하지 않은 채 코드를 작성하는 것은 내가 학습하던 스타일도 아닐 뿐더러
이대로 가다가는 효율적인 구조를 정립하는 것도 못하고
그 구조를 기반으로 효율적인 코드를 작성하는 것도 불가능할거라고 생각하여 개념 공부부터 하기로 했다.
요 며칠간 학습한 지식들을 기반으로 작성 해보았다.
📚 개요
Java는 공부를 해뒀으니
Spring으로 넘어가서 Spring의 전체적인 흐름과 구조를 학습하고
Servlet, Tomcat 등등 디테일한 부분을 챙겨보자
이번 작성 글은 용어 정리가 되시겠다.
최대한 Javascript의 개념과 엮어서 이해하기 쉽도록 작성했다. ( 나중에 내가 볼거니까 )
🎬 Get Started
우선 뭐하는 녀석들인지부터 알아야한다.
- Spring : 대규모 애플리케이션 개발에 필요한 여러 기능을 제공하는 프레임워크이다. 대표적인 기능으로는 DI, AOP, IoC 등이 있다.
- Spring boot : Spring 을 기반으로 만들어진 웹 프레임 워크이다. Spring으로 웹 프로젝트를 만들려면 이런저런 설정을 해야하는데, 이런 기초적인 초기 세팅들을 설정해준다.
🛠 구조
- Model 1 : JSP 등등 템플릿 엔진들이 비즈니스 로직과 정적파일을 서빙하는 웹서버의 역할을 전부 수행한다.
비즈니스 로직이 JSP 같은 곳에 담겨 브라우저에 노출 될 수 있기때문에 보안이 정말정말 취약하고 해당 템플릿 엔진에 맞춰서 코드를 작성해야하기 때문에 유지보수 측면으로도 굉장히 안좋다.
- Model 2 : 우리가 아는 흔한 MVC 패턴이랑 비슷하다고 보면 편하다. 그리고 CSR을 곁들인.. 한방에 이해하기 좋은 그림을 그려주도록 하겠다.

요런 식의 구조를 띈다
- Spring MVC : MVC 가 우리가 알던 MVC가 아니다. 단순히 Model - View - Controller 에 맨 앞단의 servlet과 resolver, handlerMapping, adapter 등이 추가된 형태이다.
순서도는 보이는 대로다.

이 밖에도
- MVVM
- MVP
등이 있지만 보통 데스크탑 브라우저에서 다루지 않기에 스킵한다.
데이터 타입 (?)
- POJO : JAVA 로 작성된 올드한 객체이다. 이름 자체가 Plain Old Java Object 니까 말 다했다.
아래와 같은 느낌이 POJO이다.
다른 기술을 속하지 않고 정말로 순수하게 자바로만 이뤄진 오브젝트를 말한다.
public class UserDTO {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
//... 중략
}
- JAVA Beans :
Java 클래스를 만들 때의 규약이다.
여러가지 다른 객체들을 하나의 객체에 담기 위함이다.
지켜야할 규약이 몇가지 있는데
1. 모든 클래스의 property(필드)는 private하며, getter, setter 메소드로 제어한다. (캡슐화)
2. 인자가 없는 public 생성자가 있어야한다. (defalut 생성자는 필수)
3. Serializable 인터페이스를 상속받아야한다. (주고 받을려면 Serializable(직렬화) 를 해야 용이)
- JSON : 속성-값 쌍, 배열 자료형 또는 기타 모든 시리얼화 가능한 값 또는 "키-값 쌍"으로 이루어진 데이터 오브젝트를 전달하기 위해 인간이 읽을 수 있는 텍스트를 사용하는 개방형 표준 포맷이다.
{
"userInfo": {
"name":"안병현",
"age":28,
"properties":["잘 웃음","요즘 부쩍 살찜", "조카 태어나서 기분좋음"
}
}
- ModelAndView : Controller가 ViewResolver 에게 View를 보낼때 데이터를 담아 보낼 수 있도록 하는 객체이다.
Default Constructor와 클래스 변수
public class ModelAndView {
@Nullable
private Object view;
@Nullable
private ModelMap model;
@Nullable
private HttpStatusCode status;
private boolean cleared = false;
public ModelAndView() {
}
- Model : 데이터를 view 에게 전달하기 위해서 쓰이는 인터페이스
package org.springframework.ui;
import java.util.Collection;
import java.util.Map;
import org.springframework.lang.Nullable;
public interface Model {
Model addAttribute(Object attributeValue);
Model addAllAttributes(Collection<?> attributeValues);
Model addAllAttributes(Map<String, ?> attributes);
Model mergeAttributes(Map<String, ?> attributes);
boolean containsAttribute(String attributeName);
@Nullable
Object getAttribute(String attributeName);
Map<String, Object> asMap();
}
- ModelMap : Model 인터페이스로 구현한 모델맵 객체이다. mvc 패턴으로 만들어진 데이터들을 view 에게 전달하기 위해서 쓰인다. Model 과는 개인적인 취향차이로 쓰고 안쓰고로 차이난다고 한다.
package org.springframework.ui;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.core.Conventions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@SuppressWarnings("serial")
public class ModelMap extends LinkedHashMap<String, Object> {
public ModelMap() {
}
public ModelMap(String attributeName, @Nullable Object attributeValue) {
addAttribute(attributeName, attributeValue);
}
public ModelMap(Object attributeValue) {
addAttribute(attributeValue);
}
public ModelMap addAttribute(String attributeName, @Nullable Object attributeValue) {
Assert.notNull(attributeName, "Model attribute name must not be null");
put(attributeName, attributeValue);
return this;
}
public ModelMap addAttribute(Object attributeValue) {
Assert.notNull(attributeValue, "Model object must not be null");
if (attributeValue instanceof Collection<?> collection && collection.isEmpty()) {
return this;
}
return addAttribute(Conventions.getVariableName(attributeValue), attributeValue);
}
public ModelMap addAllAttributes(@Nullable Collection<?> attributeValues) {
if (attributeValues != null) {
for (Object attributeValue : attributeValues) {
addAttribute(attributeValue);
}
}
return this;
}
public ModelMap addAllAttributes(@Nullable Map<String, ?> attributes) {
if (attributes != null) {
putAll(attributes);
}
return this;
}
public ModelMap mergeAttributes(@Nullable Map<String, ?> attributes) {
if (attributes != null) {
attributes.forEach((key, value) -> {
if (!containsKey(key)) {
put(key, value);
}
});
}
return this;
}
public boolean containsAttribute(String attributeName) {
return containsKey(attributeName);
}
@Nullable
public Object getAttribute(String attributeName) {
return get(attributeName);
}
}
🔗 출처
ModelAndView, Model, ModelMap 내용
https://ooeunz.tistory.com/101
ModelAndView, Model, ModelMap 소스
org.springframework
나머지 내용
Spring | Home
Cloud Your code, any cloud—we’ve got you covered. Connect and scale your services, whatever your platform.
spring.io
나머지 그림
수제
'B4 Junior' 카테고리의 다른 글
| ⚙️ CentOS7 GUI 에서 CLI 로 (0) | 2023.04.07 |
|---|---|
| 📚 JAVA 기본 개념 - 변수 (0) | 2023.04.05 |
| 📃 PDF 파일 안 읽힘 ( 유형 - 알 수 없는 파일 ) (2) | 2023.03.10 |
| Cloudfront - S3 - EC2 도입 ( FE 배포과정 ) (0) | 2023.03.01 |
| Cloudfront - S3 - EC2 도입 ( BE 배포과정 ) (0) | 2023.03.01 |
백엔드는 못말려
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!