프로젝트 생성
스프링 부트 기반 프로젝트를 만들어주는 사이트이다.
필요한 라이브러리를 가져오고 빌드하는 라이프사이클까지 관리해 주는 툴이다. 과거에는 Maven을 많이 사용했지만, 최근에는 Gradle을 많이 사용한다.
SNAPSHOT은 아직 만들고 있는 버전이라는 의미이다. M1은 정식 릴리즈된 버전은 아니다.
Group에는 보통 기업명을 사용한다. Artifact는 빌드되어 나온 결과물이다.
어떤 라이브러리를 가져와 사용할지 선택한다.
GENERATE를 클릭해 다운로드하고 압축을 푼 뒤
IDE에서 build.gradle을 열어준다.
프로젝트로 열기를 클릭한다.
기본적으로 main과 test폴더가 나눠져 있다. test 폴더에는 test 코드와 관련된 소스들이 들어간다.
sourceCompatibility는 자바 버전이다.
testImplementation는 test 라이브러리를 의미한다.
추가된 dependencies는 다운로드를 하여야 하는데 mavenCentral이라는 공개된 사이트에서 다운로드를 하게 된다.
repositories {
mavenCentral()
}
여기서 main 메서드를 실행시키면
2023-03-10T08:45:54.708+09:00 INFO 10852 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
이렇게 뜨면 성공한 것이다.
@SpringBootApplication
public class HelloSpringApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringApplication.class, args);
}
}
@SpringBootApplication 어노테이션으로 인해 스프링부트 애플리케이션이 실행되고, Tomcat 웹서버를 내장하고 있어서 이것을 자체적으로 띄우면서 SpringBoot가 같이 올라온다.
인텔리제이를 사용하면 자바를 직접 실행하는 것이 아니라 그래들을 통해서 실행될 때가 있는데
이렇게 바꾸면 그래들을 통하지 않고 인텔리제이에서 바로 실행돼서 빠르다.
라이브러리
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
직접 추가해 준 것은 타임리프와 웹 두 가지이고, 자동으로 들어온 테스트가 있다.
그런데 외부 라이브러리를 보면 굉장히 많은 것들이 있다.
Maven과 Gradle과 같은 빌드 도구들은 의존관계들을 관리해 준다.
(*)은 다른 곳 (여기서는 thymeleaf)에서 이미 가져와서 중복을 제거했다는 의미이다. tomcat은 내장되어 있다.
스프링 부트 라이브러리
- 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
- logback, slf4j
- spring-boot
테스트 라이브러리
- spring-boot-starter-test
- junit: 테스트 프레임워크
- mockito: 목 라이브러리
- assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
- spring-test: 스프링 통합 테스트 지원
View 환경설정
welcome 페이지 만들기
<!DOCTYPE HTML>
<html>
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello
<a href="/hello">hello</a>
</body>
</html>
그리고 서버를 실행한 뒤 localhost:8080으로 가면
이렇게 뜬다.
스프링이 제공하는 기능은 어마어마하다. 그래서 필요한 것을 잘 찾을 수 있어야 한다.
Spring | Home
Cloud Your code, any cloud—we’ve got you covered. Connect and scale your services, whatever your platform.
spring.io
Reference Doc에 들어가면 사용법을 볼 수 있다.
thymeleaf 템플릿 엔진
Thymeleaf
Integrations galore Eclipse, IntelliJ IDEA, Spring, Play, even the up-and-coming Model-View-Controller API for Java EE 8. Write Thymeleaf in your favourite tools, using your favourite web-development framework. Check out our Ecosystem to see more integrati
www.thymeleaf.org
https://spring.io/guides/gs/serving-web-content/
Spring | Home
Cloud Your code, any cloud—we’ve got you covered. Connect and scale your services, whatever your platform.
spring.io
Spring Boot Features
Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest
docs.spring.io
HelloController를 생성한다.
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";
}
}
hello.html을 생성한다.
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
</body>
</html>
model.addAttribute("data", "hello!!");
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
${data}에 컨트롤러에서 이름이 data인 속성의 값을 넘겨준다.
빌드하고 실행하기
cmd에서 프로젝트가 있는 폴더로 이동한 뒤
1. gradlew
2. gradlew build
3. cd build/libs (build의 libs 폴더로 이동)
4. dif (폴더 목록 확인)
5. java -jar hello-spring-0.0.1-SNAPSHOT.jar
'SpringBoot > 기초' 카테고리의 다른 글
스프링 DB 접근 기술(1) - 순수 JDBC (0) | 2023.03.10 |
---|---|
회원 관리 예제 - 웹 MVC 개발 (0) | 2023.03.10 |
스프링 빈과 의존관계 (0) | 2023.03.10 |
회원 관리 예제 - 백엔드 개발 (0) | 2023.03.10 |
스프링 웹 개발 기초 (0) | 2023.03.10 |