Springboot Study material
Springboot Study material
• 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
<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>