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

Springboot Study material

The document provides an overview of the Spring Framework and Spring Boot, highlighting their features, advantages, and differences. It explains the concept of microservices, their benefits, and how they can be developed using Spring Boot. Additionally, it covers various Spring Boot starters, embedded web servers, and the ApplicationContext interface, along with examples of configuration and bean management.

Uploaded by

kk.da.29
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)
29 views

Springboot Study material

The document provides an overview of the Spring Framework and Spring Boot, highlighting their features, advantages, and differences. It explains the concept of microservices, their benefits, and how they can be developed using Spring Boot. Additionally, it covers various Spring Boot starters, embedded web servers, and the ApplicationContext interface, along with examples of configuration and bean management.

Uploaded by

kk.da.29
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/ 34

Spring Framework

• Lightweight application development framework used for Java


Enterprise Edition (JEE)
• Developed by Rod Johnson in 2003
• Easy development of JavaEE application.
• Dependency injection feature, code becomes loosely coupled
• Makes the code easy to test
• Provides predefined templates for JDBC, Hibernate, JPA etc., thus
reducing your effort of writing too much code.
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 Framework.
•Support for developing a micro service and enables you to
develop enterprise-ready applications
What is Spring Boot
•Combination of Spring Framework and Embedded Servers.
•No requirement for XML configuration (deployment descriptor).
•Can use Spring STS IDE or Spring Initializr to develop Spring
Boot Java application.
Why Spring Boot Framework?
•Dependency injection approach is used.
•Powerful database transaction management capabilities.
•Simplifies integration with other Java frameworks like
JPA/Hibernate ORM, Struts, etc.
•Reduces the cost and development time of the application.
•Offers annotation-based spring application
•Eases dependency management.
Advantages of Spring Boot
• Provides production-ready features
• No requirement for XML configuration
• Offers a CLI tool for developing and testing the Spring Boot application.
• Minimizes writing multiple boilerplate codes
• Increases productivity and reduces development time.
• Tests web applications easily with the help of different Embedded HTTP
servers such as Tomcat, Jetty, etc.
Spring Spring Boot
• Spring Framework is a widely used Java • Spring Boot Framework is widely used to
EE framework for building applications. develop REST APIs.

• It aims to simplify Java EE development • It aims to shorten the code length and provide
the easiest way to develop Web Applications.
that makes developers more productive.
• The primary feature of Spring Boot is
• The primary feature of the Spring Autoconfiguration. It automatically configures
Framework is dependency injection. the classes based on the requirement.
• The developer writes a lot of code • It reduces boilerplate code.
(boilerplate code) to do the minimal task. • Spring Boot offers embedded server such as
• To test the Spring project, we need to set Jetty and Tomcat, etc.
up the sever explicitly. • Spring Boot comes with the concept of starter
in pom.xml file that internally takes care of
• Developers manually define downloading the dependencies JARs based on
dependencies for the Spring project in Spring Boot Requirement.
pom.xml.
Microservices

• A microservice is an engineering approach focused on decomposing


applications into single-function modules with well-defined interfaces
which are independently deployed and operated by small teams who
own the entire lifecycle of the service.
(or)
• Microservices are a modern approach to software whereby application
code is delivered in small, manageable pieces, independent of others.
Why build microservices?
• Their small scale and relative isolation can lead to many additional
benefits...
• Easier maintenance
• Improved productivity
• Greater fault tolerance
• Better business alignment and more.
Microservices
Decomposing class UserApp {
User getUser() {
• Instead of having one large // 1. auth user
application, decompose it into // 2. get user data
separate, different, mini-applications // 3. log user actions
(services). }
• Each service handles a specific }
business domain (logging, auth, class UserApp {
orders, customers) and provides the void authUser(User user) { ... }
implementation for the user User getUserData() { ... }
interface, business logic, and void logUserActions() { ... }
connection to the database. }
Microservices
Single-function class UserApp {
• Each and every service has a specific void authUser(User user) {
function or responsibility.
// log user login action (success or
Well-defined interfaces failure)
• define a list of methods, their inputs and // using logUserActions
outputs
}
Independent
User getUserData() { ... }
• Services don’t know about each other
implementation. They can get tested, void logUserActions() { ... }
deployed, and maintained independently.
}
Microservices
Small Teams
• Split the work up and team across the services.
• Each team focuses on a specific service, they don’t need to know about the
internal workings of other teams.
Entire Lifecycle
• The team is responsible for the entire lifecycle of the service; from coding, testing,
staging, deploying, debugging, maintaining.
• In a traditional application, we may have a team for coding, and another one for
deployment. In microservices, that’s not the case.
Microservices
Minimizing Communication
• Only essential cross-team communication should be through the
interface that each service provides
Scope and risk of change
• Services should be changed without breaking other services.
12 Factor App
• Refer AJP Class notes...
SpringApplication - Entry Point Class
package com.javatpoint.springbootexample; • The SpringApplication is a class that provides a
convenient way to bootstrap a Spring application.
import org.springframework.boot.SpringApplication;
import • It can be started from the main method. We can
org.springframework.boot.autoconfigure.SpringBootApplic call the application just by calling a static run()
ation; method.
@SpringBootApplication • The entry point of the spring boot application is
public class SpringBootExampleApplication the class contains @SpringBootApplication
{ annotation and the main method.
public static void main(String[] args) • Spring Boot automatically scans all the
{ components included in the project by using
SpringApplication.run(SpringBootExampleApplication.clas @ComponentScan annotation.
s, args);
}
}
Contd...
@SpringBootApplication

• A single @SpringBootApplication annotation is used to enable the


following annotations:

• @EnableAutoConfiguration: It enables the Spring Boot auto-configuration


mechanism.
• @ComponentScan: It scans the package where the application is located.
• @Configuration: It allows us to register extra beans in the context or import
additional configuration classes.
Spring Boot Starters
• Spring Boot provides a number of starters that allow us to add jars in the
classpath.
• Built-in starters make development easier and rapid.
• Dependency descriptors.
• All starters follow a similar naming pattern:
• spring-boot-starter-*
• where * denotes a particular type of application.
• For example
• To use Spring and JPA for database access,
• Need to include the spring-boot-starter-data-jpa dependency in our pom.xml file of the
project.
Third-Party Starters
• The third-party starter starts with the name of the project.
• For example,
• Third-party project name is abc,
• The dependency name will be abc-spring-boot-starter.
Application starters
Spring Boot Starter Security
• Dependency is used for Spring Security.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Application starters
Spring Boot Starter web
• Dependency is used to write a Rest Endpoints.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Application starters
Spring Boot Starter Thyme Leaf
• Dependency is used to create a web application.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Application starters
Spring Boot Starter Test
• Dependency is used for writing Test cases.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
Application starters under the org.springframework.boot
group
Name Description
spring-boot-starter-thymeleaf It is used to build MVC web applications using Thymeleaf views.
spring-boot-starter-mail It is used to support Java Mail and Spring Framework's email sending.
spring-boot-starter-web It is used for building the web application, including RESTful
applications using Spring MVC. It uses Tomcat as the default
embedded container.
spring-boot-starter-jdbc It is used for JDBC with the Tomcat JDBC connection pool.
spring-boot-starter-security It is used for Spring Security.
spring-boot-starter-data-jpa It is used for Spring Data JPA with Hibernate.
spring-boot-starter-cache It is used for Spring Framework's caching support.
Production Starters
Spring-boot-starter-actuator
• Provides production-ready features to help you monitor and manage
your application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Technical Starters
Name Description
spring-boot-starter-undertow It is used for Undertow as the embedded servlet container.
An alternative to spring-boot-starter-tomcat.
spring-boot-starter-jetty It is used for Jetty as the embedded servlet container. An
alternative to spring-boot-starter-tomcat.
spring-boot-starter-logging It is used for logging using Logback. Default logging
starter.
spring-boot-starter-tomcat It is used for Tomcat as the embedded servlet container.
Default servlet container starter used by
spring-boot-starter-web.
spring-boot-starter-log4j2 It is used for Log4j2 for logging. An alternative to
spring-boot-starter-logging.
Spring Boot Starter Parent
• The spring-boot-starter-parent is a project starter.
• Provides default configurations for our applications.
• Used internally by all dependencies.
• All Spring Boot projects use spring-boot-starter-parent as a parent in
pom.xml file.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
Spring Boot Embedded Web Server
• Default embedded server is Tomcat.
• Supports another two embedded servers
• Jetty Server
• Has the capability of serving static and dynamic content
• Used when machine to machine communication is required.
• Undertow Server
• It is written in Java and manage and sponsored by JBoss.
• Advantages : Websocket Support, Flexible, Embeddable..
Remember:
• While using Jetty server in the application, make sure that the default Tomcat
server is excluded from the spring-boot-starter-web. It avoids the conflict
between servers.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Remember
• While using Undertow server in the application, make sure that the default Tomcat
server is excluded from the spring-boot-starter-web. It avoids the conflict between
servers.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
ApplicationContext
• It is the central interface within a Spring application for providing
configuration information to the application.
• Provides getBean() method to retrieve bean from the spring container.
• Represents Spring IoC container
• The container gets its instructions on what objects to instantiate,
configure, and assemble by reading configuration metadata.
• The configuration metadata is represented in XML, Java annotations,
or Java code.
ApplicationContext
It has several implementations.

• ClassPathXmlApplicationContext
• Takes configuration from an XML file on the classpath

• AnnotationConfigApplicationContext
• Reads configuration using annotations, especially @Configuration.
HelloWorld.java file MainApp.java
package com.javatool; package com.javatool;
public class HelloWorld { import org.springframework.context.ApplicationContext;
private String message; import
public void setMessage(String org.springframework.context.support.FileSystemXmlApplicationCo
message){ ntext;
this.message = message; public class MainApp {
} public static void main(String[] args) {
public void getMessage(){ ApplicationContext context = new
FileSystemXmlApplicationContext("Beans.xml");
System.out.println("Your
Message : " + message); HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
} obj.getMessage();
} }
}
Beans.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id = "helloWorld" class = "com.javatool.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>

You might also like