Module 1 - Introduction & Environment Setup
Lecture: Introduction to Spring Boot
What is Spring Boot?
Spring Boot is a framework that simplifies the development of Spring-based applications. It provides a set of tools and conventions that allow developers to create production-ready applications with minimal configuration. Spring Boot is built on top of the Spring Framework, which has been a popular choice for enterprise Java development for nearly two decades.
Key Features of Spring Boot
-
Auto-configuration: Spring Boot automatically configures your application based on the dependencies you’ve added to your project. This eliminates the need for extensive XML or Java configuration.
-
Standalone Applications: Spring Boot makes it easy to create standalone applications that can be run with a simple
java -jar
command. This is accomplished through the embedded server approach. -
Embedded Servers: Spring Boot applications come with embedded web servers like Tomcat, Jetty, or Undertow. This eliminates the need to deploy WAR files to external servers.
-
Production-ready Features: Spring Boot includes features like health checks, metrics, externalized configuration, and other tools that make it easier to deploy applications to production environments.
-
Spring Boot Starters: These are dependency descriptors that simplify the inclusion of common technologies in your project. For example, if you want to build a web application, you can include the
spring-boot-starter-web
dependency.
The Spring Ecosystem
Spring Boot is part of the larger Spring ecosystem, which includes:
- Spring Framework: The core framework that provides fundamental features like dependency injection, transaction management, data access, and more.
- Spring Data: Simplifies data access by reducing boilerplate code.
- Spring Security: Provides comprehensive security services for Java applications.
- Spring Cloud: Tools for building distributed systems and microservices.
- Spring Batch: Framework for batch processing.
Architecture of a Spring Boot Application
A typical Spring Boot application follows a layered architecture:
- Presentation Layer: Controllers that handle HTTP requests and responses.
- Service Layer: Business logic implementation.
- Data Access Layer: Components that interact with databases or external systems.
- Database Layer: The actual database where data is stored.
Key Annotations in Spring Boot
-
@SpringBootApplication: This annotation marks the main class of a Spring Boot application. It combines three annotations:
- @Configuration: Marks the class as a source of bean definitions.
- @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings.
- @ComponentScan: Tells Spring to look for components in the current package and its sub-packages.
-
@RestController: Marks a class as a controller where every method returns a domain object instead of a view.
-
@RequestMapping: Maps HTTP requests to handler methods.
-
@Autowired: Enables dependency injection.
-
@Service: Indicates that a class contains business logic.
-
@Repository: Indicates that a class is used for database access.
Spring Boot Internals: How Auto-configuration Works
Spring Boot’s auto-configuration mechanism works by:
- Analyzing the classpath and the dependencies you’ve included.
- Creating appropriate beans based on certain conditions.
- Allowing you to override these automatic configurations when needed.
For example, when you include spring-boot-starter-web
, Spring Boot automatically:
- Sets up an embedded Tomcat server
- Configures a DispatcherServlet
- Sets up default error handling
- Configures JSON serialization/deserialization
Setting Up the Development Environment
To develop Spring Boot applications, you’ll need:
- Java Development Kit (JDK): Version 17 or higher is recommended for recent Spring Boot versions.
- Build Tool: Maven or Gradle to manage dependencies and build the project.
- IDE: IntelliJ IDEA, Eclipse, or Spring Tool Suite (STS) with Spring Boot plugins.
- Git: For version control.
Bootstrapping a Spring Boot Application
You can create a new Spring Boot project using:
- Spring Initializr: A web-based tool at start.spring.io that generates project structures.
- IDE Integration: Most modern IDEs have integrated Spring Initializr.
- Spring Boot CLI: A command-line tool to run and test Spring Boot applications.
- Via Build Tools: Directly using Maven or Gradle with appropriate plugins.
Understanding the Project Structure
A typical Spring Boot project structure:
my-spring-boot-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── myapp/
│ │ │ ├── MySpringBootApplication.java
│ │ │ ├── controller/
│ │ │ ├── service/
│ │ │ ├── repository/
│ │ │ └── model/
│ │ └── resources/
│ │ ├── application.properties
│ │ ├── static/
│ │ └── templates/
│ └── test/
│ └── java/
│ └── com/
│ └── example/
│ └── myapp/
│ └── tests/
└── pom.xml (or build.gradle)
- src/main/java: Contains Java source code.
- src/main/resources: Contains configuration files and static resources.
- src/test: Contains test code.
- pom.xml or build.gradle: Build configuration file.
Application Properties
The application.properties
(or application.yml
) file contains configurations for your application:
# Server properties
server.port=8080
# Data source properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
# Logging properties
logging.level.root=INFO
Running a Spring Boot Application
You can run a Spring Boot application in several ways:
- From the IDE: Using the run button.
- Using the Maven plugin:
mvn spring-boot:run
- Using the Gradle task:
./gradlew bootRun
- As a packaged JAR:
java -jar target/myapp-0.0.1-SNAPSHOT.jar
Quiz: Spring Boot Fundamentals
-
What is the primary advantage of using Spring Boot over the traditional Spring Framework?
- A) It requires more configuration
- B) It simplifies application development with auto-configuration
- C) It removes the dependency on Spring Framework
- D) It only works with non-web applications
-
Which annotation is used to mark the main class of a Spring Boot application?
- A) @SpringApplication
- B) @MainApplication
- C) @SpringBootApplication
- D) @BootApplication
-
What does the @EnableAutoConfiguration annotation do?
- A) Enables manual configuration of all beans
- B) Automatically configures the application based on dependencies
- C) Enables security features
- D) Configures database connections only
-
Which of the following is NOT a feature of Spring Boot?
- A) Embedded server support
- B) Auto-configuration
- C) Mandatory XML configuration
- D) Production-ready features like metrics and health checks
-
What are Spring Boot Starters?
- A) Classes that implement the ApplicationRunner interface
- B) Special scripts that initiate the application
- C) Dependency descriptors that simplify dependency management
- D) Methods annotated with @PostConstruct
-
Which embedded servers can be used with Spring Boot? (Choose all that apply)
- A) Tomcat
- B) Jetty
- C) Undertow
- D) WebLogic
-
What file is commonly used to externalize configuration in Spring Boot?
- A) config.xml
- B) application.properties
- C) settings.json
- D) spring-config.yaml
-
How do you typically run a packaged Spring Boot application?
- A) mvn spring-boot:execute
- B) java -jar application.jar
- C) spring run app.jar
- D) gradle execute
-
Which annotation would you use to create a RESTful controller in Spring Boot?
- A) @Controller
- B) @RestController
- C) @ApiController
- D) @WebController
-
What is the default port that a Spring Boot web application runs on?
- A) 8080
- B) 8000
- C) 9090
- D) 3000
Quiz Answers
- B) It simplifies application development with auto-configuration
- C) @SpringBootApplication
- B) Automatically configures the application based on dependencies
- C) Mandatory XML configuration
- C) Dependency descriptors that simplify dependency management
- A, B, C (Tomcat, Jetty, Undertow)
- B) application.properties
- B) java -jar application.jar
- B) @RestController
- A) 8080
By Wahid Hamdi