Chapter 04: Developing a Spring Boot Application: Difference between revisions
Created page with "<span id="chapter-04-developing-a-spring-boot-application"></span> = Chapter 04: Developing a Spring Boot Application = It’s time to write code! In this chapter, we’ll create our first Spring Boot application with Kotlin, explore the project structure, understand build configuration, and get our “Hello World” running. By the end, you’ll have a solid foundation for building real applications. <span id="creating-a-project"></span> == 4.1 Creating a Project ==..." |
|||
| (6 intermediate revisions by the same user not shown) | |||
| Line 14: | Line 14: | ||
IntelliJ IDEA provides the most streamlined experience for creating Spring Boot projects with Kotlin. | IntelliJ IDEA provides the most streamlined experience for creating Spring Boot projects with Kotlin. | ||
'''Step 1: Start the New Project Wizard''' | '''Step 1: Start the New Project Wizard''' | ||
# Open IntelliJ IDEA | |||
# Click “New Project” or File → New → Project | |||
# Select “Spring Initializr” from the left panel | |||
'''Step 2: Configure Project Metadata''' | '''Step 2: Configure Project Metadata''' | ||
| Line 50: | Line 54: | ||
Click "Create"</pre> | Click "Create"</pre> | ||
'''What IntelliJ Does Behind the Scenes:''' | '''What IntelliJ Does Behind the Scenes:''' | ||
* Downloads project template from start.spring.io | |||
* Configures Gradle wrapper | |||
* Sets up proper source directories | |||
* Indexes dependencies | |||
* Configures Kotlin compiler | |||
* Sets up run configurations | |||
<span id="creating-a-project-from-the-official-spring-site"></span> | <span id="creating-a-project-from-the-official-spring-site"></span> | ||
=== 4.1.2 Creating a Project from the Official Spring Site === | === 4.1.2 Creating a Project from the Official Spring Site === | ||
| Line 77: | Line 89: | ||
'''Step 3: Add Dependencies''' | '''Step 3: Add Dependencies''' | ||
Click “ADD DEPENDENCIES” and search for: | Click “ADD DEPENDENCIES” and search for: | ||
'''Step 4: Generate and Extract''' | * Spring Web | ||
* Spring Data JPA | |||
* PostgreSQL Driver | |||
* Spring Boot DevTools | |||
* Spring Boot Actuator | |||
'''Step 4: Generate and Extract''' | |||
# Click “GENERATE” to download the ZIP file | |||
# Extract to your projects directory | |||
# Open in IntelliJ IDEA: File → Open → Select the extracted folder | |||
'''Pro Tip: Using the Spring Initializr REST API''' | '''Pro Tip: Using the Spring Initializr REST API''' | ||
| Line 101: | Line 123: | ||
cd my-first-app</syntaxhighlight> | cd my-first-app</syntaxhighlight> | ||
<span id="understanding-the-generated-project-structure"></span> | <span id="understanding-the-generated-project-structure"></span> | ||
=== Understanding the Generated Project Structure === | === Understanding the Generated Project Structure === | ||
| Line 144: | Line 167: | ||
runApplication<MyFirstAppApplication>(*args) | runApplication<MyFirstAppApplication>(*args) | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
The <code>@SpringBootApplication</code> annotation is actually three annotations in one: | The <code>@SpringBootApplication</code> annotation is actually three annotations in one: | ||
* <code>@Configuration</code>: Marks this as a configuration class | |||
* <code>@EnableAutoConfiguration</code>: Enables Spring Boot’s auto-configuration | |||
* <code>@ComponentScan</code>: Enables component scanning from this package | |||
<span id="exploring-build.gradle.kts"></span> | <span id="exploring-build.gradle.kts"></span> | ||
== 4.2 Exploring build.gradle.kts == | == 4.2 Exploring build.gradle.kts == | ||
| Line 156: | Line 183: | ||
Before diving into Gradle, let’s understand why we use build tools: | Before diving into Gradle, let’s understand why we use build tools: | ||
'''What Build Tools Do:''' | '''What Build Tools Do:''' | ||
* Dependency management | |||
* Compilation and packaging | |||
* Running tests | |||
* Code quality checks | |||
* Deployment preparation | |||
'''Popular Build Tools for JVM:''' | '''Popular Build Tools for JVM:''' | ||
* '''Gradle''': Modern, flexible, uses Groovy or Kotlin DSL | |||
* '''Maven''': Mature, XML-based, extensive plugin ecosystem | |||
* '''Bazel''': Google’s build tool, excellent for monorepos | |||
* '''SBT''': Scala-focused but works with Kotlin | |||
We use Gradle with Kotlin DSL because: | We use Gradle with Kotlin DSL because: | ||
# Type-safe build scripts | |||
# IDE auto-completion | |||
# Kotlin syntax consistency | |||
# Better refactoring support | |||
<span id="gradle"></span> | <span id="gradle"></span> | ||
=== 4.2.2 Gradle === | === 4.2.2 Gradle === | ||
Gradle is more than a build tool—it’s a build automation platform. Let’s understand its core concepts: | Gradle is more than a build tool—it’s a build automation platform. Let’s understand its core concepts: | ||
'''Gradle Concepts:''' | '''Gradle Concepts:''' | ||
* '''Project''': What you’re building (your application) | |||
* '''Task''': A unit of work (compile, test, jar) | |||
* '''Plugin''': Adds tasks and conventions | |||
* '''Dependency''': External libraries your project needs | |||
* '''Repository''': Where dependencies are downloaded from | |||
'''Gradle Wrapper:''' The wrapper ensures everyone uses the same Gradle version: | '''Gradle Wrapper:''' The wrapper ensures everyone uses the same Gradle version: | ||
| Line 184: | Line 230: | ||
gradlew.bat build # Windows</syntaxhighlight> | gradlew.bat build # Windows</syntaxhighlight> | ||
<span id="managing-dependencies-using-kotlin-dsl"></span> | <span id="managing-dependencies-using-kotlin-dsl"></span> | ||
=== 4.2.3 Managing Dependencies Using Kotlin DSL === | === 4.2.3 Managing Dependencies Using Kotlin DSL === | ||