← Back to Advanced Java Course | Chapter 10: Spring Boot | Lesson 1 of 6

Java Spring Boot Introduction

What is Spring Boot?

Spring Boot is a popular, opinionated framework built on top of the Spring ecosystem for building production-ready Java web applications quickly. It simplifies project setup dramatically by providing sensible default configurations and an embedded web server, so you can run a working app without manually configuring Tomcat or XML files.

Example: What is Spring Boot?

java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
	public static void main(String[] args) {
		// SpringApplication.run(Main.class, args); // starts an embedded server, no manual Tomcat/XML config
		System.out.println("Spring Boot provides sensible defaults out of the box");
	}
}

Inversion of Control (IoC)

Spring Boot is built around Inversion of Control (IoC): rather than your code creating and wiring up its own dependencies with new, you declare what a class needs and the framework manages creating those objects (called Beans) and their lifecycles for you.

Example: Inversion of Control (IoC)

java
import org.springframework.stereotype.Component;
public class Main {
	@Component
	static class GreetingService {
		String greet() { return "Hello"; } // the framework creates and manages this Bean, not your code
	}
	public static void main(String[] args) {
		System.out.println("Spring manages Bean creation instead of manual 'new'");
	}
}

Dependency Injection

Dependency Injection (DI) is the mechanism Spring Boot uses to actually deliver those managed Beans into your classes. Annotating a field or constructor parameter with @Autowired tells Spring to find a matching Bean and inject it automatically, instead of you instantiating it yourself.

Example: Dependency Injection

java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.*;
public class Main {
	@Component
	static class GreetingService {
		String greet() { return "Hello"; }
	}
	@Component
	static class GreetingController {
		@Autowired
		GreetingService service; // Spring injects a matching Bean automatically
	}
	public static void main(String[] args) {
		System.out.println("@Autowired injects the managed Bean for you");
	}
}

REST Controllers

REST Controllers are how Spring Boot exposes web API endpoints -- annotate a class with @RestController and its methods with mapping annotations like @GetMapping to route incoming HTTP requests to specific Java methods, which then return data that Spring automatically serializes to JSON.

Example: REST Controllers

java
import org.springframework.web.bind.annotation.*;
@RestController
public class Main {
	@GetMapping("/hello")
	String hello() {
		return "Hello, World!"; // automatically serialized to JSON/text in the response
	}
	public static void main(String[] args) {
		System.out.println(new Main().hello());
	}
}

Spring Boot Application Run

Every Spring Boot app has a central starter class whose main method calls SpringApplication.run(). That single call bootstraps the entire application context, scans for and registers your Beans, and starts the embedded server, all without you writing manual startup wiring.

Example: Spring Boot Application Run

java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
	public static void main(String[] args) {
		// SpringApplication.run(Main.class, args); // bootstraps the app context and starts the embedded server
		System.out.println("main() -> SpringApplication.run() bootstraps everything");
	}
}
Common Mistakes
  1. Using the new keyword to instantiate Spring-managed Beans, bypassing Inversion of Control entirely.
  2. Forgetting to add required component-scanning annotations, causing Spring to fail to locate and inject your classes.
  3. Writing heavy, blocking code inside your main starting method, preventing the framework from bootstrapping successfully.
Chapter Summary
  • Spring Boot is a production-ready framework that provides automatic configurations and embedded web servers natively.
  • Inversion of Control (IoC) means the framework manages your object instances (Beans) and lifecycles automatically.
  • Use Dependency Injection (DI) to deliver managed Beans cleanly, and REST Controllers to map browser paths to API methods.
Browser Support

Spring Boot and standard Java compilation requirements are natively supported by all Java 11+ development kits.

🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.