300===Dev Framework/Spring Boot

Spring Boot Introduced

블로글러 2024. 5. 27. 12:11

Spring Boot is a framework that simplifies the process of building production-ready applications with the Spring framework, providing default configurations and built-in functionalities to streamline development.

The Big Picture

Imagine building a complex machine from scratch. You'd need to carefully select and configure each component, ensuring everything works together. Spring Boot acts like a pre-assembled toolkit with most of the components you need already in place, ready to be used. This saves time and reduces the chances of making errors.

Core Concepts

  1. Convention Over Configuration: Spring Boot comes with sensible defaults that simplify the setup of new applications. Instead of configuring everything from scratch, it provides a set of conventions to follow, reducing boilerplate code.

  2. Embedded Servers: Unlike traditional Spring applications where you deploy a WAR file to an external server, Spring Boot includes embedded servers (like Tomcat or Jetty). This makes it easy to run applications with a simple command.

  3. Starter Dependencies: These are convenient dependency descriptors you can include in your project. They bring in a set of dependencies (libraries) that you need to get started quickly without worrying about version conflicts.

  4. Auto-Configuration: Spring Boot automatically configures your application based on the dependencies you add. For instance, if you include a database dependency, Spring Boot sets up a DataSource for you.

  5. Actuator: This feature provides production-ready features like monitoring, metrics, and health checks with minimal effort.

Detailed Walkthrough

Let's break down the process of creating a Spring Boot application:

  1. Setting Up: Use Spring Initializr (a web-based tool) or an IDE plugin to generate a new Spring Boot project. This tool helps you select the necessary dependencies and generates a base project structure.

  2. Creating the Main Application Class: This class is annotated with @SpringBootApplication, which triggers the auto-configuration, component scan, and configuration properties.

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MySpringBootApplication {
        public static void main(String[] args) {
            SpringApplication.run(MySpringBootApplication.class, args);
        }
    }
  3. Adding Dependencies: By including starter dependencies in your pom.xml (for Maven) or build.gradle (for Gradle), Spring Boot automatically configures the necessary components.

    <!-- Maven example -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  4. Creating REST Controllers: Use annotations like @RestController and @RequestMapping to define RESTful endpoints.

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
    
        @GetMapping("/hello")
        public String hello() {
            return "Hello, World!";
        }
    }
  5. Running the Application: With the embedded server, you can run the application directly using mvn spring-boot:run or by executing the main class. The application will start up with an embedded server, and you can access it at http://localhost:8080.

Understanding Through an Example

Let's create a simple Spring Boot application that manages a list of books.

  1. Project Setup: Use Spring Initializr to create a new project with dependencies for Spring Web and Spring Data JPA.

  2. Entity Class: Define a Book entity.

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity
    public class Book {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String title;
        private String author;
    
        // Getters and Setters
    }
  3. Repository Interface: Create a repository interface for data access.

    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface BookRepository extends JpaRepository<Book, Long> {
    }
  4. Service Class: Implement a service class to handle business logic.

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class BookService {
    
        @Autowired
        private BookRepository bookRepository;
    
        public List<Book> getAllBooks() {
            return bookRepository.findAll();
        }
    
        public Book addBook(Book book) {
            return bookRepository.save(book);
        }
    }
  5. Controller Class: Create a controller to expose REST endpoints.

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/books")
    public class BookController {
    
        @Autowired
        private BookService bookService;
    
        @GetMapping
        public List<Book> getAllBooks() {
            return bookService.getAllBooks();
        }
    
        @PostMapping
        public Book addBook(@RequestBody Book book) {
            return bookService.addBook(book);
        }
    }

Conclusion and Summary

Spring Boot simplifies the process of building, configuring, and deploying Spring applications. By providing default configurations, embedded servers, and starter dependencies, it reduces the complexity and boilerplate code involved in setting up a Spring project. With auto-configuration and built-in features like Actuator, it helps developers focus on writing business logic rather than dealing with infrastructure concerns.

Test Your Understanding

  1. What is the main advantage of using Spring Boot's embedded servers?
  2. How does Spring Boot's auto-configuration feature simplify application setup?
  3. Describe the role of starter dependencies in a Spring Boot project.
  4. Explain the use of @SpringBootApplication annotation.

For further reading, you can refer to the official Spring Boot documentation.

728x90