본문 바로가기
Study/SPRING

Spring Boot - 시작하기 및 환경설정

by 멘탈은안녕하신가 2020. 9. 23.
728x90
반응형
  • Boot, Gradle, yml 사용
  • Spring Boot 시작 아래 한가지 방법으로 시작

1. start.spring.io 활용

  • 원하는 패키지 설정 GENERATE CTRL + ENTER 선택, 다운로드 압축 패키지를 압축 해제 툴에 IMPORT.

 

2. Eclipse 활용

  • File > New > Spring Starter Project 선택

  • 항목 작성 Next, 원하는 Dependency 우선 설정할 있음

  • 기본적인 Spring 기능을 활용하기 위해선 Spring Web 선택, Next

  • 기본값으로 두고 Finish 클릭, 프로젝트가 생성됨

  • 기본적으로 생성되는 build.gradle

  • 프로젝트 생성시 Gradle Refresh 실행해주자(Maven Update 동일한 역할)

  • 기본적인 프로젝트 구조
    • boot 패키지명에 따라 기본적인 Application class 생성된다(여기서는 BootStudyApplication.java).
    • Boot 실행을 위해 main 메소드가 필요.

  • main 클래스에 설정되어 있는 @SpringBootApplication 어노테이션은 아래와 같은 역할을 한다.
    1. 현재 설정된 클래스가 있는 패키지를 최상위 패키지로 인식 후 ComponentScan 수행
    2. 아래 3가지 어노테이션의 특징을 하나로 합친 어노테이션
      • @EnableAutoConfiguration : Boot 자동화 기능을 활성화 시킴
      • @ComponentScan : 패키지 application Component 어디에 위치해 있는지 검사(Bean 검색)
        • xml 설정파일에 ComponentScan 일일히 지정해 주지 않아도 .
        • @Service 어노테이션을 지정한 Componet 자동으로 사용 가능
      • @Configuration : Bean 대해 Context 추가하거나 특정 클래스를 참조할 있음
  • Spring Framework 다르게 Boot에선 webapp/WEB-INF 사용하지 않고 resources view 파일과 정적 파일을 함께 담고 있음.
    • static 정적파일을 생성, templates view파일을 생성한다.
    • 하지만 기본 root static 하단을 바라보고 있어 localhost:8080 실행 static/index.html 실행
    • Boot jsp대신 thymeleaf 템플릿 엔진을 기본 엔진으로 사용한다.
      • Dependency 아래 내용을 추가

//https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf

compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.3.2.RELEASE'

 

  • yml 아래 내용 추가

spring:

  thymeleaf:

    cache: false

    enabled: true

    encoding: UTF-8

    content-type: text/html

    check-template: true

    check-template-location: true

    prefix: classpath:/templates/

    suffix: .html

    mode: HTML5

 

728x90
반응형

댓글