0% found this document useful (0 votes)
13 views24 pages

Ydp Api&ms Intro

The document provides an overview of Spring Framework and Spring Boot, detailing their modules, dependency injection methods, and application context types. It covers the creation of web services and RESTful applications, as well as the use of Maven for project management. Additionally, it discusses Spring Data JPA, transaction management, and best practices for application development.

Uploaded by

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

Ydp Api&ms Intro

The document provides an overview of Spring Framework and Spring Boot, detailing their modules, dependency injection methods, and application context types. It covers the creation of web services and RESTful applications, as well as the use of Maven for project management. Additionally, it discusses Spring Data JPA, transaction management, and best practices for application development.

Uploaded by

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

API AND

MICROSERVICES

Y DURGA PRASAD
Associate Professor
Department of Computer Science and Engineering
ADITYA
UNIT I:
Spring 5 Basics : Why Spring, What is Spring Framework, Spring Framework - Modules, Configuring IoC container
using Java-based configuration, Introduction To Dependency Injection, Constructor Injection, Setter Injection,
What is AutoScanning
UNIT II:
Spring Boot: Creating a Spring Boot Application, Spring Boot Application Annotation, What is Autowiring , Scope
of a bean, Logger, Introduction to Spring AOP, Implementing AOP advices, Best Practices : Spring Boot Application

UNIT III:
Spring Data JPA with Boot: Limitations of JDBC API, Why Spring Data JPA, Spring Data JPA with Spring Boot,
Spring Data JPA Configuration, Pagination and Sorting, Query Approaches, Named Queries and Query, Why
Spring Transaction, Spring Declarative Transaction, Update Operation in Spring Data JPA, Custom Repository
Implementation, Best Practices - Spring Data JPA

UNIT IV:
Web Services: Why Web services, SOA - Service Oriented Architecture, What are Web Services, Types of Web
Services, SOAP based Web Services, RESTful Web Services, How to create RESTful Services

UNIT V:
Spring REST: Spring REST - An Introduction, Creating a Spring REST Controller, @RequestBody and
ResponseEntity, Parameter Injection, Usage of @PathVariable, @RequestParam and @MatrixVariable, Exception
Handling, Data Validation, Creating a REST Client, Versioning a Spring REST endpoint, Enabling CORS in Spring
REST, Securing Spring REST endpoints
2
ADITYA

Maven is a powerful build tool for Java software projects. Actually, you can build software projects
using other languages too, but Maven is developed in Java, and is thus historically used more for Java
projects.
A build tool is a tool that automates everything related to building the software project. Building a
software project typically includes one or more of these activities:
▪Generating source code (if auto-generated code is used in the project).
▪Generating documentation from the source code.
▪Compiling source code.
▪Packaging compiled code into JAR files or ZIP files.
Installing the packaged code on a server, in a repository or somewhere else.
Maven is centered around the concept of POM files (Project Object Model). A POM file is an XML
representation of project resources like source code, test code, dependencies (external JARs used)
etc. The POM contains references to all of these resources. The POM file should be located in the root
directory of the project it belongs to.
3
ADITYA

4
ADITYA
dependency>
<

<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.43.1</version>
</dependency>
The src directory has a number of subdirectories, each of which has a clearly defined purpose:
➢src/main/java: Your Java source code goes here (strangely enough!)
➢src/main/resources: Other resources your application needs
➢src/main/filters: Resource filters, in the form of properties files, which may be used to define
variables only known at runtime
➢src/main/config: Configuration files
➢src/main/webapp: The Web application directory for a WAR project
➢src/test/java: Unit tests
➢src/test/resources: Resources to be used for unit tests, but will not be deployed
➢src/test/filters: Resources filters to be used for unit tests, but will not be deployed
➢src/site: Files used to generate the Maven project Website 5
ADITYA

6
ADITYA
➢Spring Framework is an open source Java application development framework that supports developing all
types of Java applications such as enterprise applications, web applications, cloud based applications, and many
more.
➢Spring is a popular open-source Java application development framework created by Rod Johnson.
➢Java applications developed using Spring are simple, easily testable, reusable, and maintainable.
➢Spring modules do not have tight coupling on each other, the developer can pick and choose the modules as per
the need for building an enterprise application.

7
ADITYA

8
ADITYA
Spring Core:
The Spring Core module, which is the core component of the Spring framework, provides the IoC container There
are two types of implementations of the Spring container, namely, bean factory and application context.
❑Bean factory is defined using the org.springframework.beans.factory.BeanFactory interface and acts as a container
for beans. The Bean factory container allows you to decouple the configuration and specification of dependencies
from program logic. In the Spring framework, the Bean factory acts as a central IoC container that is responsible for
instantiating application objects. It also configures and assembles the dependencies between these objects. There are
numerous implementations of the BeanFactory interface. The XmlBeanFactory class is the most common
implementation of the BeanFactory interface. This allows you to express the object to compose your application
and remove interdependencies between application objects.
❑Spring Application Context Module:
The Spring Application context module is based on the Core module. Application context
org.springframework.context.ApplicationContext is an interface of BeanFactory. This module derives its feature from
the org.springframework.beans package and also supports functionalities such as internationalization (I18N),
validation, event propagation, and resource loading. The Application context implements MessageSource interface
and provides the messaging functionality to an application. 9
ADITYA
❑Core: This is the key module of Spring Framework
which provides fundamental support on which all
other modules of the framework are dependent.
❑Bean:This module provides a basic Spring container
called BeanFactory.
❑Context: This module provides one more Spring
container called ApplicationContext which inherits
the basic features of the BeanFactory container and
also provides additional features to support
enterprise application development.
❑Spring Expression Language (SpEL): This module is
used for querying/manipulating object values.
❑AOP (Aspect Oriented Programming) and
aspects: These modules help in isolating cross-cutting
functionality from business logic.
10
ADITYA

❖The following modules support Data Access/Integration:


❑Java Database Connectivity (JDBC): It provides an abstract layer to support JDBC calls to relational databases.
❑Object Relational Mapping (ORM): It provides integration support for popular ORM(Object-Relational Mapping)
solutions such as Hibernate, JPA, etc.
❑Transactions: It provides a simple transaction API which abstracts the complexity of underlying repository specific
transaction API's from the application.
Spring Framework provides the following modules to support web application development:
❑Web: This module has a container called web application context which inherits basic features from ApplicationContext
container and adds features to develop web based applications.
❑Webmvc: It provides the implementation of the MVC(model-view-controller) pattern to implement the serverside
presentation layer and also supports features to implement RESTful Web Services.
❑WebFlux: Spring 5.0 introduced a reactive stack with a web framework called Spring WebFlux to support Reactive
programming in Spring's web layer and runs on containers such as Netty, Undertow, and Servlet 3.1+.
❑WebSocket: It is used for 2 way communication between client and server in WebSocket based web applications.

Spring Framework has few additional modules, test module is one of the most commonly used ones for testing
Spring applications.
❑Test: This module provides the required support to test Spring applications using TestNG or JUnit. 11
ADITYA
❖Spring Container managed application objects are called The Spring container knows which objects to
beans in Spring. create and when to create through the
additional details that we provide in our
We need not create objects in dependency injection application called Configuration Metadata.
instead describe how objects should be created through
configuration.
❖DI is a software design pattern that provides better software
design to facilitate loose coupling, reuse, and ease of testing.
❖Benefits of Dependency Injection(DI):
➢Helps to create loosely coupled application architecture
facilitating re-usability and easy testing.
➢Separation of responsibility by keeping code and
configuration separately. Hence dependencies can be
easily modified using configuration without changing the
code. Spring allows providing the configuration
➢Allows to replace actual objects with mock objects for metadata using :
testing, this improves testability by writing simple JUnit ❑XML Configuration
tests that use mock objects. ❑Annotation Based configuration
❑Java Based configuration
12
ADITYA

Types of Spring Dependency Injection

There are two types of Spring Dependency Injection. They are:

Setter Dependency Injection (SDI): This is the simpler of the two DI methods. In this, the DI will be injected with

the help of setter and/or getter methods. Now to set the DI as SDI in the bean, it is done through the bean-

configuration file For this, the property to be set with the SDI is declared under the <property> tag in the bean-

config file.

Constructor Dependency Injection (CDI): In this, the DI will be injected with the help of constructors. Now to set

the DI as CDI in bean, it is done through the bean-configuration file For this, the property to be set with the CDI is

declared under the <constructor-arg> tag in the bean-config file.

13
ADITYA

Types of ApplicationContext
The most commonly used ApplicationContext implementations that use XML configuration sources are:

➢FileSystemXmlApplicationContext – loads the definitions of the beans from an XML file.


➢ClassPathXmlApplicationContext – loads the definitions of the beans from an XML file. Additionally, we can
set CLASSPATH properly because this container can look bean configuration XML file in CLASSPATH.
➢WebXmlApplicationContext – loads the XML file with definitions of all beans from within a web application.
In the case of Java configuration or annotation-based configuration classes, the following container types are
used:

➢AnnotationConfigApplicationContext – accepts component classes as input – in particular @Configuration-


annotated classes. It also plain @Component types and JSR-330 compliant classes using javax.inject
annotations.
➢AnnotationConfigWebApplicationContext – produces a WebApplicationContext accepting similar inputs as
above. It is the equivalent of AnnotationConfigApplicationContext for a web environment.

14
ADITYA

15
ADITYA

16
ADITYA

17
ADITYA
@RequestParam to extract query parameters, form parameters, and even files from the request.
It has the following optional elements:
❑defaultValue - used as a fallback when the request parameter is not provided or has an empty value
❑name - name of the request parameter to bind to
❑required - tells whether the parameter is required
❑value - alias for name
@RequestMapping("showdata") @RequestMapping("showdata")
public String userForm(@RequestParam("user_name") public String userForm(@RequestParam(required = false)
String name, Model model) String name, Model model)
{ {
model.addAttribute("name", name); model.addAttribute("name", name);
return "user-data"; return "user-data";
} }

@RequestMapping("showdata")
public String userForm(@RequestParam(defaultValue =
"No_value") String name, Model model)
{
model.addAttribute("name", name);
return "user-data";
} 18
ADITYA
❑In Spring MVC, the model works a
container that contains the data of the
application. Here, a data can be in any form
such as objects, strings, information from
the database, etc.
❑The Model object is automatically created
by the Spring container when a controller
method is invoked.
❑It is required to place the Model interface
in the controller part of the application. The
object of HttpServletRequest reads the
information provided by the user and pass
it to the Model interface. Now, a view page
easily accesses the data from the model
part.
❑We can access data from the Model
object in the view layer by using the
Expression Language (EL) syntax. For
example, ${name} would retrieve the value
of an attribute named “name” from the
Model object.
19
ADITYA

20
ADITYA

21
ADITYA

22
ADITYA

Spring provides a RowMapper interface for mapping a


single row of a ResultSet to an object. It can be used for
both single and multiple row queries. It is parameterized as
of Spring 3.0.
public interface RowMapper<T> {
T mapRow(ResultSet rs, int rowNum) throws
SQLException;
}
An interface used by JdbcTemplate for mapping rows of
a ResultSet on a per-row basis. Implementations of this
interface perform the actual work of mapping each row to
a result object, but don’t need to worry about exception
handling. SQLExceptions will be caught and handled by the
calling JdbcTemplate.

23
ADITYA

You might also like