Spring Boot 프로젝트 생성을 위한 온라인 도구
https://start.spring.io
IntelliJ IDEA에서는 File → New → Project → Spring Boot 선택으로 직접 생성 가능
helloSpringBoot
├── src
│ ├── main
│ │ ├── java # 자바 소스 코드
│ │ └── resources # 설정 파일, 템플릿, 정적 리소스
│ │ ├── static # CSS, JS, 이미지 등 웹 정적 콘텐츠
│ │ └── templates# Thymeleaf 템플릿
│ └── test
│ └── java # 테스트 코드
├── pom.xml # Maven 빌드 설정
└── mvnw* # Maven Wrapper
주의: JAR 패키징 시 /src/main/webapp
디렉터리는 사용하지 마세요. WAR 전용이며 JAR로 패키징하면 무시됩니다.
pom.xml
살펴보기<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- 부모 POM 참조 -->
</parent>
<groupId>kr.ac.hansung.cse</groupId>
<artifactId>helloSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloSpringBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<!-- Thymeleaf 템플릿 엔진 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Spring Web, 내장 Tomcat 포함 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Lombok (옵셔널) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 테스트용 -> scope=test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 실행 가능한 JAR 생성 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring-boot-starter-web
기본으로 DispatcherServlet
을 /
에 매핑하고, 내장 Tomcat(포트 8080)을 추가합니다.mvn package
실행 시 spring-boot:repackage
가 자동 수행되어 실행 가능한 JAR 생성java -jar target/helloSpringBoot-0.0.1-SNAPSHOT.jar
mvn spring-boot:run
@SpringBootApplication
애노테이션을 사용합니다:
@SpringBootApplication
public class HelloSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringBootApplication.class, args);
}
}
@SpringBootApplication
= @EnableAutoConfiguration
+ @ComponentScan
+ @Configuration
NONE
: 웹 애플리케이션 비실행SERVLET
: 서블릿 기반 웹 애플리케이션 (기본 값)REACTIVE
: 리액티브 웹 애플리케이션spring.main.web-application-type=servlet
@SpringBootApplication
이 선언된 패키지와 그 하위 패키지를 스캔@SpringBootApplication(
scanBasePackages={
"kr.ac.hansung.cse.hellospringboot",
"com.mypackage.springapp",
"kr.ac.hansung.iot"
}
)
public class HelloSpringBootApplication { ... }
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "hello world");
return "index";
}
}
src/main/resources/templates/index.html
)<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
</head>
<body>
<div th:text="${message}"></div>
</body>
</html>
/static
, /public
디렉터리에서 서빙 (CSS, JS, 이미지)/templates
디렉터리에서 Thymeleaf 등 템플릿 엔진 사용src/main/resources/application.properties
에 설정:
# 서버 포트 및 컨텍스트 경로
server.port=9000
server.servlet.context-path=/helloSpringBoot
# 로깅 레벨
logging.level.kr.ac.hansung=debug
# 커스텀 프로퍼티
app.professor=Namyun Kim
app.course=Web Framework
@Value
@Value("${app.professor}")
private String professor;
@ConfigurationProperties
@Configuration
@ConfigurationProperties(prefix="jdbc")
public class DataSourceConfig {
private String driver;
private String url;
private String username;
private String password;
// getters/setters
}
jdbc.driver
, jdbc.url
등 jdbc.*
프로퍼티가 자동 바인딩됩니다.