[inflearn/spring 입문] section.1 / 4-5강 요약 및 이해하기
spring libraries
External Libraries폴더를 열어보면 굉장히 많은 외부라이브러리들이 존재.
과거에는 tomcat 및 다른 라이브러리들을 일일이 추가하는 작업을 해줘야 했지만
spring boot 를 쓰면 외부 라이브러리들을 한번에 땡겨올 수 있다.
tomcat(서버)도 내장서버(embed)로 설치되어 있다.
경로는 (Intellj > Window) alt 두번 클릭(alt+alt)시, 또는
화면하단의 표시 클릭시 창 오른쪽에 떠있는 Gradle 이 보이고 클릭
옆은 기존 상태로 돌아가고 싶을시 클릭
Gradle 이 수많은 라이브러리들을 당겨온다. Dependecies들이 존재
test관련 라이브러리들도 존재
test 라이브러리 정리 : spring-boot-starter-test
- junit : 테스트 프레임워크
- mockito : 목 라이브러리
- assertj : 테스트 코드를 좀더 편하게 작성하게 도와주는 라이브러리
- spring-test : 스프링 통합 테스트 지원
thymeleaf > starter > core, loggin, configure 등 모든걸 땡겨온다.
이중에 loggin 이란?
system.out.println으로 출력이 아닌 log로 남겨야 된다.
모든 log파일 관리하기 위해서 (서버의 모든 기록을 관리하는 차원)
더 공부할 내용 logback, slf4j (추가예정)
logback : 로깅 프레임워크 중 하나로 slf4j의 구현체
3가지 모듈로 나뉜다.
core / classic / access
- logback-core : appender와 layout 인터페이스가 속하는 모듈
- logback-classic : logback-core에서 확장된 모듈, logback-core와 slf4j API 라이브러리를 가진다. logger 클래스가 속하는 모듈.
- logback-access : servlet container와 통합되어 HTTP 액세스에 대한 로깅 기능을 제공한다. 웹 애플리케이션 레벨이 아닌 컨테이너 레벨에서 설치되어야 한다.
구조 : tag로 구성
- Logger : 로그의 주체, 메세지 전달, 특정 패키지 안의 특정 레벨이상인 것에 대해 출력
- Appender : 어디에 출력할지에 대해 기술 : console / file / DB appender
- Encoder : 어떻게 출력할지에 대해 기술
로그 레벨
Logback 은 5단계의 로그 레벨을 가진다.
심각도 수준은 Error(예상치 못한 심각한 문제와 즉시 조취를 취할 수준) > Warn(로직상 유효성 확인, 주의)> Info(운영에 참고 사항 및 중요 비즈니스 프로세스 완료) > Debug(개발 단계, SQL 로깅 가능) > Trace(모든 레벨에 대한 로깅 추적 가능) 이다.
배포단계에서는 Debug , Trace 레벨의 로깅 사용하지 않는게 좋다.
slf4j : 로깅 프레임 워크에 대한 추상화(인터페이스) 역할을 하는 라이브러리
단독 사용하지 않는다. SLF4J를 의존하는 클라이언트 코드에서는 실제 구현을 몰라도 된다. (의존 관계 역전 법칙)
spring boot port 번호 설정
src - main- resources - application.properties 파일에서
server.port = 8888
위와 같이 입력해준다.
spring 파일 설정
src - main - java 에는 controller
src - main - resources - static 에는 index.html
src - main - resources - templates 에는 view파일들
package Hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model){
model.addAttribute("data", "hello!!");
return "hello";
}
}
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p th:text="'안녕하세요,, ' + ${data}" > 안녕하세요. 손님</p>
</body>
</html>
1. 클라이언트가 localhost:8888/hello를 url에 치면
2. 내장 tomcat 서버를 통해 /hello인 hello.html 파일이 보인다.
3. 보이기까지 spring container에서 HelloController 파일에서 매핑된(GetMapping) 파일을 찾기 위해 return을 받고
4. viewResolver가 return값으로 templates폴더에 있는 hello(viewName)를 찾아
5. HelloController 파일에서 model을 써서 (key값인)data : (value값인) hello!! 가 만나
5. key와 value가 매칭된 html이 클라이언트의 웹브라우저에 표현된다.
주의 : <html xmlns:th="http//www.thymeleaf.org"> 꼭 써주기 와 body의 p태그 <p th:text=> </p> 가 이어지게 된다.
끝
접은글은 내가 느끼는 부분
기존의 spring freamwork를 배웠던 기억과 맞물려 아직까지 intellj 쓰는 법 외에 엄청나게 어렵지는 않았다.
Reference (logback과 slf4j 관련)
- https://tecoble.techcourse.co.kr/post/2021-08-07-logback-tutorial/
- https://oingdaddy.tistory.com/78
- https://livenow14.tistory.com/64
- https://livenow14.tistory.com/63