728x90

우선 글을 쓰기에 앞서 인프런의 스프링 부트 개념과 활용(백기선) 강의를 듣고 정리를 하는 것임을 밝힙니다.

 

스프링 부트 소개

아래 url에 들어가면 스프링 부트가 무엇인지 소개가 나옵니다.

https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#getting-started-introducing-spring-boot

 

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 Spr

docs.spring.io

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can run. We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration. 라고한다. 

정리하자면 스프링 부트는 단독으로 실행되며, 상용화 가능한 수준의 스프링에 기반한 어플리케이션을 만들기 쉽게 한다고 한다. 또한 최소한의 설정으로 스프링 플랫폼과 서드파티 라이브러리들을 사용할 수 있다.

 

스프링 부트 시작하기

이클립스나 인텔리제이를 사용해서 프로젝트를 생성한다. maven이나 gradle 어떤 프로젝트로 생성하든 상관없다. 본인이 익숙한 것으로 생성하자. 일단 maven 기준으로 설명하겠다.

maven으로 프로젝트를 생성하면 pom.xml이라는 것이 있다. 이 파일에 아래와 같이 parent, dependency, build 를 붙여넣자. 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>myproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
	</parent>

	<!-- Add typical dependencies for a web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<!-- Package as an executable jar -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

그 후 src/main/java/패키지 아래에 클래스를 생성한다. 클래스 이름은 크게 상관없다. 보통 Application을 포함하게 이름을 짓는다.

 

위와 같이 코드를 쓴 후 실행하면 spring이 실행될 것이다. 

그 후 localhost:8080에 접속하면 아래와 같이 에러 페이지가 뜰 것이다.

스프링 부트가 정상적으로 실행된 것이다. 

 

다른 방법으로 스프링 프로젝트 생성하기

https://start.spring.io/

위의 url에 접속해서 원하는대로 설정을 한 후 의존성도 주입해서 프로젝트를 생성할 수 있다. 

직접 의존성 코드를 복사 붙여넣기 귀찮을 때 간편하게 생성할 수 있는 방법이다.

 

스프링 프로젝트 구조

위의 방법대로 생성한 스프링 부트 프로젝트의 구조이다. 

src아래 main과 test로 나뉘고 main은 다시 java, resources로 나뉜다.

java 아래에는 java 코드를 작성하면 되고 resources에는 html, css, properties 등의 파일들을 작성하면 된다.

 

맨 처음 생성한 메인 Application 클래스는 우리가 생성한 프로젝트의 기본 패키지 하위에 작성해줘야 한다.

내 프로젝트의 경우 me.cho.springbootfirst가 기본 패키지이고, 이 곳이 아닌 다른 패키지 하위에 생성하면 component scan이 꼬일 수 있으니 구조를 잘 살펴보자. 

728x90

+ Recent posts