Spring VS SpringBoot
Purpose
Used to build Web Apps Used to build REST API
Key Functionality
Most essential features is Prime feature of Spring Boot is Auto
Dependency Injection Configuration
Use Case
Loosely Coupled Application Standalone Application
Servers
Explicit configuration of servers is Pre-Installed servers like Tomcat and
needed Jetty
XML Configuration
Required Not Required
Database
No support for in-memory database Support for in-memory database like
H2 and Tomcat
Starter Projects
Developers manually add Provides starter dependencies (e.g.,
dependencies. spring-boot-starter-web, spring-boot-
starter-data-jpa) to simplify dependency
management.
Spring Application Starts
Main Class (Bootstrapper)
Create a class with a main method to initialize the Spring container.
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new
AnnotationConfigApplicationContext(AppConfig.class);
}
}
Load Configuration
Use Java Config (@Configuration) or XML (applicationContext.xml).
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}
Initialize ApplicationContext
ApplicationContext is created to manage beans.
AnnotationConfigApplicationContext (Java Config)
ClassPathXmlApplicationContext (XML Config)
Component Scanning
Beans are detected using @ComponentScan and @Component annotations.
Bean Creation and Injection
Spring injects dependencies using @Autowired or XML <bean>.
Lifecycle Management
Beans go through @PostConstruct and @PreDestroy for initialization and destruction.
Web App
For Spring MVC, a DispatcherServlet handles HTTP requests, routing them to
controllers.
Run Application
Deploy as WAR to Tomcat/Jetty.
No embedded server, external server is required.
Spring Boot Application Starts
Main Class with @SpringBootApplication
One class with @SpringBootApplication and a main method
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Auto-Configuration
@EnableAutoConfiguration automatically configures the app based on dependencies.
Embedded Server
Tomcat/Jetty/Undertow starts automatically.
No need for external deployment.
Component Scanning
@SpringBootApplication includes @ComponentScan to detect beans.
Bean Creation and Injection
Spring Boot manages dependencies with @Autowired and @Bean.
Web App (Optional)
Add REST controllers with @RestController.
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring!";
}
}
Run Application
Run via java -jar app.jar or directly from IDE.
Embedded server listens on localhost:8080 by default.