Spring Boot
1. Why Spring Boot is used ?
The main Goal of Spring Boot is to quickly create production-ready-spring based applications
without requiring developers to write same boilerplate configuration again and again.
[Link] are spring boot auto-configuration ?
Spring Boot auto-configuration attempts to automatically configure your spring application
based on the jar dependencies that you have added.
• @ConditonalOnClass : used to enable DispatcherServletClass present in classpath.
3.
What are Spring Boot Starters
• a collection of dependency descriptors that can be used to bootstrap Spring
applications. They provide a one-stop-shop for all the Spring and related technology
needed to develop an application.
4. what are Actuators
• Spring Boot Actuator is a sub-project of Spring Boot that provides production-ready tools
for managing and monitoring applications
Rest API :
• Get – Retrieve Put - Update Post – Create Delete - Delete
@Controller : To make a simple java class as a Spring WEB MVC class we have to use
@Controller so that it can handle web requests sent by client.
@ResponseBody : This annotation is used to convert java object into XML or JSON format
and display to the client.
@RestController : combination of @Controller + @ResponseBody.
@PathVariable : It extracts values from URI Path and its not encoded.
@RequestParam : It extracts values from query string and its encoded.
• @RequestParam is used to bind query parameters (or) web request parameters to
method argument.
Q) How to set another server into SpringBoot to run the application and How to
remove/disable existing tomcat server
• To change server port number we have to use
[Link]=”Give u r own port number” in
[Link] file.
• To set another server into Spring Boot :
• To remove/disable existing tomcat server :
Q) How to run Spring Boot Application From Command Line
• By using java –jar command
• mvnw package
• cd target
• dir
•
• By using Spring Boot maven plugin
• mvnw spring-boot:run
Spring Boot Dev Tools Dependency Usage:
If we perform any changes to our controller class server will not get automatically started we
have to restart the server for reflecting those changes. To overcome that problems we use
Spring Dev Tools.
If we perform any changes to our controller class server will get automatically restarted.
Describe Rest API Http Methods and their Status Codes ?
@RequestBody : This annotation indicates that Spring should deserialize a request body into
an object.
(It converts if the request body is in the form of JSON/XML into JAVA object).
@Response Body : It converts JAVA Object into JSON/XML.
ResponseEntity : ResponseEntity is the class in the Spring Framework that represents the
entire HTTP response, including the status code, headers, and body.
Difference between @Controller & @RestController
• @Controller: used to declare common web controllers that can return HTTP
responses
• @RestController: used to create controllers for REST APIs that can return JSON
responses.
• What are differences b/w Spring Web MVC & Spring Boot
Spring MVC and Spring Boot are both frameworks that are part of the Spring framework, but
they have different purposes and are used in different ways:
• Purpose
Spring MVC is used to build web applications, while Spring Boot is used to build
standalone applications that are ready for production.
• Ease of use
Spring Boot is more beginner-friendly and requires less configuration than Spring MVC.
• Development time
Spring Boot reduces development time, while Spring MVC takes longer to develop due to
the need for manual configuration.
• Deployment
Spring Boot applications can be deployed as standalone applications or as a war file,
while Spring MVC applications require manual deployment.
• Configuration
Spring Boot provides default configurations that can be overridden, while Spring MVC
requires manual configuration.
• Dependency management
Spring Boot uses a starter POM system to manage dependencies automatically, while
Spring MVC requires manual dependency management.
• Testing
Spring Boot provides testing tools and configuration out-of-the-box, while Spring MVC
requires manual setup of a testing environment.
• Suitability
Spring Boot is well-suited for small to medium-sized projects and microservices, while
Spring MVC is better suited for complex and large-scale enterprise projects.
• what is dispatcher servlet ?
• The Dispatcher Servlet is a central component in the Spring MVC web framework that
manages HTTP requests and responses. It acts as a front controller.
• what is InternalResourceViewResolver ?
• Maps logical view names to actual view files in a web application's directory
• Difference b/w get and load method ?
• Difference b/w save and persist method ?
• Difference b/w composition and aggregation ?
• Without existence of container object if there is no chance of existing of contained
object then we can say container object & contained object are strongly associated with
each other. This process is called as Composition.
• Without existence of container object if there is a chance of existing of contained object
then we can say container object & contained object are weekly associated with each
other. This process is called as Aggregation.
• Differences between @Bean & @Autowired ?
• @Autowired: Spring provides annotation-based auto-wiring by providing @Autowired
annotation. It is used to autowire spring bean on setter methods, instance variable, and
constructor
• Difference b/w @Autowire and @Qualifier ?
• @Autowired is for automatic dependency injection. Spring provides annotation-based
auto-wiring by providing @Autowired annotation. It is used to autowire spring bean on
setter methods, instance variable, and constructor
• @Qualifier annotation is used to specify which bean to inject when there are multiple
beans of the same type. It is used to resolve ambiguity.
• Difference b/w session and sessionFactory method ?
• SessionFactory is a factory class for Session objects. It is available for the whole
application while a Session is only available for particular transaction. Session is short-
lived while SessionFactory objects are long-lived. SessionFactory provides a second level
cache and Session provides a first level cache.
• How to configure second level cache ?
• what are Spring bean scopes, explain Prototype bean, what is difference
between session & global session ?
• How may ways to inject Dependency injection ?
• Constructor Dependency Injection
• If we inject dependent values into bean object by using Constructor then , it is called
as Constructor Dependency Injection.
• Setter Method Dependency Injection
• If we inject dependent values into bean object by using setter method then it is
called as Setter Method Dependency Injection.
• difference b/w xmlBeanfactory & Application Context ?
• what is difference between @controller and @restcontroller ?
• difference between @component and @bean ?
• @Component auto detects and configures the beans using classpath scanning whereas
@Bean explicitly declares a single bean, rather than letting Spring do it automatically.
• @Component is a class level annotation whereas @Bean is a method level annotation
and name of the method serves as the bean name.
• @Component need not to be used with the @Configuration annotation where as
@Bean annotation has to be used within the class which is annotated with
@Configuration.
• We cannot create a bean of a class using @Component, if the class is outside spring
container whereas we can create a bean of a class using @Bean even if the class is
present outside the spring container.
• @Component has different specializations like @Controller, @Repository and @Service
whereas @Bean has no specializations.
• Can we use @component and @bean in same class?
• What is the difference between @component and @configuration
annotation?
• @Configuration : The @Configuration annotation in Spring is a class-level annotation
that indicates that a class is a source of bean definitions
• what is starter dependency ?
• Before Spring Boot was introduced, Spring Developers used to spend a lot of time on
Dependency management. Spring Boot Starters were introduced to solve this problem
so that the developers can spend more time on actual code than Dependencies. Spring
Boot Starters are dependency descriptors that can be added under the <dependencies>
section in [Link].
• what will be default value if you are using @Value for a variable ?
• what are actuators you have used to check monitoring ?
• How do we handle exception in springboot ?
• How would you implement caching in a spring boot application ?
• How to call Rest Appi from your spring boot application ?
• What are http methods used REST API?
• HTTP methods such as GET, POST, PUT, PATCH, and DELETE are used in RESTful API
development to specify the type of action being performed on a resource.
• what are http methods & status codes ?
• HTTP methods are used to request information from a server, and
HTTP status codes indicate the result of the request.
• Difference between POST, PUT AND PATCH METHODS ?
• what is idempotent & explain ?
• Difference between @PathVariable and @RequestParam Annotations ?
• @RequestParam : Extracts data from query parameters in the request URL.
• @PathVariable : Extracts data from URI templates in the URL path.
• Define Abstract Controller
• Abstract controller class is used to prepare Controller classes where no
form data submission in request and displays dynamic content through
web pages.
• Define Parameterizable View Controller
• Spring framework has provided this controller class in the form of
"[Link]"
.
• The main intention of this controller class is to display web pages with
out processing request.
• In ParameterizableViewController classes, it is not required to define
any user defined controller class, rather , it must be configured in
Spring configuration file directly and it will take "viewName" property
with the logical name of the view name inorder to open JSP page.
IN general, we will use mechanism when we want to get some web
pages just by clicking on hyper links.
• Define Multi-Action Controller
• In general, in Spring WEB MVC applications, for each and every form or
an URI we will prepare a separate Controller class depending on our
requirement.
• In the above approach, if we want to provide a particular Entity related
actions like save, update, search and delete then we have to prepare
seperate Controller classes like SaveController, UpdateController,
SearchController, DeleteController... it will increase no of controller
classes unnecessarily.
• In the above context, to reduce no of controller classes, Spring
Framework has provided an alternative in the form of
"[Link]
r". The main intention of MultiActionController class is to group related
actions into a single controller class. If we want to use MultiAction
controller class in Spring WEB MVC applications then we have to
declare an user defined class as sub class to "MultiActionController"
and we have to define action methods with the same incoming URI
names.
• Define Command Class
• Command Class is a normal Java Bean class, it can be instantiate by
Spring WEB MVC framework inorder to store form data which is
submitted by the respective Client along with request.
• Define Abstract Command Controller
• This controller class will populate form data in Command class objects
automatically by creating Command class object for each and every
request. In general, we will use Controller class when we have form
submission from client and when we don’t want to perform Data
Validations at Server side.
• Define Abstract Form Controller
• Define Simple Form Controller
• Define Abstract Wizard Form Controller