[inflearn] 스프링 부트 개념과 활용 2.스프링 부트 시작하기 | [inflearn] Spring Boot Concepts and Utilization 2. Getting Started with Spring Boot
1. Spring Boot 소개
1-1. Spring Boot Start
특징
토이를 만드는게 아니라 제품수준의 어플리케이션을 만들때 도와주는 툴.
opinated view : 스프링 부트가 갖고있는 컨벤션을 의미한다 (널리 사용되는 설정)
Spring platform에 대한 기본 설정 뿐만아니라 다른 library에 대한 설정(tomcat)도 기본적으로 해준다
목표
- 모든 스프링 개발을 할 때 더 빠르고 더 폭넓은 사용성을 제공한다.
- 일일히 설정하지 않아도 convention으로 정해져있는 설정을 제공한다. 하지만 우리의 요구사항에 맞게 이런 설정을 쉽고 빠르게 바꿀 수 있다.(스프링 부트를 사용하는 이유)
- non-fucntional 설정도 제공해 준다. 비즈니스로직 구현에 필요한 기능 외에도 non-functional feature도!
- XML 사용하지 않고, code generation도 하지 않는다.
Spring 루 : 독특하게 code generation을 해주는데 지금은 잘 사용되지 않는다. generation을 안해서 더 쉽고 명확하고 커스터마이징하기 쉽다. > spring boot의 bb
System Requirements
Spring boot 는 java 8 이상을 필요로 한다.
지원하는 servletContainer로는 tomcat, jetty Undertow가 있다.
2. Spring Boot 시작하기
Intellij ultimate를 사용하면 Spring boot initializer가 있으나, community 버전은 없다. 따라서 자신이 원하는 build tool을 이용해서 만들어 주면된다 Spring boot initializer를 이용하지 않고, 프로젝트 생성하는 법 을 공부 할 것이다.
2-1. gradle project에서 시작
auto import OK (build.gradle 파일 변경할 때 마다 바로바로 변경 : dependency 추가 등)
spring.io > project > spring boot > Learn > Reference Doc > Gradle Installation
Spring Boot Reference Guide
This section dives into the details of Spring Boot. Here you can learn about the key features that you may want to use and customize. If you have not already done so, you might want to read the "Part II, “Getting Started”" and "Part III, “Using Spring Boot
docs.spring.io
build.gradle
build.gradle 파일을 입력해준다.
plugins {
id 'org.springframework.boot' version '2.0.3.RELEASE'
id 'java'
}
의존성 관리와 매우 관련이 있는 설정이다.
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
일반적으로 프로젝트는 하나이상의 "starter"에 대한 의존성을 선언한다.
spring boot는 의존성 선언을 간소화했으며, jar를 생성하는데 유용한 Gradle 플러그인을 제공한다.
initial build.gradle 파일
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'spring-boot-getting-started'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
SpringBootApplication.java
package com.jyami;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String args[]){
SpringApplication.run(Application.class, args);
}
}
SpringBootApplication 어노테이션을 사용해서, SpringApplication을 run하는 메소드 호출
Spring MVC가 돌아가려면 여러 dependency가 필요한데, 어떻게 하여 수많은 의존성들이 들어왔는가?
mvc앱을 설정해야하는데 (bean, tomcat .. )
: 이게 @SpringBootApplication에 설정되어있다. > InableAutoCompletecation
Intellij 설정에서
Build, Execution, Deployment > Compiler > Annotation Processors에 들어가
Enable annotation processing에 체크해줘야 gradle로 build한 annotation들을 사용할 수 있다.
Run (실행하기)
Run을 하고 log를 보면 벌서 Tomcat이 8080 port에서 실행되고 있음을 알 수 있고
http://localhost:8080 을 띄어보면, tomcat web application이 동작함을 알 수 있다. (error이긴 하지만)
build (빌드하기)
gradle build
이 package를 build한다. java프로젝트이므로 jar파일이 생성되고, 이 jar파일을 생성한다
java -jar build/libs/spring-boot-getting-started-0.1.0.jar
jar 파일을 실행하면, 아까와 같은 spring web application이 동작하게 된다.
2-2. 웹으로 Spring Boot project 시작
원하는 build 형태의 spring boot project를 생성해준다. (dir 형태로!)
3. 스프링 프로젝트의 구조
gradle java 기본 프로젝트 구조와 동일하다
| 저장 파일 | 파일 경로 | 설명 |
| 소스 코드 | src/main/java | - |
| 소스 리소스 | src/main/resource | java application에서 resources 기준으로 아래 것들을 참조 가능 (classpath) |
| 테스트 코드 | src/test/java | - |
| 테스트 리소스 | src/test/resource | test 관련 리소스를 만들 수 있다 |
메인 애플리케이션 위치 (@SpringBootApplication) : 기본 패키지 package com.jyami
프로젝트가 쓰고있는 가장 최상위 패키지! > why? 컴포넌트 스캔을 하기 때문
com.jyami에서부터 시작을 해서, 그 아래에 있는 파일들을 스캔해서 bean으로 등록한다.
src/main/java 위치에 넣으면 모든 패키지를 스캔하므로
만약 java>com.hello 패키지가 있고, 그안에 메인 애플리케이션이 아닌 java파일이 있으면, 그 java파일은 component 스캔이 이루어지지 않는다.
1. Introduction to Spring Boot
1-1. Spring Boot Start
Features
It's a tool that helps you build production-level applications, not just toy projects.
opinated view : This refers to the conventions that Spring Boot has (widely used configurations)
It provides default configurations not only for the Spring platform but also for other libraries (like tomcat) out of the box.
Goals
- Provides faster and broader usability for all Spring development.
- Provides convention-based configurations without having to set everything up manually. But you can easily and quickly change these settings to match your requirements. (This is the reason to use Spring Boot)
- Provides non-functional configurations as well. Beyond features needed for business logic, it also covers non-functional features!
- Does not use XML and does not do code generation.
Spring Roo : It uniquely does code generation, but it's not widely used anymore. By not doing generation, things are easier, clearer, and simpler to customize. > The predecessor of Spring Boot
System Requirements
Spring Boot requires Java 8 or higher.
Supported servlet containers include Tomcat, Jetty, and Undertow.
2. Getting Started with Spring Boot
If you use IntelliJ Ultimate, it has a Spring Boot Initializer built in, but the Community edition does not. So you can create one using whichever build tool you prefer. We'll learn how to create a project without using the Spring Boot Initializer.
2-1. Starting from a Gradle Project
auto import OK (Applies changes immediately whenever the build.gradle file is modified: adding dependencies, etc.)
spring.io > project > spring boot > Learn > Reference Doc > Gradle Installation
Spring Boot Reference Guide
This section dives into the details of Spring Boot. Here you can learn about the key features that you may want to use and customize. If you have not already done so, you might want to read the "Part II, “Getting Started”" and "Part III, “Using Spring Boot
docs.spring.io
build.gradle
Enter the build.gradle file content.
plugins {
id 'org.springframework.boot' version '2.0.3.RELEASE'
id 'java'
}
This is a configuration closely related to dependency management.
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Typically, a project declares dependencies on one or more "starters".
Spring Boot simplifies dependency declarations and provides a useful Gradle plugin for generating jars.
initial build.gradle file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'spring-boot-getting-started'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
SpringBootApplication.java
package com.jyami;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String args[]){
SpringApplication.run(Application.class, args);
}
}
Using the SpringBootApplication annotation to call the method that runs SpringApplication
Spring MVC requires many dependencies to run — so how did all those dependencies get pulled in?
We need to configure the MVC app (bean, tomcat, etc.)
: This is all configured in @SpringBootApplication. > EnableAutoConfiguration
In IntelliJ settings, go to
Build, Execution, Deployment > Compiler > Annotation Processors and
check Enable annotation processing to be able to use annotations built with Gradle.
Run
When you run the application and check the logs, you can see that Tomcat is already running on port 8080.
If you open http://localhost:8080, you can confirm that the Tomcat web application is working. (Even though it shows an error page)
Build
gradle build
This builds the package. Since it's a Java project, a jar file is generated.
java -jar build/libs/spring-boot-getting-started-0.1.0.jar
When you run the jar file, the same Spring web application will start up just like before.
2-2. Starting a Spring Boot Project from the Web
It generates a Spring Boot project in your desired build format. (As a directory structure!)
3. Spring Project Structure
It follows the same structure as a standard Gradle Java project.
| File Type | File Path | Description |
| Source Code | src/main/java | - |
| Source Resources | src/main/resource | In a Java application, files below the resources directory can be referenced (classpath) |
| Test Code | src/test/java | - |
| Test Resources | src/test/resource | You can create test-related resources here |
Main Application Location (@SpringBootApplication) : Default package package com.jyami
This should be in the topmost package of the project! > Why? Because of component scanning.
Starting from com.jyami, it scans files underneath and registers them as beans.
If you place it directly under src/main/java, it would scan all packages.
If there's a package java>com.hello with a Java file that isn't the main application, that Java file will not be picked up by component scanning.
댓글
Comments