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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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:

  1. Presentation Layer: Controllers that handle HTTP requests and responses.
  2. Service Layer: Business logic implementation.
  3. Data Access Layer: Components that interact with databases or external systems.
  4. Database Layer: The actual database where data is stored.

Key Annotations in Spring Boot

  1. @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.
  2. @RestController: Marks a class as a controller where every method returns a domain object instead of a view.

  3. @RequestMapping: Maps HTTP requests to handler methods.

  4. @Autowired: Enables dependency injection.

  5. @Service: Indicates that a class contains business logic.

  6. @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:

  1. Analyzing the classpath and the dependencies you’ve included.
  2. Creating appropriate beans based on certain conditions.
  3. 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:

  1. Java Development Kit (JDK): Version 17 or higher is recommended for recent Spring Boot versions.
  2. Build Tool: Maven or Gradle to manage dependencies and build the project.
  3. IDE: IntelliJ IDEA, Eclipse, or Spring Tool Suite (STS) with Spring Boot plugins.
  4. Git: For version control.

Bootstrapping a Spring Boot Application

You can create a new Spring Boot project using:

  1. Spring Initializr: A web-based tool at start.spring.io that generates project structures.
  2. IDE Integration: Most modern IDEs have integrated Spring Initializr.
  3. Spring Boot CLI: A command-line tool to run and test Spring Boot applications.
  4. 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:

  1. From the IDE: Using the run button.
  2. Using the Maven plugin: mvn spring-boot:run
  3. Using the Gradle task: ./gradlew bootRun
  4. As a packaged JAR: java -jar target/myapp-0.0.1-SNAPSHOT.jar

Quiz: Spring Boot Fundamentals

  1. 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
  2. Which annotation is used to mark the main class of a Spring Boot application?

    • A) @SpringApplication
    • B) @MainApplication
    • C) @SpringBootApplication
    • D) @BootApplication
  3. 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
  4. 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
  5. 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
  6. Which embedded servers can be used with Spring Boot? (Choose all that apply)

    • A) Tomcat
    • B) Jetty
    • C) Undertow
    • D) WebLogic
  7. What file is commonly used to externalize configuration in Spring Boot?

    • A) config.xml
    • B) application.properties
    • C) settings.json
    • D) spring-config.yaml
  8. 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
  9. Which annotation would you use to create a RESTful controller in Spring Boot?

    • A) @Controller
    • B) @RestController
    • C) @ApiController
    • D) @WebController
  10. What is the default port that a Spring Boot web application runs on?

    • A) 8080
    • B) 8000
    • C) 9090
    • D) 3000

Quiz Answers

  1. B) It simplifies application development with auto-configuration
  2. C) @SpringBootApplication
  3. B) Automatically configures the application based on dependencies
  4. C) Mandatory XML configuration
  5. C) Dependency descriptors that simplify dependency management
  6. A, B, C (Tomcat, Jetty, Undertow)
  7. B) application.properties
  8. B) java -jar application.jar
  9. B) @RestController
  10. A) 8080

By Wahid Hamdi