Java Spring Boot Introduction
In this page:
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?
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");
}
}
Login to try C/C++/Java/PHP code in the editor
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)
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'");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
Login to try C/C++/Java/PHP code in the editor
- Using the new keyword to instantiate Spring-managed Beans, bypassing Inversion of Control entirely.
- Forgetting to add required component-scanning annotations, causing Spring to fail to locate and inject your classes.
- Writing heavy, blocking code inside your main starting method, preventing the framework from bootstrapping successfully.
- 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.
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: