0% found this document useful (0 votes)
21 views

Spring Boot Questions

Uploaded by

ahmed.high212
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Spring Boot Questions

Uploaded by

ahmed.high212
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

@Bean: Used at the method level to declare a bean to be used by the Spring container

@Repository: Indicates that an annotated class is a repository, which encapsulates the logic for
accessing data sources.

@Service: Indicates that an annotated class is a service, which holds business logic.

. @Controller: Indicates that a particular class serves the role of a controller. @RestController is a
specialized version that handles RESTful web requests

The Entity Annotation

Let’s say we have a POJO called Student, which represents the data of a student, and we would like to store it in the database:

So let’s define it by making use of the @Entity annotation. We must specify this annotation at the class level

@Entity

The Id Annotation

Each JPA entity must have a primary key that uniquely identifies it. The @Id annotation defines the primary key

The Table Annotation

@Table(name="STUDENT"
the name of the table in the database and the name of the entity won’t be the same.
In these cases, we can specify the table name using the @Table annotation:
Q1. What is Spring MVC?
Spring MVC is a module in the Spring Framework that implements the Model-View-Controller
design pattern for building web applications.
It simplifies the development process by providing a flexible and configurable way to handle HTTP
requests and responses.

What is the use of @Controller annotation.


The @Controller annotation in Spring MVC is used to mark a class as a Spring Web controller.
This annotation allows the Spring container to automatically detect the annotated class and register it as
a controller in the application context, enabling it to handle requests.

Q6. Difference between @RequestParam and @PathVariable.


@RequestParam is used to extract query parameters from the request URL,
while @PathVariable is used to extract values from the URI path within a URL.
For example, @RequestParam reads "id" from /user?id=123, while @PathVariable reads "id" from
/user/123.

Q7. Explain @Service and @Repository annotations. @Service and @Repository are stereotypes in
Spring, marking classes at the service layer and repository (or DAO Data Access Object) layer,
respectively.
@Service is used for service classes, and @Repository is used for database access classes, enabling
Spring's data handling support and exception translation

Q8. What is the purpose of @ModelAttribute? The @ModelAttribute annotation is used to bind
method parameters or method return values to a named model attribute, exposed to a web view.

Q12. Explain @GetMapping and @PostMapping. @GetMapping and @PostMapping are specialized
versions of the @RequestMapping annotation that specifically handle HTTP GET and POST requests,
respectively.

Q13. How to send the data from Controller to UI. Data can be sent from a Controller to the UI by adding
attributes to the Model object or using a ModelAndView object. These attributes are then accessed in
the view layer, typically through JSPs or other view templates, to render the response.

Q16. What do you know about Thymeleaf? Thymeleaf is a modern server-side Java template engine for
web and standalone environments. It is often used with Spring MVC as it provides a natural templating
capability that can process HTML, XML, JavaScript, CSS, and text.

Q19. How will you map the incoming request to a Controller method. Incoming requests are mapped
to controller methods by using annotations such as @RequestMapping, @GetMapping, and
@PostMapping, which specify the URL patterns and HTTP methods that the methods are responsible for
handling.

Q20. How to bind the form data to Model Object in Spring MVC.
Form data is bound to model objects in Spring MVC using @ModelAttribute annotation. This
automatically populates a model object with request parameters matching the object's field names.

Q22. Difference between @Controller and @RestController annotations.


@Controller is typically used for view-oriented applications where the controller returns a view.
@RestController combines @Controller and @ResponseBody, simplifying the creation of Restful web
services by handling responses without relying on view resolvers.

Spring Framework
What is Spring?
Spring is a Java framework that helps in building enterprise applications. It provides support for
dependency injection, aspect-oriented programming, and various other features.

What are the advantages of the Spring framework?


Spring simplifies development, promotes good coding practices, enables modular development,
provides easy testing, and offers integration with other frameworks.

Difference between Spring and Spring Boot?


Spring is a framework for building Java applications, while Spring Boot is a tool that simplifies the setup
and configuration of Spring-based applications.

What is IOC and DI?


IOC (Inversion of Control) means objects do not create other objects; instead, they are provided with
their dependencies.
DI (Dependency Injection) is a way of implementing IOC.

Difference between Constructor Injection and Setter Injection? Constructor Injection injects
dependencies through a constructor, while Setter Injection uses setter methods to inject dependencies
after the object is instantiated.

What is Maven and what problem does it solve?


Maven is a build automation tool used primarily for Java projects. It simplifies and standardizes the build
process, manages dependencies, and provides project structure conventions.
What is a POM file in Maven? POM (Project Object Model) is an XML file that contains project
information and configuration details required by Maven for building the project. It includes
dependencies, plugins, and other settings.

What is a Maven Repository? A Maven repository is a directory where all project jars, library jar,
plugins, or any other project specific artifacts are stored and can be easily used by Maven

Basic Spring Boot Interview Questions

5) Why do we prefer Spring Boot over Spring?


Spring Boot is preferred over traditional Spring because it requires less manual configuration and setup,
offers production-ready features out of the box like embedded servers and metrics, and simplifies
dependency management. This makes it easier and faster to create new applications and microservices,
reducing the learning curve and development time.

12) What is Auto-wiring? Autowiring in spring is the process by which Spring automatically injects the
dependencies of objects into one another. It eliminates the need for manual bean wiring and makes the
code cleaner and easier to maintain

11) What is a Spring Bean? A: In Spring, a bean is an object that is instantiated, assembled, and
managed by Spring IoC (Inversion of Control) container.
Beans are created with the configuration metadata that we provide in the Spring configuration file or
annotations.

24) Explain @RestController annotation in Spring Boot. The @RestController annotation in Spring Boot
is used to create RESTful web controllers. This annotation is a convenience annotation that combines
@Controller and @ResponseBody, which means the data returned by each method will be written
directly into the response body as JSON or XML, rather than through view resolution.

25) Difference between @Controller and @RestController The key difference is that @Controller is
used to mark classes as Spring MVC Controller and typically return a view. @RestController combines
@Controller and @ResponseBody, indicating that all methods assume @ResponseBody semantics by
default, returning data instead of a view.

26) What is the difference between RequestMapping and GetMapping? @RequestMapping is a general
annotation that can be used for routing any HTTP method requests (like GET, POST, etc.), requiring
explicit specification of the method. @GetMapping is a specialized version of @RequestMapping that is
designed specifically for HTTP GET requests, making the code more readable and concise.
37) What is dependency Injection and its types? Dependency Injection is a design pattern used in
software development where objects receive their dependencies from external sources rather than
creating them internally. In Spring, the two primary types of dependency injection are Constructor
Injection, where dependencies are provided through the constructor, and Setter Injection, where
dependencies are set through JavaBean-style setter methods.

38) What is an IOC container? An Inversion of Control (IOC) container is a framework that manages the
creation, lifecycle, and configuration of application objects. It injects dependencies as needed, managing
both the setup and the wiring of application components, thus promoting loose coupling and
modularity.

39) What is the difference between Constructor and Setter Injection?


Constructor Injection injects dependencies through a constructor of a class, ensuring that an object is
always created with all its dependencies, while Setter Injection uses setter methods to inject
dependencies after the object is instantiated..

40) Explain the need of dev-tools dependency


It enables automatic restarts of our application when code changes are detected, which is faster than
restarting manually. It also offers additional development-time checks to help us catch common
mistakes early.

Explain Cross-Origin Resource Sharing (CORS) and how you would configure it in a Spring Boot
application.
Cross-Origin Resource Sharing allows a website to safely access resources from another website.
In Spring Boot, we can set up CORS by adding @CrossOrigin to controllers or by configuring it globally.
This tells our application which other websites can use its resources, what type of requests they can
make, and what headers they can use.
This way, We control who can interact with our application, keeping it secure while letting it
communicate across different web domains.
Q #4) Write a Java Program to iterate HashMap using While and advance for loop.
Q #14) Write a program that accepts comma-separated strings, sorts the strings in ascending order, and
outputs the concatenated string of sorted strings.
Q #22) Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

You might also like