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

29.spring Boot

Uploaded by

Abdu Swamadh U A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

29.spring Boot

Uploaded by

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

SPRING BOOT

Prerequisites

1. Eclipse IDE for Java EE


2. Java 8
Contents

1. What is Spring Boot


2. Comparison Between Spring and Spring Boot
3. Spring Boot Starters
4. Spring Boot Annotations
5. Package Structure of a Spring Boot Project
6. Spring Boot Actuator
7. Spring Boot Dev tools
What Is Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade


Spring based Applications with minimum effort that you can
"just run"
Most Spring Boot applications need minimal Spring configuration.
Features

Create stand-alone Spring applications

Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

Provide opinionated 'starter' dependencies to simplify your build configuration

Automatically configure Spring and 3rd party libraries whenever possible

Provide production-ready features such as metrics, health checks, and externalized configuration

Absolutely no code generation and no requirement for XML configuration


Spring and Spring Boot

What Is Spring?

Simply put, the Spring framework provides comprehensive infrastructure support


for developing Java applications. It's packed with some nice features like
Dependency Injection, and out of the box modules like:

Spring JDBC
Spring MVC
Spring Security
What Is Exactly Spring Boot?

Spring Boot is basically an extension of the Spring framework, which eliminates the boilerplate configurations
required for setting up a Spring application.
It takes an opinionated view of the Spring platform, which paves the way for a faster and more efficient
development ecosystem.

Features in Spring Boot:


Opinionated ‘starter' dependencies to simplify the build and application configuration
Embedded server to avoid complexity in application deployment
Metrics, Health check, and externalized configuration
Automatic config for Spring functionality – whenever possible
Maven Dependencies

Minimum dependencies required to create a web application using Spring

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>5.3.5</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>5.3.5</version>

</dependency>
Unlike Spring, Spring Boot requires only one dependency to get
a web application up and running:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<version>2.4.4</version>

</dependency>

All other dependencies are added automatically to the final


archive during build time!!
Spring Boot provides a number of starter dependencies for
different Spring modules. Some of the most commonly used
ones are:

spring-boot-starter-data-jpa
spring-boot-starter-security
spring-boot-starter-test
spring-boot-starter-web
spring-boot-starter-thymeleaf
MVC Configuration

We can do this using either the web.xml file or an Initializer class:

public class MyWebAppInitializer implements WebApplicationInitializer {

@Override

public void onStartup(ServletContext container) {

AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

context.setConfigLocation("com.example");

container.addListener(new ContextLoaderListener(context));

ServletRegistration.Dynamic dispatcher = container

.addServlet("dispatcher", new DispatcherServlet(context));

dispatcher.setLoadOnStartup(1);

dispatcher.addMapping("/");

}
We also need to add the @EnableWebMvc annotation to a @Configuration class, and
define a view-resolver to resolve the views returned from the controllers:

@EnableWebMvc

@Configuration

public class ClientWebConfig implements WebMvcConfigurer {

@Bean

public ViewResolver viewResolver() {

InternalResourceViewResolver bean

= new InternalResourceViewResolver();

bean.setViewClass(JstlView.class);

bean.setPrefix("/WEB-INF/view/");

bean.setSuffix(".jsp");

return bean;

}
Spring Boot only needs a couple of properties to make things
work once we add the web starter

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
There are bunch of things that spring require lot mannual time and
effort.
Example:
Configuring template engine
Spring security Configuration
Database connection
Application Bootstrap

The basic difference in bootstrapping an application in Spring and


Spring Boot lies with the servlet.
Spring uses either the web.xml or SpringServletContainerInitializer
as its bootstrap entry point.
Spring Boot uses only Servlet 3 features to bootstrap an
application.
How Spring Boot Bootstraps?

The entry point of a Spring Boot application is the class which is


annotated with @SpringBootApplication

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Spring Boot Starters

Spring Boot starters can help to reduce the number of manually


added dependencies just by adding one dependency. So instead of
manually specifying the dependencies just add one starter
Example

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring Boot Annotations

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
@SpringBootConfiguration
Package Structure of a Spring Boot Project

org.springframework.samples.petclinic.model
org.springframework.samples.petclinic.owner
org.springframework.samples.petclinic.system
org.springframework.samples.petclinic.vet
org.springframework.samples.petclinic.visit
Spring Boot Actuator

Actuator brings production-ready features to our application.


Monitoring our app, gathering metrics, understanding traffic, or
the state of our database become trivial with this dependency.
The main benefit of this library is that we can get production-grade
tools without having to actually implement these features
ourselves.
Actuator is mainly used to expose operational information about
the running application — health, metrics, info, dump, env, etc.
It uses HTTP endpoints or JMX beans to enable us to interact
http://localhost:8080/actuator/
Configure A Spring Boot Web Application
Loading Initial Data with Spring Boot
spring.datasource.url=jdbc:mysql://localhost:3306/spring-security
spring.datasource.username=root
spring.datasource.password =root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

You might also like