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

Advanced Java (Module 6)

Uploaded by

Sushma Sumant
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)
42 views

Advanced Java (Module 6)

Uploaded by

Sushma Sumant
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/ 12

C-4, Wagle Industrial Estate,

Near Mulund Check Naka,


Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

Advance Java
Module 6

1|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

---------------------------------------------------------------------------------------------
Getting Started with Spring Boot:
Spring Boot and Database, spring Boot Web Application Development, Spring Boot RESTful
WebServices

Self learning topics: Understanding Transaction Management in Spring

--------------------------------------------------------------------------------------------------------

What is Micro Service?


Micro Service is an architecture that allows the developers to develop and deploy services
independently. Each service running has its own process and this achieves the lightweight
model to support business applications.

Advantages
Micro services offers the following advantages to its developers −

 Easy deployment
 Simple scalability
 Compatible with Containers
 Minimum configuration
 Lesser production time

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.It is a
Spring module that provides the RAD (Rapid Application Development) feature to the Spring

2|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

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.

Why should we use Spring Boot Framework?

We should use Spring Boot Framework because:

 The dependency injection approach is used in Spring Boot.


 It contains powerful database transaction management capabilities.
 It simplifies integration with other Java frameworks like JPA/Hibernate ORM, Struts, etc.
 It reduces the cost and development time of the application.

Along with the Spring Boot Framework, many other Spring sister projects help to build
applications addressing modern business needs. There are the following Spring sister projects
are as follows:

 Spring Data: It simplifies data access from the relational and NoSQL databases.
 Spring Batch: It provides powerful batch processing.
 Spring Security: It is a security framework that provides robust security to applications.
 Spring Social: It supports integration with social networking like LinkedIn.
 Spring Integration: It is an implementation of Enterprise Integration Patterns. It
facilitates integration with other enterprise applications using lightweight messaging and
declarative adapters.

Advantages

Spring Boot offers the following advantages to its developers −

 It creates stand-alone Spring applications that can be started using Java -jar.
 It tests web applications easily with the help of different Embedded HTTP servers such
as Tomcat, Jetty, etc. We don't need to deploy WAR files.
 It provides opinionated 'starter' POMs to simplify our Maven configuration.
 It provides production-ready features such as metrics, health checks, and externalized
configuration.
 There is no requirement for XML configuration.
 It offers a CLI tool for developing and testing the Spring Boot application.
 It offers the number of plug-ins.

3|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

Goals

Spring Boot is designed with the following goals −

 To avoid complex XML configuration in Spring


 To develop a production ready Spring applications in an easier way
 To reduce the development time and run the application independently
 Offer an easier way of getting started with the application

Why Spring Boot?

You can choose Spring Boot because of the features and benefits it offers as given here −

 It provides a flexible way to configure Java Beans, XML configurations, and Database
Transactions.
 It provides a powerful batch processing and manages REST endpoints.
REST or RESTful API design (Representational State Transfer) is designed to take
advantage of existing protocols. While REST can be used over nearly any protocol, it
usually takes advantage of HTTP when used for Web APIs.
 In Spring Boot, everything is auto configured; no manual configurations are needed.
 It offers annotation-based spring application
 Eases dependency management
 It includes Embedded Servlet Container

Spring Boot Features

Web Development
It is a well-suited Spring module for web application development. We can easily create a self-
contained HTTP application that uses embedded servers like Tomcat, Jetty, or Undertow. We
can use the spring-boot-starter-web module to start and run the application quickly.

SpringApplication
The SpringApplication is a class that provides a convenient way to bootstrap a Spring
application. It can be started from the main method. We can call the application just by calling
a static run() method.

public static void main(String[] args)


{
SpringApplication.run(ClassName.class, args);
}

4|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

Application Events and Listeners


Spring Boot uses events to handle the variety of tasks. It allows us to create factories file that
is used to add listeners. We can refer it to using the ApplicationListener key.
Always create factories file in META-INF folder like META-INF/spring.factories.

Admin Support
Spring Boot provides the facility to enable admin-related features for the application. It is used
to access and manage applications remotely. We can enable it in the Spring Boot application
by using spring.application.admin.enabled property.

Externalized Configuration
Spring Boot allows us to externalize our configuration so that we can work with the same
application in different environments. The application uses YAML files to externalize
configuration.

Properties Files
Spring Boot provides a rich set of Application Properties. So, we can use that in the properties
file of our project. The properties file is used to set properties like server-port =8082 and many
others. It helps to organize application properties.

YAML Support
It provides a convenient way of specifying the hierarchical configuration. It is a superset of
JSON. The SpringApplication class automatically supports YAML. It is an alternative of
properties file.

Type-safe Configuration
The strong type-safe configuration is provided to govern and validate the configuration of the
application. Application configuration is always a crucial task which should be type-safe. We
can also use annotation provided by this library.

Logging
Spring Boot uses Common logging for all internal logging. Logging dependencies are managed
by default. We should not change logging dependencies if no customization is needed.

Security
Spring Boot applications are spring bases web applications. So, it is secure by default with basic
authentication on all HTTP endpoints. A rich set of Endpoints is available to develop a secure
Spring Boot application.

5|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

Spring vs. Spring Boot

Spring: Spring Framework is the most popular application development framework of Java.
The main feature of the Spring Framework is dependency Injection or Inversion of Control
(IoC). With the help of Spring Framework, we can develop a loosely coupled application. It is
better to use if application type or characteristics are purely defined.

Spring Boot: Spring Boot is a module of Spring Framework. It allows us to build a stand-alone
application with minimal or zero configurations. It is better to use if we want to develop a simple
Spring-based application or RESTful services.

The primary comparison between Spring and Spring Boot are discussed below:

Spring Spring Boot


Spring Framework is a widely used Java EE Spring Boot Framework is widely used to
framework for building applications. develop REST APIs.
It aims to simplify Java EE development that It aims to shorten the code length and
makes developers more productive. provide the easiest way to develop Web
Applications.
The primary feature of the Spring Framework The primary feature of Spring Boot is
is dependency injection. Autoconfiguration. It automatically configures
the classes based on the requirement.
It helps to make things simpler by allowing us It helps to create a stand-alone application
to develop loosely coupled applications. with less configuration.
The developer writes a lot of code (boilerplate It reduces boilerplate code.
code) to do the minimal task.
To test the Spring project, we need to set up Spring Boot offers embedded server such as
the sever explicitly. Jetty and Tomcat, etc.
It does not provide support for an in-memory It offers several plugins for working with an
database. embedded and in-memory database
Developers manually define dependencies for Spring Boot comes with the concept of starter
the Spring project in pom.xml. in pom.xml file that internally takes care of
downloading the dependencies JARs based on
Spring Boot Requirement.

6|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

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: The presentation


layer handles the HTTP requests, translates
the JSON parameter to object, and
authenticates the request and transfer it to
the business layer. In short, it consists of
views i.e., frontend part.
Business Layer: The business layer
handles all the business logic. It consists of
service classes and uses services provided
by data access layers. It also performs
authorization and validation.
Persistence Layer: The persistence layer
contains all the storage logic and translates
business objects from and to database
rows.

Database Layer: In the database layer, CRUD (create,


Databaseretrieve,
Layer:update,
In the delete) operations
database layer,
are performed. CRUD (create, retrieve, update, delete)
operations are performed.

7|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

Spring Boot Flow Architecture

 Now we have validator classes, view classes, and utility classes.


 Spring Boot uses all the modules of Spring-like Spring MVC, Spring Data, etc. The
architecture of Spring Boot is the same as the architecture of Spring MVC, except one
thing: there is no need for DAO and DAOImpl classes in Spring boot.
 Creates a data access layer and performs CRUD operation.
 The client makes the HTTP requests (PUT or GET).
 The request goes to the controller, and the controller maps that request and handles it.
After that, it calls the service logic if required.
 In the service layer, all the business logic performs. It performs the logic on the data
that is mapped to JPA with model classes.
 A JSP page is returned to the user if no error occurred.

8|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

Configure Eclipse For SpringBoot


Start eclipse --> go to help -->select eclipse marketplace -->search for spring tools-->
Install spring tools 4 --> after installation eclipse will restart

What is maven
Apache Maven is a software project management and comprehension tool. Based on the
concept of a project object model (POM), Maven can manage a project's build, reporting
and documentation from a central piece of information.
We can get maven plugin’s using https://mvnrepository.com/

Step by step creating Spring Boot Application (Maven Project Eclipse)

1. Steps to be configured inside pom.xml


1. Configure spring boot starter parent

The spring-boot-starter-parent is a project starter. It provides default configurations for


our applications. It is used internally by all dependencies. All Spring Boot projects use
spring-boot-starter-parent as a parent in pom.xml file. This must be added before
dependencies

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>

Parent Poms allow us to manage the following things for multiple child projects and
modules:
o Configuration: It allows us to maintain consistency of Java Version and other
related properties.
o Dependency Management: It controls the versions of dependencies to avoid
conflict.
o Source encoding
o Default Java Version
o Resource filtering
o It also controls the default plugin configuration.
The spring-boot-starter-parent inherits dependency management from spring-boot-
dependencies. We only need to specify the Spring Boot version number. If there is a
requirement of the additional starter, we can safely omit the version number.

9|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

2. Configure spring boot starter web

There are two important features of spring-boot-starter-web:

It is compatible for web development


Auto configuration
If we want to develop a web application, we need to add the following dependency in
pom.xml file:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>

Starter of Spring web uses Spring MVC, REST and Tomcat as a default embedded server.
The single spring-boot-starter-web dependency transitively pulls in all dependencies
related to web development. It also reduces the build dependency count. The spring-
boot-starter-web transitively depends on the following:
o org.springframework.boot:spring-boot-starter
o org.springframework.boot:spring-boot-starter-tomcat
o org.springframework.boot:spring-boot-starter-validation
o com.fasterxml.jackson.core:jackson-databind
o org.springframework:spring-web
o org.springframework:spring-webmvc
By default, the spring-boot-starter-web contains the following tomcat server
dependency:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.0.RELEASE</version>
<scope>compile</scope>
</dependency>

The spring-boot-starter-web auto-configures the following things that are required for
the web development:
o Dispatcher Servlet
o Error Page
o Web JARs for managing the static dependencies
o Embedded servlet container

10 | P a g e
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

3. Configure java version

After dependencies section need to add following code for configuring java version

<properties>
<java.version>1.8</java.version>
</properties>

4. Configure spring boot plugin

After dependencies section need to add following code for configuring spring boot plugin

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

2. Create MainApp
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class MinApp
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
ApplicationContext con= SpringApplication.run(MinApp.class, args);

11 | P a g e
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More

3. Create Another class for RequestMapping

package com.test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController
{
@RequestMapping("/Home")
public String welcome()
{
return "welcome to springBoot";
}

5. Execute an application as SpringBoot Application

We can create springBoot Application using Spring Starter Project which automates
all above steps

12 | P a g e

You might also like