Getting Started With Spring Boot

Introduction
Hi! I’m Mehul, a computer science grad and a big-time Java enthusiast. I’ve always loved digging into new tech and figuring out how things work under the hood. Back in college, I was that unofficial “teacher friend” in my circle - the one people came to when they got stuck. And you know what? I really enjoyed explaining things. That’s actually one reason I decided to start blogging - it helps me learn better and (hopefully) makes things easier for you too.
What is Spring Boot?
It’s a framework built on top of Spring that makes development easier, reduces boilerplate code, and supports auto-configuration. That sounds like a mouthful, so let’s break it down.
Before Spring Boot came into the picture, developers had to manually configure almost everything. For example, if you wanted to use PostgreSQL, you’d need to write long XML configuration files to set up the datasource, connection pool, transaction manager, and more just to get started.
Enter Spring Boot.
Spring Boot (developed by VMware, alongside other Spring projects) was created to simplify this process. It provides an “opinionated view” of how Spring applications should be configured. In other words, the Spring Boot team baked in sensible defaults and best practices, so instead of you writing endless setup code, Spring Boot does it for you automatically.
Think of it like this: instead of arguing whether to use camelCase
or snake_case
, Spring Boot just picks one for you, the one that works best in most cases. You can override it if you want, but the default usually gets you up and running faster.
spring-boot-starter-data-jpa
in your dependencies, it will automatically configure Hibernate and a datasource for you.How does it work?
Decoding Auto-configuration.
Spring Boot try to Auto-configure the Spring application based on the dependencies that you have added in the pom.xml file. So if H2
Database is on your classpath, and you have not manually added the database connection beans, then Spring Boot auto-configures the in-memory database for you.
Classpath Scanning.
When Spring boot Application starts, it scans the classpath for the available dependencies and components. This step is important because based on this the application determines which configurations to apply.
For example:
- If the spring-boot-starter-web is on the classpath, then configures the embedded web server, a Dispatcher servlet, and REST controllers.
Once the dependencies are discovered, Spring Boot looks into its registry of auto-configuration classes to decide which ones might be relevant.
Auto-Config Classes
Spring Boot ships with several auto-configuration classes (like DataSourceAutoConfiguration
, WebMvcAutoConfiguration
, etc). But Spring Boot doesn’t load them blindly, otherwise startup would be slow and bloated.
Instead it uses special files inside Spring Boot Jars. This is basically a registry of all possible auto-configuration
The Role of META-INF:
Prior to Spring Boot version 2.7, the META-INF/spring.factories
file was important to auto-configuring process. This file consist of list of all auto-configuration classes that Spring Boot should consider. Example of spring.factories
:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
Starting from Spring Boot 2.7 or later, the Role shifted to META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
. This shift from previous version introduced startup performance gain by reducing overhead of parsing “giant” spring.factories
file. It also aligns better with GraalVM native image requirements by using a more explicit and structured approach.
It also aligns better with GraalVM native image requirements by using a more explicit and structured approach.
Conditional Configuration with Annotations:
Even if these classes are listed, they won’t always activate.
Each auto-config class has conditional annotation to decide if specific configurations should be applied, depending on the runtime state of the application. commonly used annotations are:
@ConditionalOnClass
→ Load this config only if a particular class exists on the classpath.- Example:
@ConditionalOnClass(javax.sql.DataSource.class)
→ ensures you have a database library.
- Example:
@ConditionalOnMissingBean
→ Only load if the developer hasn’t already defined their own bean.- Prevents overwriting user-defined beans.
@ConditionalOnBean
→ Opposite of above: only load if a specific bean already exists.- Useful for chaining configs.
@ConditionalOnProperty
→ Load if a property is present/enabled inapplication.properties
orapplication.yml
.- Example:
@ConditionalOnProperty(name="spring.datasource.url")
.
- Example:
@ConditionalOnResource
→ Load only if a resource file is available (e.g.,schema.sql
).@ConditionalOnWebApplication
/@ConditionalOnNotWebApplication
→ Only apply in a web app (Tomcat/Jetty/Netty available) vs. non-web app.
Let’s take a look at how this annotation works
@Configuration
@ConditionalOnClass(javax.sql.DataSource.class) // Classpath check
@ConditionalOnMissingBean(DataSource.class) // Bean check
public class DataSourceAutoConfiguration {
@Bean
@ConfigurationProperties("spring.datasource") // Properties binding
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
Explanation:
Spring Boot first looks at the
DataSourceAutoConfiguration
in theMETA-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
.It checks, is
javax.sql.DataSource
exist on classpath? (Yes because you added H2/Postgres or any other db you have added.)It checks did the dev already defind a
DataSource
bean? (No)
Both Condition Satisfies then Spring Boot configures the DataSource
bean automatically.
If you had defined the DataSource bean, then Spring Boot skips the configuration for it.
Implementing our first project.
Let’s Implement a basic rest API Backend using Spring Boot.
- Spring Initializr (https://start.spring.io) is a web interface and API provided by the Spring team to quickly bootstrap new projects. Instead of manually creating a Maven/Gradle project, adding dependencies one by one, and wiring the folder structure, Initializr generates a ready-to-run skeleton project for you.
- Configure Project Metadata
Here, we can select project metadata like Project type, Language, Spring Boot version, etc.
Notice that I selected Maven
, Java
21, and added the Spring Web
dependency. These are enough to start building a REST API.
Let’s add the necessary dependency for our project.
We will add Spring Web dependency.
- Generate and import the Project.
You can click on Explore (ctrl + space) to see the Project Structure and more importantly pom.xml file.
Let’s download this project and open in the IDE. Here, i am using Intellij IDEA Utimate edition. The Project will have structure like this:
src/main/java
: This is where your main application code (controllers, services, repositories, etc.) will live.src/main/resources
: Whereapplication.properties
/application.yml
live. Also holds static files (HTML, CSS, JS) and templates if you build a web appsrc/test/java
: Holds test cases. All your JUnit or integration tests go here.pom.xml
: Heart of the project. Declares dependencies (like Spring Web, Spring Data JPA, etc.).
HelloworldApplication.java
package com.mehulsuthar.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
This is the entry point of our Project and the most important class. The @SpringBootApplication
Annotation is used to enable Auto Configuration for our Project.
@SpringBootApplication
: It is a combination other Annotations.
@Configuration
(marks class as source of bean definitions)@EnableAutoConfiguration
(auto-configures Spring based on dependencies)@ComponentScan
(automatically scans the package for beans/components)
- Create Hello Controller
Create a HelloController.java class in the helloworld package.
package com.mehulsuthar.helloworld;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "Hello World!";
}
}
And this is our basic project and let’s give it a try. Run the project and go to this url (http://localhost:8080/hello) in your browser. You should see some thing like this.
If you look closely, we didn’t configure a web server, dispatcher servlet, or any deployment descriptor (like web.xml
). Spring Boot did all of that for us behind the scenes. All we wrote was our business logic.
That’s the real power of Spring Boot, it eliminates boilerplate configuration and lets developers focus on solving problems rather than setting up infrastructure.
In a traditional Java project, you would have to manually configure a server (like Tomcat), define servlets, and map requests in configuration files. Spring Boot makes this seamless by providing sensible defaults and auto-configuration.
Conclusion
To wrap up, Spring Boot takes away the pain of manual configurations and lets developers focus on writing business logic. With features like auto-configuration, sensible defaults, and an easy project bootstrapper (Spring Initializr), it has become the go-to framework for modern Java development. Our little “Hello World” app might be simple, but it demonstrates the core idea — less setup, more coding.
I hope this guide gave you a clearer picture of how Spring Boot works under the hood and how easily you can get started. If you found this helpful, feel free to share your thoughts, ask questions, or suggest topics you’d like me to cover next. Let’s keep learning Spring Boot together.
Subscribe to my newsletter
Read articles from Mehul Suthar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
