Spring Boot Questions
Spring Boot Questions
@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
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
@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.
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.
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.
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 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
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.
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.