Getting Started with SpringBoot in Kotlin/Java [DRAFT]
Published: 2024-01-07 16:34:43

> **🔍 Disclaimer:**
>
> The following is based on my experience and knowledge, and may not represent the best approach or most up-to-date information. Please feel free to share your insights and correct any outdated information.
>
> *This is an open dialogue; let's learn and improve together.* 🚀

If you are a beginner, you might face problems when you find resources in a particular language. My best advice is whenever you look for a resource be neutral to language as you might not find the best resource, you can always implement the same logic in another language

For example in Spring Boot, if you go with Kotlin (Gradle Kotlin DSL), on the internet you might find more implementation pom.xml rather than gradle in Kotlin. But when you match both syntaxes, you will learn how to read pom.xml and can convert it on your own without any AI to gradle compatible.

## Quick Note

As a general approach before starting anything, you should know why SpringBoot and what's the alternatives in the market.

Springboot is widely used in ITs as a backend, because of its flexibility and ease to maintain large services using micro service and MVC architecture.

## Springboot vs Spring Framework?

Simple -> Springboot is made over spring framework allows more functionality as follows:

- Easily create standalone applications.
- Offers production-ready features.
- SpringBoot is easier to launch.
- Easier customization and management.

Prerequisite
1. IntelliJ IDE [Community/ Enterprise]
2. Java/Kotlin 
3. Knowledge of MVC Architecture

## Getting Started with SpringBoot
We will be creating a basic REST Server. Use [https://start.spring.io/](https://start.spring.io/) to create a start template for us. You can choose your combination as per your need. I am going with Gradle-Kotlin with language Kotlin.

### Dependency and Plugins in Springboot

Dependency is nothing but a 'Library' that provides specific functionality that we can use in our application

whereas, plugins are used to: create jar files, create war files, compile code, unit test code, create project documentation, and on and on.

We will be adding some dependencies to start with.
-Spring Web- Provides functionality like live reload of server.

![start.srping.io-Website](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6guzg6dbdv44e91kk9n.png)

This will generate a zip file, extract it, and open it on IDE. When you open this, you will see some sync going on in the background i.e. Gradle Sync, this will download all the necessary dependencies to plugins defined in build.gradle.kt file.

#### Project Structure
```
.
└── src
    ├── main
    │   ├── kotlin
    │   │   └── com.example.demo
    │   │       └── Demo.Application.kt
    │   └── resources
    │       ├── static
    │       ├── template
    │       └── application.properties
    └── test
        └── kotlin
            └── com.example.demo
                └── Demo.Application.kt
```
src is our classpath where is our all application logic coded.

### DemoApplication.kt Explained

```
@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
	runApplication<DemoApplication>(*args)
}
```

@SpringBootApplication This is provided by springboot annotations, we will look at some annotations and their use cases.

The @SpringBootApplication annotation is equivalent to using **@Configuration**, **@EnableAutoConfiguration**, and **@ComponentScan** with their default attributes, as shown in the following example:

- **@EnableAutoConfiguration:** enable Spring Boot’s auto-configuration mechanism
- **@ComponentScan:** enable @Component scan on the package where the application is located
- **@Configuration:** allow to register extra beans in the context or import additional configuration classes

### Beans in SpringBoot
Beans are just like objects of classes, but these are not objects of class, these are objects of spring framework context and managed by spring