스프링 부트, 입문!

스프링 부트, 입문!

쉽고 빠르게 배우는, 스프링 부트 첫걸음!

10 롬복(lombok)과 리팩터링(refactoring)

# 롬복과 리팩터링 ## 미션 롬복을 활용하여 기존 코드를 리팩터링 하시오. ![홍팍-스프링-부트-입문--미션](http://drive.google.com/uc?export=view&id=1dtjRddlxgCM173BN6aLjPHq3JLGULwf6) ## 02:32 롬복 라이브러리 추가 #### firstproject/build.gradle ``` plugins { id 'org.springframework.boot' version '2.6.6' // 스프링 부트 버전 id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' // JDK 버전 repositories { mavenCentral() } dependencies { // 10강: 롬복 추가 compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-mustache' implementation 'org.springframework.boot:spring-boot-starter-web' runtimeOnly 'com.h2database:h2' testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() } ``` #### ⚠️ 최신 IntelliJ는 롬복 플러그인을 자체적으로 포함합니다. ## 06:17 리팩터링, 코드 개선하기 #### ../dto/ArticleForm ``` package com.example.firstproject.dto; import com.example.firstproject.entity.Article; import lombok.AllArgsConstructor; import lombok.ToString; @AllArgsConstructor @ToString public class ArticleForm { private String title; private String content; public Article toEntity() { return new Article(null, title, content); } } ``` #### ../entity/Article ``` package com.example.firstproject.entity; import lombok.AllArgsConstructor; import lombok.ToString; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity @AllArgsConstructor @ToString public class Article { @Id @GeneratedValue private Long id; @Column private String title; @Column private String content; } ``` ## 09:35 로그 남기기 #### ../controller/ArticleController ``` package com.example.firstproject.controller; import com.example.firstproject.dto.ArticleForm; import com.example.firstproject.entity.Article; import com.example.firstproject.repository.ArticleRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @Controller @Slf4j // 로깅을 위한 롬복 어노테이션 public class ArticleController { @Autowired private ArticleRepository articleRepository; @GetMapping("/articles/new") public String newArticleForm() { return "articles/new"; } @PostMapping("/articles/create") public String createArticle(ArticleForm form) { log.info(form.toString()); // println() 을 로깅으로 대체! Article article = form.toEntity(); log.info(article.toString()); // println() 을 로깅으로 대체! Article saved = articleRepository.save(article); log.info(saved.toString()); // println() 을 로깅으로 대체! return ""; } } ``` ## 🔥 구글링 훈련하기 - java lombok 사용법 - 리팩터링이란 - 로깅이란 - @AllArgsConstructor - @ToString - @Slf4j