인프런/스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술

1일차 - Spring project 생성

곰돌이볼 2023. 8. 15. 13:16
  • 오늘 공부 분량
    • 섹션 0 ~ 섹션 1 : 

 

 

공부 요약


  • 프로젝트 생성하기
    • 인텔리제이의 spring initializr를 이용해서 생성함(아래의사이트에서 프로젝트 생성 후 다운받아서 사용 가능) 
    • Group과 Artifact를 변경해서 생성되는 프로젝트의 패키지명 변경 가능
    • 사용하는 라이브러리 : lombok, spring web, thymeleaf

 

  • build.gradle
    • sourceCompatibility 는 java version 표시
    • repositories { mavenCentral() } 코드는 설치받는 라이브러리 위치를 설정할 수 있음
      • 공개된 사이트인 mavenCentral에서 기본적으로 다운받지만 원한다면 다른 사이트 설정 가능
    • dependencies에서 필요한 라이브러리 설정을 할 수 있음
plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.14'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'hello'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '11' // java version
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

// 공개된 mavenCentral 사이트에서 dependencies에 있는 라이브러리 다운받음(설정)
// 원하면 특정 사이트 주소 입력 가능
repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' // html template engine
    implementation 'org.springframework.boot:spring-boot-starter-web' // web project
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test' // test library
}

tasks.named('test') {
    useJUnitPlatform()
}

 

  • .gitignore
    • github에 코드를 commit할 때, 올라갈 파일을 관리할 수 있는 설정 파일

 

  • 서버 동작시키기
    • main 메서드가 있는 HelloSpringApplication 클래스를 동작시키면 아래와 같은 로그가 나옴
    • @SpringBootApplication
      • tomcat이나 웹서버 내장되어 있음
    • 로그를 통해서 서버를 접속할 수 있는 주소를 알 수 있음
      • "Tomcat started on port(s): 8080 (http) with context path '' 
package hello.hellospring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloSpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloSpringApplication.class, args);
    }

}

 

  • 웹페이지 접속
    • 서버를 실행한 상태에서 웹 브라우저에 아래의 주소로 입력하면 다음과 같은 화면이 나옴 → 서버 정상 작동 중