0% found this document useful (0 votes)
969 views166 pages

01 Spring Boot Overview

This document provides an introduction and overview of a course on building Java applications with Spring Boot and Hibernate. The main objective is to learn how to develop Spring Boot applications, leverage Hibernate/JPA for database access, develop REST and MVC functionality, and connect Spring Boot apps to databases. The course will cover all Java configuration with no XML and use Maven. Source code and slides will be provided. Questions can be posted online. A basic Java development environment with JDK and IDE is required.

Uploaded by

Chotu
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)
969 views166 pages

01 Spring Boot Overview

This document provides an introduction and overview of a course on building Java applications with Spring Boot and Hibernate. The main objective is to learn how to develop Spring Boot applications, leverage Hibernate/JPA for database access, develop REST and MVC functionality, and connect Spring Boot apps to databases. The course will cover all Java configuration with no XML and use Maven. Source code and slides will be provided. Questions can be posted online. A basic Java development environment with JDK and IDE is required.

Uploaded by

Chotu
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

Course Introduction

Date

© luv2code LLC
Main Objective

Learn how to build Java applications with

Spring Boot and Hibernate

[Link] © luv2code LLC


You will learn how to …
• Develop Spring Boot applications

• Leverage Hibernate/JPA for database access

• Develop a REST API using Spring Boot

• Create a Spring MVC app with Spring Boot

• Connect Spring Boot apps to a Database for CRUD development

• Apply Spring Security to control application access

• Leverage all Java configuration (no xml) and Maven

[Link] © luv2code LLC


Source Code and PDFs

All source code is available for download

All PDFs of slides are available for download

[Link] © luv2code LLC


Questions / Help

If you have questions or need help…

Post the question in the classroom discussion forum

[Link] © luv2code LLC


Java Development Environment

Date

© luv2code LLC
Java Development Environment
• We assume that you are already have experience with Java

• OOP, classes, interfaces, inheritance, exception handling, collections

• You should have the following items already installed

• Java Development Kit (JDK) - Spring Boot 3 requires JDK 17 or higher

• Java IDE (we'll use IntelliJ in the videos, but any Java IDE will work)

[Link] © luv2code LLC


About IntelliJ Super Amazing IDE!!!
Super Amazing IDE!!!
Super Amazing IDE!!!

• In this course, we will use the free version of IntelliJ

• Known as IntelliJ Community Edition

• Download from: [Link]

• Select Community Edition

• You can also use the Ultimate Edition ($) ... free trial version is available

[Link] © luv2code LLC


Java Development Environment Checkpoint
• You should have the following items already installed

• Java Development Kit (JDK) - Spring Boot 3 requires JDK 17 or higher

• Java IDE (we'll use IntelliJ in the videos, but any Java IDE will work)

• Before continuing with the course

• Make sure you can run a basic HelloWorld Java app in your Java IDE

[Link] © luv2code LLC


Spring Boot Introduction

© luv2code LLC
Spring in a Nutshell

• Very popular framework for building Java applications

• Provides a large number of helper classes and annotations

[Link] © luv2code LLC


The Problem
• Building a traditional Spring application is really HARD!!!

Q: Which JAR dependencies do I need? And that's


JUST the basics
for getting started
Q: How do I set up configuration (xml or Java)?

Q: How do I install the server? (Tomcat, JBoss etc…)

[Link] © luv2code LLC


Spring Boot Solution
• Make it easier to get started with Spring development

• Minimize the amount of manual configuration

• Perform auto-configuration based on props files and JAR classpath

• Help to resolve dependency conflicts (Maven or Gradle)

• Provide an embedded HTTP server so you can get started quickly

• Tomcat, Jetty, Undertow, …

[Link] © luv2code LLC


Spring Boot and Spring

• Spring Boot uses Spring behind the scenes

• Spring Boot simply makes it easier to use Spring

[Link] © luv2code LLC


Spring Initializr

• Quickly create a starter Spring Boot project [Link]

• Select your dependencies

• Creates a Maven/Gradle project

• Import the project into your IDE

• Eclipse, IntelliJ, NetBeans etc …

[Link] © luv2code LLC


Spring Boot Embedded Server

• Provide an embedded HTTP server so you can get started quickly

• Tomcat, Jetty, Undertow, …

• No need to install a server separately


Self-contained unit
[Link] Nothing else to install

mycode JAR file


includes your application code
AND
Tomcat includes the server

[Link] © luv2code LLC


Running Spring Boot Apps
• Spring Boot apps can be run standalone (includes embedded server)

• Run the Spring Boot app from the IDE or command-line

[Link]

mycode > java -jar [Link]

Tomcat
Name of our JAR file

[Link] © luv2code LLC


Deploying Spring Boot Apps
• Spring Boot apps can also be deployed in the traditional way

• Deploy Web Application Archive (WAR) file to an external server:

• Tomcat, JBoss, WebSphere etc …

Tomcat

[Link] [Link] [Link]

mycode

[Link] © luv2code LLC


Spring Boot FAQ #1

Q: Does Spring Boot replace Spring MVC, Spring REST etc …?

• No. Instead, Spring Boot actually uses those technologies

Spring Boot

Spring MVC Spring REST Spring …

Spring Core Spring AOP Spring …

[Link] © luv2code LLC


Spring Boot FAQ #2

Q: Does Spring Boot run code faster than regular Spring code?

• No.

• Behind the scenes, Spring Boot uses same code of Spring Framework

• Remember, Spring Boot is about making it easier to get started

• Minimizing configuration etc …

[Link] © luv2code LLC


Spring Boot FAQ #3

Q: Do I need a special IDE for Spring Boot?

• No.

• You can use any IDE for Spring Boot apps … even use plain text editor

• The Spring team provides free Spring Tool Suite (STS) [IDE plugins]

• Some IDEs provide fancy Spring tooling support

• Not a requirement. Feel free to use the IDE that works best for you

[Link] © luv2code LLC


Spring Boot Initializr Demo

© luv2code LLC
Spring Initializr
[Link]
• Quickly create a starter Spring project

• Select your dependencies

• Creates a Maven/Gradle project

• Import the project into your IDE

• Eclipse, IntelliJ, NetBeans etc …

[Link] © luv2code LLC


Quick Word on Maven

• When building your Java project, you may need additional JAR files

• For example: Spring, Hibernate, Commons Logging, JSON etc…

• One approach is to download the JAR files from each project web site

• Manually add the JAR files to your build path / classpath

[Link] © luv2code LLC


Maven Solution
• Tell Maven the projects you are working with (dependencies)

• Spring, Hibernate etc ….

• Maven will go out and download the JAR files for those projects for you

• And Maven will make those JAR files available during compile/run

• Think of Maven as your friendly helper / personal shopper :-)

[Link] © luv2code LLC


Development Process Step-
By-S
tep

1. Configure our project at Spring Initializr website


[Link]

2. Download the zip file

3. Unzip the file

4. Import the project into our IDE

[Link] © luv2code LLC


Spring Boot - Create REST Controller

© luv2code LLC
REST Controller
• Let's create a very simple REST Controller

[Link] © luv2code LLC


Create REST Controller
Set up rest controller

@RestController
public class FunRestController {

// expose "/" that returns "Hello World"

@GetMapping("/") Handle HTTP GET requests


public String sayHello() {
return "Hello World!";
}

[Link] © luv2code LLC


Spring Framework
Overview

© luv2code LLC
Spring Website - Official

[Link]

[Link] © luv2code LLC


Why Spring?

Simplify Java Enterprise Development

[Link] © luv2code LLC


Goals of Spring

• Lightweight development with Java POJOs (Plain-Old-Java-Objects)

• Dependency injection to promote loose coupling

• Declarative programming with Aspect-Oriented-Programming (AOP)

• Minimize boilerplate Java code

[Link] © luv2code LLC


Core Container

Beans

Core

SpEL

Context

[Link] © luv2code LLC


Web Layer

Servlet

WebSocket

Web

[Link] © luv2code LLC


Data Access Layer

JDBC

ORM

Transactions

OXM JMS

[Link] © luv2code LLC


Infrastructure

AOP

Aspects

Instrumentation

Messaging

[Link] © luv2code LLC


Test Layer

Unit

Integration

Mock

[Link] © luv2code LLC


Spring Framework
Overview

© luv2code LLC
Spring Projects

© luv2code LLC
What Are Spring “Projects”

• Additional Spring modules built-on top of the core Spring Framework

• Only use what you need …

• Spring Cloud, Spring Data

• Spring Batch, Spring Security

• Spring Web Services, Spring LDAP

• others ...

[Link] © luv2code LLC


Spring Projects

© luv2code LLC
Maven Crash Course

© luv2code LLC
Spring Boot and Maven
• When you generate projects using Spring Initializr: [Link]

• It can generate a Maven project for you

• In this section, we will learn the basics of Maven

• Viewing dependencies in the Maven [Link] file

• Spring Boot Starters for Maven

[Link] © luv2code LLC


What is Maven?

• Maven is a Project Management tool

• Most popular use of Maven is for build management and dependencies

[Link] © luv2code LLC


What Problems Does Maven Solve?

• When building your Java project, you may need additional JAR files

• For example: Spring, Hibernate, Commons Logging, JSON etc…

• One approach is to download the JAR files from each project web site

• Manually add the JAR files to your build path / classpath

[Link] © luv2code LLC


My Project without Maven

My Super Cool App Spring JAR files

Hibernate JAR files


Spring
Hibernate developer
Commons Logging
Apache Commons
JSON
JAR files

JSON JAR files

[Link] © luv2code LLC


Maven Solution
• Tell Maven the projects you are working with (dependencies)

• Spring, Hibernate etc ….

• Maven will go out and download the JAR files for those projects for you

• And Maven will make those JAR files available during compile/run

• Think of Maven as your friendly helper / personal shopper :-)

[Link] © luv2code LLC


My Project with Maven Maven Central Repository (remote)

My Super Cool App Spring JAR files

Hibernate JAR files


Spring
Hibernate Maven
Commons Logging
Apache Commons
JSON
JAR files

developer
JSON JAR files

[Link] © luv2code LLC


Maven - How It Works Maven Local Repository

Check local repo (your computer)

Project Config file 2


Spring Read config file
Hibernate 4 Save in local repo

Commons Logging 1
JSON Maven Maven Central Repository (remote)

5
developer
Build and Run
3
Get from remote repo

[Link] © luv2code LLC


Handling JAR Dependencies

• When Maven retrieves a project dependency

• It will also download supporting dependencies

• For example: Spring depends on commons-logging …

• Maven will handle this for us automagically

[Link] © luv2code LLC


Building and Running

• When you build and run your app …

• Maven will handle class / build path for you

• Based on config file, Maven will add JAR files accordingly

[Link] © luv2code LLC


Standard Directory Structure

• Normally when you join a new project

• Each development team dreams up their own directory structure

• Not ideal for new comers and not standardized

• Maven solves this problem by providing a standard directory structure

[Link] © luv2code LLC


Standard Directory Structure

Directory Description

src/main/java Your Java source code

src/main/resources Properties / config files used by your app

JSP files and web config files


src/main/webapp
other web assets (images, css, js, etc)

src/test Unit testing code and properties

Destination directory for compiled code.


target
Automatically created by Maven

[Link] © luv2code LLC


Standard Directory Structure

Place your Java source code here

[Link] © luv2code LLC


Standard Directory Structure

Place your Web assets here

[Link] © luv2code LLC


Standard Directory Structure Benefits

• For new developers joining a project

• They can easily find code, properties files, unit tests, web files etc …

[Link] © luv2code LLC


Standard Directory Structure Benefits
• Most major IDEs have built-in support for Maven

• Eclipse, IntelliJ, NetBeans etc

• IDEs can easily read/import Maven projects

• Maven projects are portable

• Developers can easily share projects between IDEs

• No need to fight about which IDE is the best LOL!

[Link] © luv2code LLC


Advantages of Maven
• Dependency Management

• Maven will find JAR files for you

• No more missing JARs

• Building and Running your Project

• No more build path / classpath issues

• Standard directory structure

[Link] © luv2code LLC


My Personal Best Maven Benefit(s)

• Once you learn Maven, you can join a new project and be productive

• You can build and run a project with minimal local configuration

[Link] © luv2code LLC


Maven Key Concepts

© luv2code LLC
Maven Key Concepts
• POM File - [Link]

• Project Coordinates

[Link]
POM File - [Link]

• Project Object Model file: POM file

• Configuration file for your project

• Basically your “shopping list” for Maven :-)

• Located in the root of your Maven project

[Link]
POM File Structure
Project name, version etc
[Link] Output file type: JAR, WAR, …

List of projects we depend on


Spring, Hibernate, etc…

Additional custom tasks to run:


generate JUnit test reports etc…

[Link]
Simple POM File
<project ...>

<modelVersion>4.0.0</modelVersion>
Project name, version etc
<groupId>com.luv2code</groupId>
Output file type: JAR, WAR, …
<artifactId>mycoolapp</artifactId>
<version>[Link]</version>
<packaging>jar</packaging>

<name>mycoolapp</name>

<dependencies>
<dependency> List of projects we depend on
<groupId>[Link]</groupId>
<artifactId>junit-jupiter</artifactId> Spring, Hibernate, etc…
<version>5.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Additional custom tasks to run:
<!-- add plugins for customization -->
generate JUnit test reports etc…
</project>

[Link]
Project Coordinates
• Project Coordinates uniquely identify a project

• Similar to GPS coordinates for your house: latitude / longitude

• Precise information for finding your house (city, street, house #)

City
<groupId>com.luv2code</groupId>
<artifactId>mycoolapp</artifactId> Street
<version>[Link]</version>
House Number

[Link]
Project Coordinates - Elements
Name Description

Name of company, group, or organization.


Group ID
Convention is to use reverse domain name: com.luv2code

Artifact ID Name for this project: mycoolapp

A specific release version like: 1.0, 1.6, 2.0 …


Version
If project is under active development then: 1.0-SNAPSHOT

<groupId>com.luv2code</groupId>
<artifactId>mycoolapp</artifactId>
<version>[Link]</version>

[Link]
Example of Project Coordinates
<groupId>com.luv2code</groupId>
<artifactId>mycoolapp</artifactId>
<version>[Link]</version>

<groupId>[Link]</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0</version>

<groupId>[Link]</groupId>
<artifactId>hibernate-core</artifactId>
<version>[Link]</version>

[Link]
Adding Dependencies
<project ...>
...
<dependencies>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>hibernate-core</artifactId>
<version>[Link]</version>
</dependency>
...

</dependencies>

</project>

[Link]
Dependency Coordinates
• To add given dependency project, we need

• Group ID, Artifact ID


DevOps
• Version is optional …

• Best practice is to include the version (repeatable builds)

• May see this referred to as: GAV

• Group ID, Artifact ID and Version

[Link]
How to Find Dependency Coordinates

• Option 1: Visit the project page ([Link], [Link] etc)

• Option 2: Visit [Link] (easiest approach)

[Link]
Spring Boot Project Structure

© luv2code LLC
Spring Initializr
• Spring Initializr created a Maven project for us
[Link]

• Let's explore the project structure

[Link] © luv2code LLC


Maven Standard Directory Structure

Directory Description

src/main/java Your Java source code

src/main/resources Properties / config files used by your app

src/test/java Unit testing source code

[Link] © luv2code LLC


Maven Wrapper files
• mvnw allows you to run a Maven project

• No need to have Maven installed or present on your path

• If correct version of Maven is NOT found on your computer

• Automatically downloads correct version


and runs Maven > mvnw clean compile test
• Two files are provided

• [Link] for MS Windows

• [Link] for Linux/Mac $ ./mvnw clean compile test

[Link] © luv2code LLC


Maven Wrapper files
• If you already have Maven installed previously

• Then you can ignore/delete the mvnw files

• Just use Maven as you normally would

$ mvn clean compile test

[Link] © luv2code LLC


Maven POM file Spring Boot Starters

A collection of Maven dependencies


• [Link] includes info that you entered at Spring Initializr website
(Compatible versions)

<groupId>[Link]</groupId>
<artifactId>mycoolapp</artifactId>
<dependencies>
<version>0.0.1-SNAPSHOT</version>
<dependency>
<packaging>jar</packaging>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
Save's the developer from having to list all of the
<groupId>[Link]</groupId>
spring-web
individual dependencies
<artifactId>spring-boot-starter-test</artifactId>
spring-webmvc
<scope>test</scope> hibernate-validator
</dependency>
Also, makes sure you have compatible versions tomcat

</dependencies> json

[Link] © luv2code LLC


Maven POM file To package executable jar
or war archive
• Spring Boot Maven plugin Can also easily run the app

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
$ ./mvnw package
Can also just use:

mvn package $ ./mvnw spring-boot:run


mvn spring-boot:run

[Link] © luv2code LLC


Java Source Code

Main Spring Boot application class

Created by Spring Initializr

RestController that we created


in an earlier video

[Link] © luv2code LLC


Application Properties
• By default, Spring Boot will load properties from: [Link]

Created by Spring Initializr

Empty at the beginning

Can add Spring Boot properties


[Link]=8585

Also add your own custom properties


[Link]=Mickey Mouse

[Link] © luv2code LLC


Application Properties
• Read data from: [Link]

# configure server port


[Link]=8484 @RestController
public class FunRestController {
# configure my props
[Link]=Mickey Mouse @Value("${[Link]}")
[Link]=The Mouse Crew private String coachName;

@Value("${[Link]}")
private String teamName;


}

[Link] © luv2code LLC


Static Content

By default, Spring Boot will load static


resources from "/static" directory

Examples of static resources


HTML files, CSS, JavaScript, images, etc …

[Link] © luv2code LLC


Static Content

WARNING:
Do not use the src/main/webapp directory if your application is packaged as a JAR.

Although this is a standard Maven directory, it works only with WAR packaging.

It is silently ignored by most build tools if you generate a JAR.

[Link] © luv2code LLC


Templates
• Spring Boot includes auto-configuration for following template engines

• FreeMarker
By default, Spring Boot will load
templates from "/templates" directory
• Thymeleaf

• Mustache

Thymeleaf is a popular template engine


We will use it later in the course

[Link] © luv2code LLC


Unit Tests

Spring Boot unit test class

Created by Spring Initializr

You can add unit tests to the file

[Link] © luv2code LLC


Spring Boot Starters

© luv2code LLC
The Problem
• Building a Spring application is really HARD!!!

FAQ: Which Maven dependencies do I need?

[Link] © luv2code LLC


The Problem

Spring
version

Which versions
are compatible?
Very error-prone

Easy to make
a simple mistake

[Link] © luv2code LLC


Why Is It So Hard?
• It would be great if there was a simple list of Maven dependencies

• Collected as a group of dependencies … one-stop shop

• So I don't have to search for each dependency

There should be
an easier solution

[Link] © luv2code LLC


The Solution - Spring Boot Starters
• Spring Boot Starters

• A curated list of Maven dependencies

• A collection of dependencies grouped together

• Tested and verified by the Spring Development team

• Makes it much easier for the developer to get started with Spring

• Reduces the amount of Maven configuration

[Link] © luv2code LLC


Spring MVC
• For example, when building a Spring MVC app, you normally need

[Link] © luv2code LLC


Solution: Spring Boot Starter - Web
• Spring Boot provides: spring-boot-starter-web
Spring Boot Starters

A collection of Maven
dependencies
(Compatible versions)

CONTAINS
Save's the developer from having to list all of the spring-web
individual dependencies spring-webmvc
hibernate-validator
Also, makes sure you have compatible versions json
tomcat

[Link] © luv2code LLC


Spring Initializr
• In Spring Initializr, simply select Web dependency

• You automatically get spring-boot-starter-web in [Link]

[Link] © luv2code LLC


Spring Initializr
• If we are building a Spring app that needs: Web, Security, …

• Simply select the dependencies in the Spring Initializr

• It will add the appropriate Spring Boot starters to your [Link]

[Link]

[Link] © luv2code LLC


Spring Initializr
File: [Link]

[Link] © luv2code LLC


Spring Boot Starters
• There are 30+ Spring Boot Starters from the Spring Development team

Name Description
Building web apps, includes validation, REST.
spring-boot-starter-web
Uses Tomcat as default embedded server

spring-boot-starter-security Adding Spring Security support

spring-boot-starter-data-jpa Spring database support with JPA and Hibernate

[Link] © luv2code LLC


Spring Boot Starters

Full list

[Link]/spring-boot-starters

[Link] © luv2code LLC


What Is In the Starter?
• FAQ: What is in spring-boot-starter-xyz?

• View the starter's POM file [Link]/spring-boot-starters

• Normally references other starters … so you will need to dig a bit

• Somewhat cumbersome …

[Link] © luv2code LLC


What Is In the Starter?
• Most IDEs have a Dependency Management / View feature

• Much easier to navigate

• I'll show you how to do this with Eclipse and IntelliJ

[Link] © luv2code LLC


What Is In the Starter?
• For Eclipse Users

• Open the [Link]

• Select the tab Dependency Hierarchy

• Expand the desired starter

[Link] © luv2code LLC


What Is In the Starter?
• For IntelliJ users

• Select View > Tool Windows > Maven Projects > Dependencies

[Link] © luv2code LLC


Spring Boot Starter Parent

© luv2code LLC
Spring Boot Starter Parent
• Spring Boot provides a "Starter Parent"

• This is a special starter that provides Maven defaults

Included in [Link]
when using
Spring Initializr

[Link] © luv2code LLC


Spring Boot Starter Parent
• Maven defaults defined in the Starter Parent

• Default compiler level

• UTF-8 source encoding

• Others …

[Link] © luv2code LLC


Spring Boot Starter Parent
• To override a default, set as a property
Specify your
Java version

[Link] © luv2code LLC


Spring Boot Starter Parent
• For the spring-boot-starter-* dependencies, no need to list version

Specify version of
Spring Boot

Inherit version from


Starter Parent

No need to list individual versions


Great for maintenance!

[Link] © luv2code LLC


Spring Boot Starter Parent
• Default configuration of Spring Boot plugin

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> > mvn spring-boot:run

[Link] © luv2code LLC


Benefits of the Spring Boot Starter Parent
• Default Maven configuration: Java version, UTF-encoding etc

• Dependency management

• Use version on parent only

• spring-boot-starter-* dependencies inherit version from parent

• Default configuration of Spring Boot plugin

[Link] © luv2code LLC


Spring Boot Dev Tools

© luv2code LLC
The Problem
• When running Spring Boot applications

• If you make changes to your source code

• Then you have to manually restart your application :-(

[Link] © luv2code LLC


Solution: Spring Boot Dev Tools
• spring-boot-devtools to the rescue!

• Automatically restarts your application when code is updated

• Simply add the dependency to your POM file

• No need to write additional code :-)

• For IntelliJ, need to set additional configurations ... will discuss shortly

[Link] © luv2code LLC


Spring Boot Dev Tools
• Adding the dependency to your POM file

Automatically restarts your


application when code is updated

[Link] © luv2code LLC


IntelliJ Community Edition - DevTools
• IntelliJ Community Edition does not support DevTools by default

• Select: Preferences > Build, Execution, Deployment > Compiler

• Check box: Build project automatically

[Link] © luv2code LLC


IntelliJ Community Edition
• Additional setting

• Select: Preferences > Advanced Settings

• Check box: Allow auto-make to ...

[Link] © luv2code LLC


Development Process Step-
By-S
tep

1. Apply IntelliJ configurations

2. Edit [Link] and add spring-boot-devtools

3. Add new REST endpoint to our app

4. Verify the app is automatically reloaded

[Link] © luv2code LLC


Spring Boot DevTools Documentation

For more details


[Link]/devtools-docs

[Link] © luv2code LLC


Spring Boot Actuator

© luv2code LLC
Problem
• How can I monitor and manage my application?

• How can I check the application health?

• How can I access application metrics?

[Link] © luv2code LLC


Solution: Spring Boot Actuator
• Exposes endpoints to monitor and manage your application

• You easily get DevOps functionality out-of-the-box

• Simply add the dependency to your POM file

• REST endpoints are automatically added to your application

No need to write additional code!

You get new REST endpoints for FREE!

[Link] © luv2code LLC


Spring Boot Actuator
• Adding the dependency to your POM file

[Link] © luv2code LLC


Spring Boot Actuator
• Automatically exposes endpoints for metrics out-of-the-box

• Endpoints are prefixed with: /actuator

Name Description

/health Health information about your application

[Link] © luv2code LLC


Health Endpoint
• /health checks the status of your application

• Normally used by monitoring apps to see if your app is up or down

Health status is customizable


based on
your own business logic

[Link] © luv2code LLC


Exposing Endpoints
• By default, only /health is exposed

• The /info endpoint can provide information about your application

• To expose /info
File: src/main/resources/[Link]

[Link]=health,info
[Link]=true

[Link] © luv2code LLC


Info Endpoint
• /info gives information about your application

• Default is empty

[Link] © luv2code LLC


Info Endpoint
• Update [Link] with your app info
File: src/main/resources/[Link]

[Link]=My Super Cool App


[Link]=A crazy and fun app, yoohoo!
[Link]=1.0.0

[Link] © luv2code LLC


Spring Boot Actuator Endpoints
• There are 10+ Spring Boot Actuator endpoints

Name Description
/auditevents Audit events for your application

List of all beans registered in the


/beans
Spring application context
/mappings List of all @RequestMapping paths

[Link] © luv2code LLC


Spring Boot Actuator Endpoints

Full list
[Link]/actuator-endpoints

[Link] © luv2code LLC


Exposing Endpoints
• By default, only /health is exposed

• To expose all actuator endpoints over HTTP


File: src/main/resources/[Link]

# Use wildcard "*" to expose all endpoints


# Can also expose individual endpoints with a comma-delimited list
#
[Link]=*

[Link] © luv2code LLC


Get A List of Beans
• Access [Link]

What about security??

We'll add security


in later videos

[Link] © luv2code LLC


Development Process Step-
By-S
tep

1. Edit [Link] and add spring-boot-starter-acuator

2. View actuator endpoints for: /health

3. Edit [Link] to customize /info

[Link] © luv2code LLC


Spring Boot Actuator - Security

© luv2code LLC
What about Security?
• You may NOT want to expose all of this information

• Add Spring Security to project and endpoints are secured :-)

/health
is still available
You can disable it
if you want

[Link] © luv2code LLC


Secured Endpoints
• Now when you access: /actuator/beans

• Spring Security will prompt for login


Check console logs
for password

Default user name: user

[Link] © luv2code LLC


Spring Security configuration
• You can override default user name and generated password

File: src/main/resources/[Link]

[Link]=scott
[Link]=tiger

[Link] © luv2code LLC


Customizing Spring Security
• You can customize Spring Security for Spring Boot Actuator

• Use a database for roles, encrypted passwords etc …

• We will cover details of Spring Security later in the course

[Link] © luv2code LLC


Excluding Endpoints
• To exclude /health

File: src/main/resources/[Link]

# Exclude individual endpoints with a comma-delimited list


#
[Link]=health

[Link] © luv2code LLC


Spring Boot Actuator Documentation

For more details


[Link]/actuator-docs

[Link] © luv2code LLC


Development Process Step-
By-S
tep

1. Edit [Link] and add spring-boot-starter-security

2. Verify security on actuator endpoints for: /beans etc

3. Disable endpoints for /health and /info

[Link] © luv2code LLC


Spring Boot: Run from Command-Line

© luv2code LLC
Running from the Command-Line
• During development we spend most of our time in the IDE

• However, we may want to run our Spring Boot app outside of the IDE

• One approach is running from the command-line

[Link] © luv2code LLC


Running from the Command-Line
• When running from the command-line

• No need to have IDE open/running

• Since we using Spring Boot, the server is embedded in our JAR file

• No need to have separate server installed/running

• Spring Boot apps are self-contained

[Link] © luv2code LLC


Running from the Command-Line
• Spring Boot apps are self-contained

[Link]
Self-contained unit
mycode Nothing else to install

Tomcat JAR file


includes your application code
AND
includes the server

[Link] © luv2code LLC


Running from the Command-Line
• Two options for running the app

• Option 1: Use java -jar

• Option 2: Use Spring Boot Maven plugin

• mvnw spring-boot:run

[Link] © luv2code LLC


Option 1: Use java -jar

[Link]

mycode
> java -jar [Link]
Tomcat

Name of our JAR file

[Link] © luv2code LLC


Option 2: Use Spring Boot Maven plugin
• mvnw allows you to run a Maven project

• No need to have Maven installed or present on your path

• If correct version of Maven is NOT found on your computer

• Automatically downloads correct version


and runs Maven > mvnw clean compile test
• Two files are provided

• [Link] for MS Windows

• [Link] for Linux/Mac $ ./mvnw clean compile test

[Link] © luv2code LLC


Maven Wrapper files
• If you already have Maven installed previously

• Then you can ignore/delete the mvnw files

• Just use Maven as you normally would

$ mvn clean compile test

[Link] © luv2code LLC


Option 2: Use Spring Boot Maven plugin
To package executable jar
or war archive
<build>
<plugins> Can also easily run the app
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> $ ./mvnw package

Can also just use:


$ ./mvnw spring-boot:run

mvn package
mvn spring-boot:run

[Link] © luv2code LLC


Development Process Step-
By-S
tep

1. Exit the IDE

2. Package the app using mvnw package

3. Run app using java -jar

4. Run app using Spring Boot Maven plugin, mvnw spring-boot:run

[Link] © luv2code LLC


Spring Boot - Custom Application Properties

© luv2code LLC
Problem
• You need for your app to be configurable … no hard-coding of values

• You need to read app configuration from a properties file

[Link] © luv2code LLC


Solution: Application Properties file
• By default, Spring Boot reads information from a standard properties file

• Located at: src/main/resources/[Link]

• You can define ANY custom properties in this file Standard Spring Boot
file name

• Your Spring Boot app can access properties using @Value


No additional coding
or configuration required

[Link] © luv2code LLC


Development Process Step-
By-S
tep

1. Define custom properties in [Link]

2. Inject properties into Spring Boot application using @Value

[Link] © luv2code LLC


Step 1: Define custom application properties
File: src/main/resources/[Link]

#
# Define custom properties
#
[Link]=Mickey Mouse
[Link]=The Mouse Club

You can give ANY


custom property names

[Link] © luv2code LLC


Step 2: Inject Properties into Spring Boot app
No additional coding
@RestController
public class FunRestController { or configuration required

// inject properties for: [Link] and [Link]


File: src/main/resources/[Link]

@Value("${[Link]}")
private String coachName;

@Value("${[Link]}")
private String teamName;


}

[Link] © luv2code LLC


Spring Boot Properties

© luv2code LLC
Spring Boot Properties
• Spring Boot can be configured in the [Link] file

• Server port, context path, actuator, security etc …

• Spring Boot has 1,000+ properties … wowzers!

[Link] © luv2code LLC


Spring Boot Properties

List of Common Properties


[Link]/spring-boot-props

[Link] © luv2code LLC


Spring Boot Properties
• Don’t let the 1,000+ properties overwhelm you

• The properties are roughly grouped into the following categories

Core Web Security Data

Actuator Integration DevTools Testing

[Link] © luv2code LLC


Spring Boot Properties

We’ll review some of the properties …

[Link] © luv2code LLC


Core Properties Core

File: src/main/resources/[Link]
Logging Levels
# Log levels severity mapping
[Link]=DEBUG TRACE
[Link]=TRACE DEBUG
[Link].luv2code=INFO INFO
WARN
# Log file name ERROR
[Link]=[Link] FATAL
[Link]=c:/myapps/demo OFF

Spring Boot Logging


[Link]/spring-boot-logging

[Link] © luv2code LLC


Web Properties Web

[Link]
File: src/main/resources/[Link]

# HTTP server port


[Link]=7070

# Context path of the application


[Link]-path=/my-silly-app

# Default HTTP session time out


[Link]=15m

15 minutes

[Link] © luv2code LLC


Actuator Properties Actuator

File: src/main/resources/[Link]

# Endpoints to include by name or wildcard


[Link]=*

# Endpoints to exclude by name or wildcard


[Link]=beans,mapping

# Base path for actuator endpoints


[Link]-path=/actuator

[Link]

[Link] © luv2code LLC


Security Properties Security

File: src/main/resources/[Link]

# Default user name


[Link]=admin

# Password for default user


[Link]=topsecret

[Link] © luv2code LLC


Data Properties Data

File: src/main/resources/[Link]

# JDBC URL of the database


[Link]=jdbc:mysql://localhost:3306/ecommerce

# Login username of the database


[Link]=scott

# Login password of the database


[Link]=tiger More on this

in later videos

[Link] © luv2code LLC


Spring Boot Properties

List of Common Properties


[Link]/spring-boot-props

[Link] © luv2code LLC


Development Process Step-
By-S
tep

1. Configure the server port

2. Configure the application context path

[Link] © luv2code LLC

You might also like