본문 바로가기
Study/Spring

[Spring] Spring 시작하기

by 나아가는 2023. 9. 24.
반응형

1. Spring 시작하기

  • 개발환경세팅
    • spring 2.* java 11버전 권장
    • spiring 3.* java 17이상 버전 권장
    • java 20 , spring 3.1.3
    • depandencies
      • spring web
      • thyeleaf > html 만들어주는 템플릿
  • main 실행
    • SpringApplication.run(클래스명.class)
    • spring 에서 톰캣 웹서버를 내장하고있어서 톰캣 웹서버를 자체적으로 띄우면서 실행

2. 라이브러리 살펴보기

  • maven, gradle 같은 빌드 툴들은 의존관계를 관리해준다.
  • start-web 을 설치했기 때문에 관련된 의존 라이브러리가 함께 설치된다.
  • 라이브러리에서 웹서버(Tomcat)를 내장하고 있어서 코드를 실행시키면 자동으로 웹서버를 띄워준다. 

2.1 스프링부트 라이브러리

  • spring-boot-starter-web
    • spring-boot-starter-tomcat: 톰캣(웹서버)
    • spring-webmvc: 스프링 웹 MVC
  • spring-boot-starter-thymeleaf : 타임리프템플릿 엔진(View)
  • spring-boot-starter(공통): 스프링부터 + 스프링 코어 + 로깅
    • spring-boot
      • spring-core
    • spring-boot-starter-logging
      • 실무에서는 print 를 쓰지않고 logging으로 오류를 저장하고 관리한다.
      • slf4j, logback > 대표적인 로그 라이브러리, 두가지 조합을 많이 사용한다.
      • logback 로그 구현체로 사용, 성능도 빠르고 지원하는 기능이 좋다.

2.2 테스트 라이브러리

  • spring-boot-starter-test
    • 테스트 프레임워크
      • junit
    • 테스트를 편리하게 하도록 도와주는 라이브러리
      • mockito, assertj
    • sprint-test: 스프링 통합 테스트 지원
    •  

 

3. View 환경설정

welcom page 만들기

  • spring은 java 전반적인 엔터프라이즈 기능 제공한다.
  • spring boot 는 spring 을 감싸서 편리하게 사용할 수 있게 해주는 것이다.
  • 기능이 매우 많기 때문에 필요한것을 찾는 능력 중요하다.

/hello.hellostpring/controller/helloController.java

package hello.hellospring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;

@Controller
public class helloController {

    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data", "hello!!");
        return "hello";
    }
}
  • return 값을 hello를 반환하면 templates 에서 hello화면을 찾아서 처리한다
  • resources:templates/ + {ViewName}+.html
spring-boot-devtools 라이브러리를 추가하면, html 파을을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능하다

/resources/templates/hello.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
<p th:text="'안녕하세요.'+${data}">안녕하세요. 손님</p>
</body>
</html>
반응형

'Study > Spring' 카테고리의 다른 글

springboot 에서 어노테이션기반 MyBatis 적용하기 (xml 사용하지 않고)  (0) 2023.12.16
[Spring]Builder  (0) 2023.10.06
[Spring] AOP 예제  (0) 2023.09.25
[Spring] 3. AOP  (0) 2023.09.24
[Spring] 2. DI(Dependancy Injection)란?  (1) 2023.09.24