Unit 4 FSD
Unit 4 FSD
Core Components
Explain some of the most used Spring Boot annotations with an example.
1. @SpringBootApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
SpringApplication.run(MySpringBootApplication.class, args);
2. @RestController
This annotation is a combination of @Controller and @ResponseBody. It is used to create
RESTful web services.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
@GetMapping("/greeting")
3. @RequestMapping
This annotation is used to map web requests to specific handler classes or handler methods.
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
return "Hello!";
}
4. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
@GetMapping("/hello")
@PostMapping("/hello")
@PutMapping("/hello")
@DeleteMapping("/hello")
5. @Autowired
This annotation is used for automatic dependency injection.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
return "Service";
@RestController
@RequestMapping("/api")
@Autowired
@GetMapping("/serve")
return myService.serve();
import org.springframework.stereotype.Component;
@Component
• Use Spring Boot if you want to quickly set up a Spring application with minimal
configuration and are looking for an opinionated, production-ready framework that
simplifies deployment and management.
• Use Spring MVC if you need fine-grained control over the configuration of your web
application and are comfortable with managing dependencies and configurations
manually. Spring MVC is suitable for complex web applications that require custom
configurations.
Design and implement simple spring boot program to print “WELCOME TO SPRING BOOT”.
package com.example.welcomespringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
SpringApplication.run(WelcomeSpringBootApplication.class, args);
@RestController
class WelcomeController {
@GetMapping("/")
Explanation:
1. Main Class:
o WelcomeSpringBootApplication: This is the entry point of the Spring Boot
application. The @SpringBootApplication annotation enables auto-
configuration, component scanning, and configuration properties support.
2. REST Controller:
o WelcomeController: This is a simple REST controller with a single endpoint
(/) that returns the message "WELCOME TO SPRING BOOT". The
@RestController annotation makes the class a RESTful web service
controller, and @GetMapping("/") maps HTTP GET requests to the welcome
method.
• The Spring container instantiates the bean using the class's no-argument constructor
(or a specified constructor).
• This is the stage where the bean object is created but not yet initialized.
6. Pre-Initialization (BeanPostProcessor)
9. Post-Initialization (BeanPostProcessor)
• The bean is now fully initialized and ready to be used within the application.
• When the application context is closed, the bean goes through the destruction phase.
• If the bean implements the DisposableBean interface, the destroy() method is
called.
• Any custom destroy methods specified in the bean configuration are invoked.
i. Spring Container
The Spring Container is the core of the Spring Framework. It is responsible for managing the
lifecycle and configuration of application objects, known as beans. The container uses
Dependency Injection (DI) to manage the components that make up an application. The two
main types of containers in Spring are:
// Service interface
void sayGreeting();
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Bean
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
greetingService.sayGreeting();
}
Explain Spring web MVC. Design and implement simple spring web MVC
framework program to display Helloworld.
Spring Web MVC is a framework that is part of the larger Spring Framework. It follows the
Model-View-Controller (MVC) design pattern, which helps in separating the business logic,
presentation logic, and navigation logic in web applications. Here is a brief overview of each
component:
Explanation of Components
package com.example.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
return "helloworld";
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
Define Spring Boot. Why should we use Spring Boot Framework? List out
Spring boot features.
Spring Boot is an open-source framework that is part of the larger Spring ecosystem. It is
designed to simplify the process of creating stand-alone, production-grade Spring-based
applications. Spring Boot does this by providing a range of out-of-the-box configurations and
defaults, which reduces the amount of manual setup required. It allows developers to focus
more on writing business logic rather than on configuring and setting up the application
infrastructure.
1. Spring Initializr: A web-based tool for generating Spring Boot projects. It allows
developers to select dependencies and download a ready-to-use project.
2. Auto-Configuration: Automatically configures Spring and third-party libraries based
on the project’s dependencies.
3. Standalone Applications: Easily create stand-alone Spring applications with
embedded servers like Tomcat, Jetty, or Undertow.
4. Production-ready Metrics: Out-of-the-box support for health checks, metrics,
application information, and externalized configuration.
5. Spring Boot CLI: Command Line Interface that allows you to quickly prototype with
Spring Boot applications using Groovy scripts.
6. Spring Boot Starters: Pre-configured templates for common tasks, such as web
development, data access, security, and messaging, which simplify dependency
management.
7. Spring Boot Actuator: Provides production-ready features to help monitor and
manage applications, including endpoints for health checks, metrics, and environment
information.
8. Externalized Configuration: Supports a variety of configuration sources, including
properties files, YAML files, environment variables, and command-line arguments,
which can be used to configure applications.
9. Logging: Provides a default logging configuration and supports various logging
frameworks like Logback, Log4j2, and Java Util Logging.
10. Spring DevTools: Enhances the development experience with features like automatic
restart, live reload, and configurations for faster feedback.
11. Spring Boot Security: Simplifies the integration of Spring Security into the
application by providing auto-configuration for basic authentication, form-based
login, and more.
12. Database Initialization: Supports database initialization with schema generation and
data loading from SQL scripts.