0% found this document useful (0 votes)
23 views118 pages

Unit4 Teena

Uploaded by

Abhay Pratap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views118 pages

Unit4 Teena

Uploaded by

Abhay Pratap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 118

Noida Institute of Engineering and Technology,

Greater Noida

Advanced Java Programming(ACSE0601)

Unit: 4
Ms. Teena
Spring MVC & Spring Boot Assistant Professor
CSE Department
Course Details
(B Tech VI Sem / 3rd Year)

Ms.Teena ACSE0601 Advanced Java Programming


17/11/2024 1
Evaluation Scheme

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 2


Evaluation Scheme

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 3


Syllabus

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 4


Syllabus

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 5


Branch-wise Applications

Advanced Java can be used :


• Design/Develop Program
• Develop appropriate data model and database scheme
• Create and test prototypes
• Develop Structure
• Identify major subsystems and interfaces
• Validate design scheme and models
• Implement Program
• Write code
• Perform unit testing
• Integrate subsystems
• Resolve defects and revise and adapt existing code
• Perform Analysis
• Define system and software requirements
• Develop test requirements
• Test and Validate the Program
• Develop test procedures
• Perform tests

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 6


Course Objectives

•• Develop
Develop error-free,
error-free,well-documented
well-documentedJava Java programs;
programs; develop
develop andand
test Java
test Javasearch
network, network, search
engine, engine,
and web and web
framework framework
programs. programs.
Learn how to write, test,
Learn how to write, test, and debug advanced-level Object-Oriented
and debug advanced-level
programs using Java. Object-Oriented programs using Java.
• Develop error-free, well-documented Java programs; develop and test Java
• network,
to provide the ability
search engine,toand
design console based,
web framework GUI based
programs. Learn ,web
how tobased
write, test,
applications,
and integratedwell-documented
debug error-free, development environment to create,
Java programs; debug
develop andand
test Java
run multi-tier and enterprise-level application
network, search engine, and web framework programs. Learn how to write, test,
and debug advanced-level Object-Oriented programs using Object-Oriented
programs using Java.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 7


Course Outcomes

After completion of this course students will be able to:

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 8


Program Outcomes

After completion of this course students will be able to:

PO1 To understand the concept of web application and latest


frameworks of Java.
PO2
To demonstrate the Java programs using OOP principles and
also implement the concepts of Spring and Spring Boot.

PO3
To demonstrate, understand and use of different concept of
MVC, JDBC, Spring etc.

PO4 To solve the real time problems using using advance concept
of Java.
PO5
To understand the JPA and entities relationship.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 9


CO-PO Mapping

Mapping of Course Outcomes and Program Outcomes:

PO PO PO PO PO PO PO PO PO PO PO PO
1 2 3 4 5 6 7 8 9 10 11 12

CO.1 3 3 3 2 1 2 3

CO.2 3 3 3 2 1 2 3

CO.3 3 3 3 2 1 2 3

CO.4 3 3 3 2 1 2 3

CO.5 3 3 3 2 1 2 3

Ms.Teena ACSE0601 Advanced Java Programming


17/11/2024 10
Program Specific Outcomes

On successful completion of graduation degree the Engineering


graduates will be able to:
PSO1: The ability to identify, analyze real world problems and design
their ethical solutions using artificial intelligence, robotics,
virtual/augmented reality, data analytics, block chain technology,

and cloud computing.


PSO2:The ability to design and develop the hardware sensor devices
and related interfacing software systems for solving complex
engineering problems.
PSO3:The ability to understand inter disciplinary computing
techniques and to apply them in the design of advanced
computing.
PSO4: The ability to conduct investigation of complex problem with
the help of technical, managerial, leadership qualities, and
modern engineering tools provided by industry sponsored
laboratories.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 11


CO-PSO Mapping

Mapping of Course Outcomes and Program Specific Outcomes:

PSO1 PSO2 PSO3 PSO4


Co.1 2 3 2 1

CO.2 2 3 2 1

CO.3 2 3 2 1

CO.4 2 3 2 1

CO.5 2 3 2 1

Ms.Teena ACSE0601 Advanced Java Programming


17/11/2024 12
Program Educational Objectives

PEO1: To have an excellent scientific and engineering breadth so as


to comprehend, analyze, design and provide sustainable
solutions for real-life problems using state-of-the-art
technologies.
PEO2:To have a successful career in industries, to pursue higher
studies or to support enterpreneurial endeavors and to face
global challenges.
PEO3:To have an effective communication skills, professional
attitude, ethical values and a desire to learn specific
knowledge in emerging trends, technologies for research,
innovation and product development and contribution to
society.
PEO4: To have life-long learning for up-skilling and re-skilling for
successful professional career as engineer, scientist,
enterpreneur and bureaucrat for betterment of society.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 13


End Semester Question Paper Templates (Offline
Pattern/Online Pattern

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 14


End Semester Question Paper Templates (Offline
Pattern/Online Pattern

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 15


End Semester Question Paper Templates (Offline
Pattern/Online Pattern

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 16


End Semester Question Paper Templates (Offline
Pattern/Online Pattern

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 17


End Semester Question Paper Templates (Offline
Pattern/Online Pattern

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 18


End Semester Question Paper Templates (Offline
Pattern/Online Pattern

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 19


End Semester Question Paper Templates (Offline
Pattern/Online Pattern

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 20


Prerequisite and Recap

• Student must know at least the basics of Java programming ,able to


start coding on any IDE.
• Knowledge of object oriented techniques using Java concepts, as
covered in ‘Programming Basic” course is necessary.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 21


About the Subject with Videos

• Advanced Java Programming refers to languages that uses objects in programming.


• Advanced Java Programming aims to implement real-world entities like inheritance, hiding, polymorphism
etc in programming.
• The main aim of Advanced Java Programming is to bind together the data and the functions that operate on
them so that no other part of the code can access this data except that function

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 22


About the Subject with Videos

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 23


UNIT OBJECTIVE

• This course gives the fundamental knowledge of


Spring Model View Controller and Spring Boots
• The unit will help the students to understand
how the approaches of threads help in solving
real life problems.
• Basic understanding of Java Framework like
Spring and Spring Boots used.
• Overview of creating custom annotations and
using them in programs.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Progra 24
mming
UNIT CONTENT
Spring MVC
• Introduction/Developing Web Application with Spring MVC
• Advanced Techniques
• Spring Controllers

Spring Boot
• Spring Boot Starters
• CLI
• Application Class
• Logging
• Auto Configuration Classes
• Spring Boot Dependencies
• Spring data JPA introduction and Overview

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 25


Spring MVC: Introduction

• Spring MVC framework is an open-source Java platform that provides


comprehensive infrastructure support for developing robust Java-based
Web applications very easily and very rapidly.
• Spring framework was initially written by Rod Johnson and was first
released under the Apache 2.0 license in June 2003.
• It follows the Model-View-Controller design pattern. It implements all the
basic features of a core spring framework like Inversion of Control, and
Dependency Injection.
• A Spring MVC provides an elegant solution to use MVC in spring
framework with the help of DispatcherServlet.
• DispatcherServlet is a class that receives the incoming request and maps
it to the right resource such as controllers, models, and views .

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 26


Spring Web Model-View-Controller
(MVC)

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 27


MVC

• Model - A model contains the data of the application. A data can be a single object
or a collection of objects. The Model encapsulates the application data and in
general, they will consist of POJO.

• View - A view represents the provided information in a particular format. Generally,


JSP+JSTL is used to create a view page. Although spring also supports other view
technologies such as Apache Velocity, Thymeleaf and FreeMarker. The View is
responsible for rendering the model data and in general, it generates HTML output
that the client's browser can interpret.

• Controller - A controller contains the business logic of an application. Here, the


@Controller annotation is used to mark the class as the controller. The Controller is
responsible for processing User Requests and Building Appropriate Model and
passes it to the view for rendering.

• Front Controller - In Spring Web MVC, the DispatcherServlet class works as the front
controller. It is responsible to manage the flow of the Spring MVC application.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 28
Working of MVC Framework

• Spring MVC Framework works as follows:


1. All the incoming requests are intercepted by the
DispatcherServlet that works as the front controller.
2. The DispatcherServlet then gets an entry of handler
mapping from the XML file and forwards the request to the
controller.
3. The object of ModelAndView is returned by the controller.
4. The DispatcherServlet checks the entry of the view resolver
in the XML file and invokes the appropriate view
component.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 29


Daily Quiz - MCQ

1. What does MVC stand for in Spring MVC?


a) Multi-View Controller
b) Module-View-Controller
c) Model-View-Controller
d) Model-Value-Configuration

Answer:
c) Model-View-Controller
Explanation:
MVC stands for Model-View-Controller, which is a design pattern to separate an
application's concerns.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 30


Daily Quiz - MCQ

2. What is Spring MVC


a) A programming language for web development.
b) A lightweight Java framework for building web applications.
c) A database management system for web applications.
d) A library for handling server-side rendering.

Answer:
b) A lightweight Java framework for building web applications.
Explanation:
Spring MVC is a lightweight Java framework built on top of the Spring
Framework. It provides a flexible architecture for developing web applications
based on the Model-View-Controller design pattern. Spring MVC simplifies web
development by providing features such as request handling, view rendering,
and data binding.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 31


Daily Quiz - MCQ

3. Which annotation is used to create a Spring MVC controller class?


a) @Controller Bean
b) @Spring Controller
c) @Web Controller
d) @Controller

Answer:
d) @Controller
Explanation:
The @Controller annotation indicates that a class is a Spring MVC controller.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 32


Daily Quiz - MCQ

4. Which of the following handles the HTTP request in Spring MVC?


a) Dispatcher Servlet
b) Handler Interceptor
c) Http Listener
d) Request Handler

Answer:
a) Dispatcher Servlet
Explanation:
In Spring MVC, the Dispatcher Servlet is responsible for dispatching incoming
HTTP requests to the appropriate controller methods.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 33


Daily Quiz - MCQ
5. Which annotation binds a method parameter to a named attribute?
a) @Model Attribute
b) @Parameter Attribute
c) @Bind Attribute
d) @Request Value

Answer:
a) @Model Attribute
Explanation:
The @Model Attribute annotation is used to bind a method parameter to a
named attribute, potentially initializing the attribute from a database or other
source.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 34


Advantages of Spring MVC
Framework
• The container is used for the development and deployment
of applications and uses a lightweight servlet.
• It enables rapid and parallel development.
• Development of the application becomes fast.
• Easy for multiple developers to work together.
• Easier to Update the application.
• It is Easier to Debug because we have multiple levels in the
application.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 35


Disadvantages of Spring MVC
Framework
• It has high complexity to develop the applications using this
pattern.
• It is not suitable for small applications which affect the
application’s performance and design.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 36


Understanding the flow of Spring
Web MVC

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 37


Cont…

• As displayed in the figure, all the incoming request is


intercepted by the DispatcherServlet that works as the front
controller.
• The DispatcherServlet gets an entry of handler mapping
from the XML file and forwards the request to the controller.
• The controller returns an object of ModelAndView.
• The DispatcherServlet checks the entry of view resolver in
the XML file and invokes the specified view component.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 38


Daily Quiz - MCQ

6. How can you handle exceptions in Spring MVC?


a) @Exception Handler
b) @Catch Exception
c) @Error Resolver
d) @Resolve Error

Answer:
a) @Exception Handler
Explanation:
The @Exception Handler annotation is used to handle exceptions in Spring MVC.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 39


Daily Quiz - MCQ

7. What is the purpose of the Model And View object in Spring MVC?
a) To store and pass data between the controller and the view.
b) To define the layout and styling of the web application.
c) To handle user authentication and authorization.
d) To manage the application's data access layer.

Answer:
a) To store and pass data between the controller and the view.
Explanation:
The Model And View object in Spring MVC is used to store and pass data
between the controller and the view. It combines both the model and view
aspects, allowing the controller to add data to the model and specify the view
that should be rendered. The Model And View object serves as a container for
carrying data and view information during request processing in Spring MVC.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 40


Daily Quiz - MCQ

8. Which annotation is used to bind a method parameter to a web


request header?
a) @Request Header
b) @Header
c) @Bind Header
d) @HTTP Header

Answer:
a) @Request Header
Explanation:
The @Request Header annotation is used to bind a method parameter to a
specific web request header.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 41


Daily Quiz - MCQ
9. How can you access request parameters in a Spring MVC controller method?
a) By using the @Request Param annotation.
b) By accessing them directly from the Http Servlet Request object.
c) By using the @Path Variable annotation.
d) By using the @Request Header annotation.

Answer:
a) By using the @Request Param annotation.
Explanation:
In a Spring MVC controller method, you can access request parameters by using the
@Request Param annotation. This annotation binds a request parameter value to a
method parameter, making it easy to retrieve and process the parameter value in
the controller.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 42


Daily Quiz - MCQ

10. In Spring MVC, what is the role of the View Resolver?


a) Resolving bean dependencies
b) Handling exceptions
c) Resolving views to specific URLs
d) Handling request parameters

Answer:
c) Resolving views to specific URLs
Explanation:
View Resolver in Spring MVC helps in mapping view names to actual views (like
JSPs).

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 43


Directory Structure of Spring MVC

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 44


Directory Structure of Spring MVC
using Maven

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 45


The Dispatcher Servlet
• The Spring Web model-view-controller (MVC) framework is designed around a
DispatcherServlet that handles all the HTTP requests and responses. The
request processing workflow of the Spring Web MVC DispatcherServlet is
shown in the following illustration.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 46


Daily Quiz - MCQ

11. Which annotation is used to denote a regular expression in URI template in


Spring MVC?
a) @Regex
b) @Path Variable
c) @URI Expression
d) @Match Pattern
\
Answer:
b) @Path Variable
Explanation:
The @Path Variable annotation can be used to extract values from the URI, and
it supports regular expressions.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 47


Daily Quiz - MCQ

12. Which Spring MVC module provides integration with RESTful services?
a) Spring Rest Controller
b) Spring REST
c) Spring Web MVC
d) Spring Web Flux

Answer:
c) Spring Web MVC
Explanation:
While @Rest Controller is an annotation used to create REST ful controllers, it's the
Spring Web MVC module that provides integration with REST ful services.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 48


Daily Quiz - MCQ

13. Which annotation indicates a method should handle HTTP POST requests?
a) @Http Post
b) @Post Handler
c)@Request Mapping (method = Request Method.POST)
d) @Post Mapping

Answer:
d) @Post Mapping
Explanation:
The @Post Mapping is a composed annotation that acts as a shortcut for @Request
Mapping(method = Request Method.POST).

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 49


Daily Quiz - MCQ

14. What does the @Response Body annotation do?


a) It sends the return value of a method back to the web response body.
b) It binds the parameters of a method to the request body.
c) It triggers an exception handling method.
d) It binds the result of a method to a view.

Answer:
a) It sends the return value of a method back to the web response body.
Explanation:
The @Response Body annotation tells a controller that the return value of a
method should be written directly to the HTTP response body.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 50


Daily Quiz - MCQ

15. How do you redirect to another URL in Spring MVC?


a) "redirect:/url_path"
b) "forward:/url_path"
c) "goto:/url_path"
d) "move:/url_path"

Answer:
a) "redirect:/url_path"
Explanation:
In Spring MVC, to perform a redirect, you return a string starting with "redirect:"
followed by the path.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 51


Create Your First Spring MVC
Application

Step 0: Setup your project with maven use the


required archetype to get the required folders
directory and configure the server with your project.

Step 2: Create the Controller Class.


Step 3: Provide the name of the controller in the web.xml file
as follows:
DispatcherServlet is the front controller in Spring Web MVC.
Incoming requests for the HTML file are forwarded to the
DispatcherServlet.
Step 4: We have to define the bean in a separate XML file. We have specified
the view components in this file. It is located in the WEB-INF directory.
Step 5: Use JSP to display the message.
Step 6: Start the server and run the project.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 52


Spring Boot

• Spring Boot provides a number of starters that allow us to


add jars in the classpath. Spring Boot built-in starters make
development easier and rapid. Spring Boot Starters are
the dependency descriptors.
• In the Spring Boot Framework, all the starters follow a similar
naming pattern: spring-boot-starter-*, where * denotes a
particular type of application. For example, if we want to use
Spring and JPA for database access, we need to include
the spring-boot-starter-data-jpa dependency in
our pom.xml file of the project.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 53


What is Spring Boot

• Spring Boot is a project that is built on the top of the Spring


Framework. It provides an easier and faster way to set up,
configure, and run both simple and web-based applications.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 54


• It is a Spring module that provides the RAD (Rapid
Application Development) feature to the Spring
Framework. It is used to create a stand-alone Spring-based
application that you can just run because it needs minimal
Spring configuration.

• In short, Spring Boot is the combination of Spring Framework and Embedded


Servers.
• In Spring Boot, there is no requirement for XML configuration (deployment
descriptor). It uses convention over configuration software design paradigm that
means it decreases the effort of the developer.
• We can use Spring STS IDE or Spring Initializr to develop Spring Boot Java
applications.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 55


Difference b/w Spring MVC & Spring Boot
S.No. SPRING MVC SPRING BOOT

Spring MVC is a Model View, and


Spring Boot is built on top of the
Controller based web framework
1. conventional spring framework,
widely used to develop web
widely used to develop REST APIs.
applications.

If we are using Spring Boot, there is


If we are using Spring MVC, we need
2. no need to build the configuration
to build the configuration manually. manually.

In the Spring MVC, a deployment In the Spring Boot, there is no need


3.
descriptor is required. for a deployment descriptor.

Spring MVC specifies each It wraps the dependencies together


4.
dependency separately. in a single unit.

There are four main layers in Spring


Spring MVC framework consists of
5. four components : Model, View, Boot: Presentation Layer, Data
Access Layer, Service Layer, and
Controller, and Front Controller.
Integration Layer.

It reduces development time and


6. It takes more time in development.
increases productivity.

Spring MVC do not provide powerful Powerful batch processing is


7. batch processing. provided by Spring Boot.

Default configurations are provided


Ready to use feature are provided by
8. by it for building a Spring powered
it for building web applications.
framework.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 56


Daily Quiz - MCQ

16. Which of the following annotations is used to handle multipart file


uploads?
a) @File Upload
b) @Multipart File
c) @Request File
d) @Upload Part

Answer:
b) @Multipart File
Explanation:
The @Multipart File annotation is used to handle multipart file uploads in
Spring MVC.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 57


Daily Quiz - MCQ

17. How do you specify that a controller method should produce JSON as a
response?
a) @Produces("application/json")
b) @ResponseBody(type="json")
c) @ResponseFormat("JSON")
d) @RequestMapping(produces="application/json")

Answer:
d) @RequestMapping(produces="application/json")
Explanation:
The @RequestMapping annotation with the produces attribute set to
"application/json" specifies that the controller method should produce JSON as a
response.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 58


Daily Quiz - MCQ

18. Which of the following represents a form-backing bean in Spring MVC?


a) @FormEntity
b) @ModelEntity
c) @ModelAttribute
d) @BeanForm

Answer:
c) @ModelAttribute
Explanation:
@ModelAttribute can be used to represent a form-backing bean in Spring
MVC.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 59


Daily Quiz - MCQ
19. Which of the following components decides which controller method is to
be called for a request?
a) Dispatcher Servlet
b) Controller
c) Handler Mapping
d) View Resolver

Answer:
c) Handler Mapping
Explanation:
The Handler Mapping component decides which controller method should be
called based on the incoming request.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 60


Daily Quiz - MCQ

20. What does the @Valid annotation in Spring MVC do?


a) It ensures that the method is correctly overridden from a superclass.
b) It triggers validation of a method parameter or field.
c) It ensures that an HTTP request is valid.
d) It validates the return type of a method.

Answer:
b) It triggers validation of a method parameter or field.
Explanation:
The @Valid annotation triggers validation of the annotated method parameter
or field.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 61


Spring Boot CLI

• The Spring Boot CLI (Command-Line Interface) can be


installed manually by using SDKMAN! (the SDK Manager) or
by using Homebrew or MacPorts if you are an OSX user.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 62


Daily Quiz-MCQ

Q21. Spring Boot is used for developing?


1. Web applications
2. Distributed applications (Restful web services)
3. Microservices
4. All of the above

Answer:
4. All of the above
Explanation:
Spring boot is used to build Web applications, REST APIs, and
Microservices.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 63


Daily Quiz-MCQ

Q22. What is Spring Initializer?


1. Web-based tool to bootstrap Spring projects and generate project skeletons with
the necessary configurations and dependencies.
2. A command-line tool for generating a basic Spring Boot project structure
3. A tool for initializing a Spring-based application with a specific set of
dependencies
4. A tool for creating a Spring Boot application with a specific set of features

Answer:
1. Web-based tool to bootstrap Spring projects and generate project skeletons with
the necessary configurations and dependencies.
Explanation:
Spring Initializr is a web-based tool provided by the Spring team to bootstrap Spring
projects and generate project skeletons with the necessary configurations and
dependencies. It simplifies the process of creating a new Spring-based application
by providing an intuitive user interface to customize and generate project structures.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 64


Daily Quiz-MCQ

Q23. What does Spring Boot do to simplify the configuration of a


Spring-based application?
1. Automatically configures necessary beans and dependencies
2. Uses annotations to configure beans and dependencies
3. Allows for the use of XML configuration files
4. Provides a command line interface for configuring the application
Answer:
1. Automatically configures necessary beans and dependencies
Explanation:
Spring Boot's auto-configuration feature automatically configures the
application based on the classpath dependencies and predefined
conventions. It scans the classpath for libraries and frameworks and
configures the application accordingly, reducing the need for manual
configuration.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 65
Daily Quiz-MCQ

Q24. How can a Spring Boot application be packaged and


distributed?
1. As a JAR file
2. As a WAR file
3. As a ZIP file
4. All of the above
Answer:
4. All of the above
Explanation:
Spring Boot application can be distributed as JAR, WAR, and
Zip files.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 66


Daily Quiz-MCQ

Q25. Which Spring annotation is used to create RESTful web services


using Spring MVC?
1. @RestController
2. @Controller
3. @Component
4. @Rest

Answer:
1. @RestController
Explanation:
When you annotate a class with @RestController, it indicates that the
class is a Spring MVC REST controller responsible for handling incoming
HTTP requests and generating the appropriate HTTP responses.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 67


Daily Quiz-MCQ

Q26. @RestController annotation is a combination of the


below two annotations
1. @Component and @ResponseBody annotations
2. @Controller and @ResponseBody annotations
3. @Service and @ResponseBody annotations
4. None of the above
Answer:
2. @Controller and @ResponseBody annotations
Explanation:
@RestController combines the functionality of the
@Controller and @ResponseBody annotations into a single
annotation, making it convenient for creating RESTful APIs.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 68
Daily Quiz-MCQ

Q27. Which Spring annotation is used to handle HTTP POST


requests?
1. @GetMapping
2. @PutMapping
3. @CreateMapping
4. @PostMapping
Answer:
4. @PostMapping
Explanation:
The @PostMapping annotation is used to map an HTTP POST
request to a specific handler method in a Spring MVC controller. It
is a shortcut for specifying the @RequestMapping annotation with
the RequestMethod.POST method.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 69
Spring Boot Architecture

• Spring Boot is a module of the Spring Framework. It is used


to create stand-alone, production-grade Spring Based
Applications with minimum efforts. It is developed on top of
the core Spring Framework.
• Spring Boot follows a layered architecture in which each
layer communicates with the layer directly below or above
(hierarchical structure) it.
• Before understanding the Spring Boot Architecture, we
must know the different layers and classes present in it.
There are four layers in Spring Boot are as follows:
• Presentation Layer
• Business Layer
• Persistence Layer
• Database Layer

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 70


Creating a Spring Boot Project

• Following are the steps to create a simple Spring Boot


Project.
• Step 1: Open the Spring initializr https://start.spring.io.
• Step 2: Provide the Group and Artifact name. We have
provided Group name com.javatpoint and Artifact spring-
boot-example.
• Step 3: Now click on the Generate button.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 71


Creating a Spring Boot Project

• Following are the steps to create a simple Spring Boot


Project.
• Step 1: Open the Spring initializr https://start.spring.io.
• Step 2: Provide the Group and Artifact name. We have
provided Group name com.javatpoint and Artifact spring-
boot-example.
• Step 3: Now click on the Generate button.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 72


Cont…

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 73


Cont…

• When we click on the Generate button, it starts packing


the project in a .rar file and downloads the project.
• Step 4: Extract the RAR file.
• Step 5: Import the folder.
• File -> Import -> Existing Maven Project -> Next ->
Browse -> Select the project -> Finish
• It takes some time to import the project. When the
project imports successfully, we can see the project
directory in the Package Explorer. The following
image shows the project directory:

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 74


Cont…

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 75


SpringBootExampleApplication
.java
1. package com.javatpoint.springbootexample;
2. import org.springframework.boot.SpringApplication;
3. import org.springframework.boot.autoconfigure.SpringBoot
Application;
4. @SpringBootApplication
5. public class SpringBootExampleApplication
6. {
7. public static void main(String[] args)
8. {
9. SpringApplication.run(SpringBootExampleApplication.class,
args);
10.}
11.}

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 76


Daily Quiz - MCQ

Q28. Which Spring annotation is used to handle HTTP GET requests?


1. @GetMapping
2. @PutMapping
3. @CreateMapping
4. @PostMapping

Answer:
1. @GetMapping
Explanation:
The @GetMapping annotation is used to map an HTTP GET request to
a specific handler method in a Spring MVC controller. It is a shortcut
for specifying the @RequestMapping annotation with the
RequestMethod.GET method.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 77


Daily Quiz - MCQ

Q29. Which Spring annotation is used to handle HTTP DELETE


requests?
1. @GetMapping
2. @PutMapping
3. @DeleteMapping
4. @PostMapping
Answer:
3. @DeleteMapping
Explanation:
The @DeleteMapping annotation is used to map an HTTP DELETE
request to a specific handler method in a Spring MVC controller. It is a
shortcut for specifying the @RequestMapping annotation with the
RequestMethod.DELETE method.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 78


Daily Quiz - MCQ

Q30 hich Spring annotation is used to handle HTTP PUT requests?


1. @GetMapping
2. @PutMapping
3. @DeleteMapping
4. @PostMapping
Answer:
2. @PutMapping
Explanation:
The @PutMapping annotation is used to map an HTTP PUT request to
a specific handler method in a Spring MVC controller. It is a shortcut
for specifying the @RequestMapping annotation with the
RequestMethod.PUT method.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 79


Daily Quiz - MCQ

Q31. Which annotation do we use to mark the class as a Service


class/component?
1. @Component
2. @Service
3. @Controller
4. @Repository

Answer:
2. @Service
Explanation:
The @Service annotation is used in Spring Framework to mark a class
as a service component. It is typically applied to classes that perform
business logic, transaction management, or other services in an
application.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 80
Daily Quiz - MCQ

Q32. Which is the default implementation class of the JpaRepository


interface?
1. SimpleJpaRepository class
2. JpaRepositoryImpl class
3. CustomJpaRepository class
4. DefaultJpaRepository class
Answer:
1. SimpleJpaRepository class
Explanation:
The SimpleJpaRepository class is a default implementation of the
JpaRepository interface in Spring Data JPA. It is provided by the Spring
Data JPA framework and serves as a generic repository
implementation for performing common CRUD (Create, Read, Update,
Delete) operations and other database operations.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 81
Daily Quiz - MCQ

Q33. Which starter dependency is used to develop web applications


or Restful web services?
1. spring-boot-starter-data-jpa
2. spring-boot-starter-web
3. spring-boot-starter-rest
4. spring-boot-starter-web-dependency

Answer:
2. spring-boot-starter-web
Explanation:
The spring-boot-starter-web dependency provides the necessary
components to develop Web Applications and RESTful API in a Spring
Boot project, including the embedded Tomcat server, Spring Web
MVC, and other required dependencies.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 82
Spring Boot Application

• Spring Boot Framework comes with a built-in mechanism


for application configuration using a file
called application.properties. It is located inside
the src/main/resources folder.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 83


Spring Boot Auto-configuration
Classes
• Spring Boot auto-configuration automatically configures the
Spring application based on the jar dependencies that we have
added.
• For example, if the H2 database Jar is present in the classpath
and we have not configured any beans related to the database
manually, the Spring Boot's auto-configuration feature
automatically configures it in the project.
• We can enable the auto-configuration feature by using the
annotation @EnableAutoConfiguration. But this annotation
does not use because it is wrapped inside
the @SpringBootApplication annotation. The annotation
@SpringBootApplication is the combination of three
annotations: @ComponentScan,
@EnableAutoConfiguration, and @Configuration. However,
we use @SpringBootApplication annotation instead of using
@EnableAutoConfiguration.
• @SpringBootApplication=@ComponentScan+@EnableAut
oConfiguration+@Configuration
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 84
Cont…..

• When we add the spring-boot-starter-web dependency


in the project, Spring Boot auto-configuration looks for the
Spring MVC is on the classpath. It auto-
configures dispatcherServlet, a default error
page, and web jars.
• Similarly, when we add the spring-boot-starter-data-
jpa dependency, we see that Spring Boot Auto-
configuration, auto-configures a datasource and an Entity
Manager.
• All auto-configuration logic is implemented in spring-boot-
autoconfigure.jar, as shown in the following figure.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 85


Cont…

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 86


Need of auto-configuration
Spring-based application requires a lot of configuration. When we use Spring MVC, we need to
configure dispatcher servlet, view resolver, web jars among other things. The following
code shows typical configuration of a dispatcher servlet in a web application:
1. <servlet>
2. <servlet-name>dispatcher</servlet-name>
3. <servlet-class>
4. org.springframework.web.servlet.DispatcherServlet
5. </servlet-class>
6. <init-param>
7. <param-name>contextConfigLocation</param-name>
8. <param-value>/WEB-INF/todo-servlet.xml</param-value>
9. </init-param>
10.<load-on-startup>1</load-on-startup>
11.</servlet>
12.<servlet-mapping>
13.<servlet-name>dispatcher</servlet-name>
14.<url-pattern>/</url-pattern>
15.</servlet-mapping>

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 87


Configuring datasource:

1. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource


"
2. destroy-method="close">
3. <property name="driverClass" value="${db.driver}" />
4. <property name="jdbcUrl" value="${db.url}" />
5. <property name="user" value="${db.username}" />
6. <property name="password" value="${db.password}" />
7. </bean>
8. <jdbc:initialize-database data-source="dataSource">
9. <jdbc:script location="classpath:config/schema.sql" />
10.<jdbc:script location="classpath:config/data.sql" />
11.</jdbc:initialize-database>

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 88


Configuring entity manager
factory
1. <bean
2. class="org.springframework.orm.jpa.LocalContainerEntityM
anagerFactoryBean"
3. id="entityManagerFactory">
4. <property name="persistenceUnitName" value="hsql_pu"
/>
5. <property name="dataSource" ref="dataSource" />
6. </bean>

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 89


Configuration Transaction Manager

1. <bean id="transactionManager" class="org.springframewo


rk.orm.jpa.JpaTransactionManager">
2. <property name="entityManagerFactory" ref="entityMana
gerFactory" />
3. <property name="dataSource" ref="dataSource" />
4. </bean>
5. <tx:annotation-driven transaction-
manager="transactionManager"/>

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 90


Daily Quiz - MCQ

34. Which Spring annotation is used to handle Cross-Origin Resource Sharing


(CORS) in Spring MVC?
a) @CrossOrigin
b) @CORSHandler
c) @EnableCORS
d) @CORSConfig

Answer:
a) @CrossOrigin
Explanation:
The @CrossOrigin annotation is used in Spring MVC to handle Cross-Origin
Resource Sharing (CORS).

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 91


Daily Quiz - MCQ

35. Which Spring Boot Starter would you use for developing web applications?
a) spring-boot-starter-web
b) spring-boot-starter-jdbc
c) spring-boot-starter-data
d) spring-boot-starter-app

Answer:
a) spring-boot-starter-web
Explanation:
spring-boot-starter-web is used for building web, including RESTful, applications
using Spring MVC.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 92


Daily Quiz - MCQ

36. In which layer is Spring MVC used?


a) Data layer
b) Business layer
c) Presentation layer
d) Integration layer

Answer:
c) Presentation layer
Explanation:
Spring MVC is used in the presentation layer to handle web requests and construct
responses.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 93


Spring Data JPA

• What is JPA?
• Spring Boot JPA is a Java specification for
managing relational data in Java applications. It allows us to access
and persist data between Java object/ class and relational database.
JPA follows Object-Relation Mapping (ORM). It is a set of
interfaces. It also provides a runtime EntityManager API for
processing queries and transactions on the objects against the
database. It uses a platform-independent object-oriented query
language JPQL (Java Persistent Query Language).
• In the context of persistence, it covers three areas:
• The Java Persistence API
• Object-Relational metadata
• The API itself, defined in the persistence package.
• JPA is not a framework. It defines a concept that can be implemented
by any framework.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 94


Why should we use JPA?

• JPA is simpler, cleaner, and less labor-


intensive than JDBC, SQL, and hand-written
mapping. JPA is suitable for non-performance
oriented complex applications. The main
advantage of JPA over JDBC is that, in JPA,
data is represented by objects and classes
while in JDBC data is represented by tables
and records. It uses POJO to represent
persistent data that simplifies database
programming.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 95


Why should we use JPA?

• JPA is simpler, cleaner, and less labor-


intensive than JDBC, SQL, and hand-written
mapping. JPA is suitable for non-performance
oriented complex applications. The main
advantage of JPA over JDBC is that, in JPA,
data is represented by objects and classes
while in JDBC data is represented by tables
and records. It uses POJO to represent
persistent data that simplifies database
programming.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 96


Architecture Of JPA

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 97


JPA Class Relationships

17/11/2024 98
Ms.Teena ACSE0601 Advanced Java Programming
JPA Implementations

• JPA is an open-source API. There is various enterprises vendor such as


Eclipse, RedHat, Oracle, etc. that provides new products by adding the JPA
in them. There are some popular JPA implementations frameworks such
as Hibernate, EclipseLink, DataNucleus, etc. It is also known as Object-
Relation Mapping (ORM) tool.

17/11/2024 99
Ms.Teena ACSE0601 Advanced Java Programming
Daily Quiz - MCQ

1. JPA stands for ____?


Java persistence API
Java programming API
Java persistence Arguments
Answer: A) Java persistence API
Explanation:
JPA stands for Java persistence API.

2. JPA is used for ____?


For data access, persistence, and management in Java applications
For testing java applications
For creating runtime API
All of the above
Answer: A) For data access, persistence, and management in Java applications
Explanation:
JPA is used for data access, persistence, and management in Java applications.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 100


Daily Quiz - MCQ

3. A database table is represented by an ____, which is a Java class.


A. Persistence
B. Entity
C. Primary key
D. Relationships
Answer: B) Entity
Explanation:
A database table is represented by an entity, which is a Java class.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 101


Daily Quiz - MCQ

4. ____ is a class that allows you to persist, update, and delete entities.
A. Persistence
B. Entity
C. EntityManager
D. Bias
Answer: C) EntityManager
Explanation:
EntityManager is a class that allows you to persist, update, and delete entities.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 102


Daily Quiz - MCQ

5. Which of the following is an entity's unique identifier?


A. Transactions
B. Entity
C. Primary key
D. Relationships
Answer: C) Primary key
Explanation:
A primary key is an entity's unique identifier.

6. Is JPA an open-source API?


A. Yes
B. No
Answer: A) Yes
Explanation:
Yes, JPA is an open-source API.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 103


Daily Quiz - MCQ

7. In a relationship mapping, which of the following annotations is used to


indicate the join column?
@Join
@Column
@JoinColumn
All of the above
Answer: C) @JoinColumn
Explanation:
@JoinColumn annotation is used to specify the join column in relationship
mapping.

8. ____ annotation is used to specify a Query by providing a static name?


@list
@NamedQuery
@NamedQueries
@querieslist
Answer: B) @NamedQuery
Explanation:
@NamedQuery annotation Ms.Teena
17/11/2024 is usedACSE0601
to specify a Query
Advanced by providing a static name. 104
Java Programming
Daily Quiz - MCQ

9. Which of the following annotations is used to define a list of named queries?


@list
@NamedQuery
@NamedQueries
@querieslist
Answer: C) @NamedQueries
Explanation:
@NamedQueries annotation is used for specifying the list of named queries.

10. Which of the following annotations is used to indicate an entity's primary key
field?
@pk
@primary
@key
@id
Answer: D) @id
Explanation:
@id annotation is used to specify the primary key field of an entity.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 105
Daily Quiz - MCQ
11. ____ is a programming capability that allows data to be converted from
object type to relational type and vice versa.
A. ORM
B. JPA
C. JRE
D. Java RMI
Answer: A) ORM
Explanation:
ORM is a programming capability that allows data to be converted from object
type to relational type and vice versa.

12. Which of the following annotations defines a class as an entity?


A. @table
B. @entitymanager
C. @entityfactory
D. @entity
Answer: D) @entity
Explanation:
@entity annotation is used to define a class as an entity.
17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 106
Old Question Papers

https://www.vturesource.com/vtu/3704942925/Advanced-JAVA-and-
J2EE/15CS553/Feb-2021

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 107


Expected Questions
• What is Spring?
• What are the advantages of spring framework?
• What are the modules of spring framework?
• What is IOC and DI?
• What is the front controller class of Spring MVC?
• What does @Controller annotation?
• What does @RequestMapping annotation?
• Does spring MVC provide validation support?

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 108


Weekly Assignment-1

1) What does an additional configuration file contain in Spring MVC


application?
2) What is an InternalResourceViewResolver in Spring MVC?
3) How to declare a class as a controller class in Spring MVC?
4) How to map controller class and its methods with URL?
5) What is the purpose of @PathVariable annotation in Spring MVC?

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 109


Weekly Assignment-2

1) What is the role of @ResponseBody annotation in Spring MVC?


2) What is the role of the Model interface in Spring MVC?
3) What do you mean by ModelAndView in Spring MVC?
4) What is ModelMap in Spring MVC?
5) What are the ways of reading data from the form in Spring MVC?
6) What is Spring MVC form tag library?
7) What do you understand by validations in Spring MVC?
8) What is Bean Validation API?

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 110


Weekly Assignment-3

1) What is the use of @Valid annotation in Spring MVC?


2) What is the purpose of BindingResult in Spring MVC validations?
3) How to validate user's input within a number range in Spring MVC?
4) How to validate the user input in a particular sequence in Spring MVC?
5) What is the purpose of custom validations in Spring MVC?
6) What do you understand by Spring MVC Tiles?

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 111


Weekly Assignment-4

1) How to create Spring Boot project using boot CLI?


2) How to create simple Spring Boot application?
3) What are the Spring Boot Annotations?
4) What is Spring Boot dependency management?
5) What are the Spring Boot properties?

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 112


Weekly Assignment-5

1) What are the Spring Boot Starters?


2) What is Spring Boot Actuator?
3) What is thymeleaf?
4) How to use thymeleaf?
5) How to connect Spring Boot to the database using JPA?

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 113


Weekly Assignment-6

1) How to connect Spring Boot application to database using JDBC?


2) What is @RestController annotation in Spring Boot?
3) What is @RequestMapping annotation in Spring Boot?
4) How to create Spring Boot application using Spring Starter Project
Wizard?
5) ) Explain Spring Vs Spring Boot?

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 114


Daily Quiz

1) What is MVC?
2) What is Spring MVC?
3) What is the front controller of Spring MVC?
4) Explain the flow of Spring MVC?
5) What are the advantages of Spring MVC Framework?

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 115


Daily Quiz

1) What is Spring Boot?


2) What are the advantages of Spring Boot?
3) What are the features of Spring Boot?
4) How to create Spring Boot application using Maven?
5) How to create Spring Boot project using Spring Initializer?

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 116


Recap of Unit

• Threads allows a program to operate more efficiently by doing


multiple things at the same time.
• User threads are threads which are created by the application or user.
• Daemon threads are threads which are mostly created by the JVM.
• Java brings various Streams with its I/O package that helps the user to perform
all the input-output operations.
• Thread Lifecycle.
• Annotations are used to provide supplement information about a program.

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 117


Thank You

17/11/2024 Ms.Teena ACSE0601 Advanced Java Programming 118

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy