01 Spring Boot Overview
01 Spring Boot Overview
Date
© luv2code LLC
Main Objective
Date
© luv2code LLC
Java Development Environment
• We assume that you are already have experience with Java
• Java IDE (we'll use IntelliJ in the videos, but any Java IDE will work)
• You can also use the Ultimate Edition ($) ... free trial version is available
• Java IDE (we'll use IntelliJ in the videos, but any Java IDE will work)
• Make sure you can run a basic HelloWorld Java app in your Java IDE
© luv2code LLC
Spring in a Nutshell
mycoolapp.jar
Tomcat
Name of our JAR file
Tomcat
mycode
Spring Boot
Q: Does Spring Boot run code faster than regular Spring code?
• No.
• Behind the scenes, Spring Boot uses same code of Spring Framework
• 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]
• Not a requirement. Feel free to use the IDE that works best for you
© luv2code LLC
Spring Initializr
http://start.spring.io
• Quickly create a starter Spring project
• When building your Java project, you may need additional JAR files
• One approach is to download the JAR files from each project web site
• 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
© luv2code LLC
REST Controller
• Let's create a very simple REST Controller
@RestController
public class FunRestController {
© luv2code LLC
Spring Website - Official
www.spring.io
Beans
Core
SpEL
Context
Servlet
WebSocket
Web
JDBC
ORM
Transactions
OXM JMS
AOP
Aspects
Instrumentation
Messaging
Unit
Integration
Mock
© luv2code LLC
Spring Projects
© luv2code LLC
What Are Spring “Projects”
• others ...
© luv2code LLC
Maven Crash Course
© luv2code LLC
Spring Boot and Maven
• When you generate projects using Spring Initializr: start.spring.io
• When building your Java project, you may need additional JAR files
• One approach is to download the JAR files from each project web site
• 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
developer
JSON JAR files
Commons Logging 1
JSON Maven Maven Central Repository (remote)
…
5
developer
Build and Run
3
Get from remote repo
Directory Description
• They can easily find code, properties files, unit tests, web files etc …
• Once you learn Maven, you can join a new project and be productive
• You can build and run a project with minimal local configuration
© luv2code LLC
Maven Key Concepts
• POM File - pom.xml
• Project Coordinates
www.luv2code.com
POM File - pom.xml
www.luv2code.com
POM File Structure
Project name, version etc
pom.xml Output file type: JAR, WAR, …
www.luv2code.com
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>1.0.FINAL</version>
<packaging>jar</packaging>
<name>mycoolapp</name>
<dependencies>
<dependency> List of projects we depend on
<groupId>org.junit.jupiter</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>
www.luv2code.com
Project Coordinates
• Project Coordinates uniquely identify a project
City
<groupId>com.luv2code</groupId>
<artifactId>mycoolapp</artifactId> Street
<version>1.0.FINAL</version>
House Number
www.luv2code.com
Project Coordinates - Elements
Name Description
<groupId>com.luv2code</groupId>
<artifactId>mycoolapp</artifactId>
<version>1.0.FINAL</version>
www.luv2code.com
Example of Project Coordinates
<groupId>com.luv2code</groupId>
<artifactId>mycoolapp</artifactId>
<version>1.0.RELEASE</version>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0</version>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.4.Final</version>
www.luv2code.com
Adding Dependencies
<project ...>
...
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.4.Final</version>
</dependency>
...
</dependencies>
</project>
www.luv2code.com
Dependency Coordinates
• To add given dependency project, we need
www.luv2code.com
How to Find Dependency Coordinates
www.luv2code.com
Spring Boot Project Structure
© luv2code LLC
Spring Initializr
• Spring Initializr created a Maven project for us
http://start.spring.io
Directory Description
<groupId>com.luv2code.springboot.demo</groupId>
<artifactId>mycoolapp</artifactId>
<dependencies>
<version>0.0.1-SNAPSHOT</version>
<dependency>
<packaging>jar</packaging>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
Save's the developer from having to list all of the
<groupId>org.springframework.boot</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
…
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
$ ./mvnw package
Can also just use:
@Value("${team.name}")
private String teamName;
…
}
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.
• FreeMarker
By default, Spring Boot will load
templates from "/templates" directory
• Thymeleaf
• Mustache
© luv2code LLC
The Problem
• Building a Spring application is really HARD!!!
Spring
version
Which versions
are compatible?
Very error-prone
Easy to make
a simple mistake
There should be
an easier solution
• Makes it much easier for the developer to get started with Spring
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
…
pom.xml
Name Description
Building web apps, includes validation, REST.
spring-boot-starter-web
Uses Tomcat as default embedded server
Full list
www.luv2code.com/spring-boot-starters
• Somewhat cumbersome …
• Select View > Tool Windows > Maven Projects > Dependencies
© luv2code LLC
Spring Boot Starter Parent
• Spring Boot provides a "Starter Parent"
Included in pom.xml
when using
Spring Initializr
• Others …
Specify version of
Spring Boot
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> > mvn spring-boot:run
• Dependency management
© luv2code LLC
The Problem
• When running Spring Boot applications
• For IntelliJ, need to set additional configurations ... will discuss shortly
© luv2code LLC
Problem
• How can I monitor and manage my application?
Name Description
• To expose /info
File: src/main/resources/application.properties
management.endpoints.web.exposure.include=health,info
management.info.env.enabled=true
• Default is empty
Name Description
/auditevents Audit events for your application
Full list
www.luv2code.com/actuator-endpoints
© luv2code LLC
What about Security?
• You may NOT want to expose all of this information
/health
is still available
You can disable it
if you want
File: src/main/resources/application.properties
spring.security.user.name=scott
spring.security.user.password=tiger
File: src/main/resources/application.properties
© 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
• Since we using Spring Boot, the server is embedded in our JAR file
mycoolapp.jar
Self-contained unit
mycode Nothing else to install
• mvnw spring-boot:run
mycoolapp.jar
mycode
> java -jar mycoolapp.jar
Tomcat
mvn package
mvn spring-boot:run
© luv2code LLC
Problem
• You need for your app to be configurable … no hard-coding of values
• You can define ANY custom properties in this file Standard Spring Boot
file name
#
# Define custom properties
#
coach.name=Mickey Mouse
team.name=The Mouse Club
@Value("${coach.name}")
private String coachName;
@Value("${team.name}")
private String teamName;
…
}
© luv2code LLC
Spring Boot Properties
• Spring Boot can be configured in the application.properties file
File: src/main/resources/application.properties
Logging Levels
# Log levels severity mapping
logging.level.org.springframework=DEBUG TRACE
logging.level.org.hibernate=TRACE DEBUG
logging.level.com.luv2code=INFO INFO
WARN
# Log file name ERROR
logging.file.name=my-crazy-stuff.log FATAL
logging.file.path=c:/myapps/demo OFF
…
http://localhost:7070/my-silly-app/fortune
File: src/main/resources/application.properties
File: src/main/resources/application.properties
File: src/main/resources/application.properties
File: src/main/resources/application.properties