SpringBoot Flashcards
What is a Spring boot?
Spring Boot is a tool that makes developing a web application and microservices with Spring Framework faster and easier through three core capabilities that are
Autoconfiguration
An opinionated approach to configuration
The ability to create standalone applications
Purpose of Spring boot?
To avoid XML Configuration completely.
To avoid defining more Annotation Configurations.
To provide some defaults to quickly start new projects.
To provide an Opinionated Development approach.
Features of spring boot?
In-built starter projects.
In-built WebServers.
RestTemplate supports.
JSON Support.
Third-Party Integration Support.
Why Spring Boot over Spring?
Actuators.
Version Management.
Auto Configuration.
Component Scanning.
Embedded server.
InMemory DB.
Starter POM.
How the Spring boot application will use RestTemplate to test HTTP-based restful web services.
Spring RestTemplate class is part of spring-web, introduced in Spring 3. We can use RestTemplate to test HTTP-based restful web services, it doesn’t support the HTTPS protocol. RestTemplate class provides overloaded methods for different HTTP methods, such as GET, POST, PUT, DELETE, etc. The Spring Framework 5, onwards we have the WebFlux stack, Spring introduced a new HTTP client called WebClient. It is an alternative HTTP client to RestTemplate. Not only does it provide a traditional synchronous API, but it also supports an efficient nonblocking and asynchronous approach.
What is a Spring boot Actuator?
Spring Boot Actuator is a component of the Spring Boot framework that offers features for monitoring and managing your application in production. It provides endpoints to retrieve information and perform operations on your application. Key features include health monitoring, metrics gathering, environment details, logging management, thread dumps, and endpoint customization. Actuator simplifies the monitoring and management process by providing out-of-the-box functionality and integration capabilities with other monitoring tools.
How we monitor and manage the Spring boot application.
To monitor and manage a Spring Boot application, you can use Spring Boot Actuator. It provides management endpoints that offer health checks, metrics, logging management, thread dumps, and more. You can access these endpoints over HTTP to monitor the application’s health, gather performance metrics, view environment details, manage logging, and analyze thread dumps. Actuator can be easily integrated into your Spring Boot application and customized according to your specific needs.
How the Spring boot application will Auto-restart means reloading Java classes and configuring it on the server side, and loading the modified code.
DevTools stands for Developer Tool. The module aims to try and improve the development time while working with the Spring Boot application. Spring Boot DevTools pick up the changes and restart the application.
Automatic Restart: Auto-restart means reloading Java classes and configuring them on the server side. After the server-side changes, it is deployed dynamically, server restarts happen, and load the modified code.
LiveReload: The Spring Boot DevTools module includes an embedded server called LiveReload. It allows the application to automictically trigger a browser refresh whenever we make changes in the resources.
Here we will see what is spring boot environments, and how we can work with the same application but different configurations.
Spring Boot allows us to externalize configuration so we can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction, or bound to structured objects via @ConfigurationProperties. Spring Boot uses a very particular @PropertySource order that is designed to allow sensible overriding of values. What happens when our web application has a parent and a child context? The parent context may have some common core functionality and beans, and then one (or multiple) child contexts, maybe containing servlet-specific beans.
In that case, what’s the best way to define properties files and include them in these contexts? And how to best retrieve these properties from Spring?
If the file is defined in the Parent context:
@Value works in Child context: YES @Value works in Parent context: YES environment.getProperty in Child context: YES environment.getProperty in Parent context: YES If the file is defined in the Child context:
@Value works in Child context: YES @Value works in Parent context: NO environment.getProperty in Child context: YES environment.getProperty in Parent context: NO
MVC Architecture
Model: The component that deals with all the data-related logic or business logic is Model
View: This component deals with the UI logic of the application.
Controller: It is an interface between Model and View. It is used to process business logic and incoming requests, manipulate data using the Model component and interact with the Views to give the final output.
Define spring annotations
In Spring Boot, annotations are used to define various aspects of your application, such as configuring beans, handling requests, enabling security, and more. Here are some commonly used annotations in Spring Boot:
@SpringBootApplication: This annotation is used to indicate the main class of a Spring Boot application. It combines three annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan.
@RestController: This annotation is used to define a RESTful web service controller. It combines @Controller and @ResponseBody annotations.
@RequestMapping: This annotation is used to map HTTP requests to specific controller methods. It can be applied at the class level or method level to define the base URL or URL patterns for the controller.
@Autowired: This annotation is used for automatic dependency injection. It allows Spring to automatically wire the dependencies by matching the type of the dependency with a bean in the application context.
@Service: This annotation is used to define a service bean. It is typically applied to the service layer classes in your application.
@Repository: This annotation is used to define a repository bean. It is typically applied to the classes that interact with the database or external data sources.
@Configuration: This annotation is used to indicate that a class defines one or more beans. It is often used in conjunction with @Bean to define custom beans in the application context.
@EnableAutoConfiguration: This annotation enables Spring Boot’s auto-configuration feature. It automatically configures the beans and components based on the dependencies and the classpath.
@ComponentScan: This annotation is used to specify the base package(s) for component scanning. It scans the specified package(s) and its sub-packages for Spring components and beans.
@Transactional: This annotation is used to define a transactional method or class. It ensures that the annotated method or all methods of the annotated class run within a transactional context.
@Controller
@Controller annotation is a specialization of the @Component annotation.
@Controller replaces the process of expanding any controller base class or referencing the Servlet-specific features.
The class annotated with @Controller has methods mapped using @RequestMapping.
The annotated controller bean may be defined explicitly in the dispatcher’s context.
The classes annotated with @Controller are automatically detected by component scanning.
To define @RequestMapping annotation
A class annotated with@Controller has methods annotated with @RequestMapping. The DipatcherServlet will scan such annotated classes to detect the @RequestMapping.
The @RequestMapping is a class and method level annotation used to map web requests to spring controller methods.
value is used to define the path or URL.
method is used to define either GET or POST or both.
@RequestParam
@RequestParam extracts values from the query string.
It is encoded as the values are taken from a query string but not directly from the path.
@PathVariable
@PathVariable extracts values from the URI path.
As it extracts values from the URI path, it is not encoded.