Advanced Java (Module 6)
Advanced Java (Module 6)
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
--------------------------------------------------------------------------------------------------------
Advantages
Micro services offers the following advantages to its developers −
Easy deployment
Simple scalability
Compatible with Containers
Minimum configuration
Lesser production time
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.
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
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
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
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.
4|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
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: 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:
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 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:
7|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
8|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
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/
<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
<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
After dependencies section need to add following code for configuring java version
<properties>
<java.version>1.8</java.version>
</properties>
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
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";
}
We can create springBoot Application using Spring Starter Project which automates
all above steps
12 | P a g e