Overview of Spring Boot for the rapid development of Java Applications and Microservices. More information can be found at : https://www.spiraltrain.nl/course-spring-boot-development/?lang=en
3. www.spiraltrain.nl
What is Spring Boot?
• Spring Boot is completely new project from Spring Team :
• Framework developed on top of existing Spring Framework
• Spring Boot uses new model to make Java Development easy :
• Avoids some tedious development steps and boilerplate code and configuration
• Provides defaults for code and annotation configuration
• Follows Opinionated Defaults Configuration Approach
• Spring Boot Framework is not implemented from scratch :
• Rather it is implemented on top of existing Spring Framework
• Used to solve same problems like Spring Framework
3
Spring
Boot
Spring
Framework
Embedded
HTTP Servers
(Tomcat, Jetty)
XML<bean>
Configuration
or
@Configuration
= + -
Spring Boot
4. www.spiraltrain.nl
Advantages Spring Boot
• Easy to develop Spring Based applications with Java or Groovy :
• Reduces development time and increases productivity
• Avoids writing lots of boilerplate Code :
• Annotations and XML Configuration.
• Easy to integrate Spring Boot Applications with Spring Ecosystem :
• Like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.
• Provides Embedded HTTP servers like Tomcat, Jetty etc. :
• Allows develop and test web applications very easily
• Provides CLI Command Line Interface tool :
• Allows develop and test Spring Boot Applications from command prompt easily
• Provides lots of plugins :
• Allows develop and test Spring Boot Applications easily using Maven and Gradle
• Allows working with embedded and in-memory Databases easily
4Spring Boot
5. www.spiraltrain.nl
Goal of Spring Boot
• Main goal of Spring Boot is to reduce Development time :
• Unit Test and Integration Test time
• Ease the development of Production ready web applications
• Spring Boot features that accomplish this :
• Avoid XML Configuration completely
• Avoid defining more Annotation Configuration :
• Combines existing Spring Framework Annotations to simple and single Annotation
• Avoid writing lots of import statements
• Provide some defaults to quick start new projects within no time
• Provide Opinionated Development approach
• Limitations of Spring Boot :
• Time consuming to convert Spring Framework projects into Spring Boot Applications
• However it is easy to create brand new/Greenfield Projects using Spring Boot
• Three approaches to start opiniated development :
• Spring Boot CLI Tool, Spring STS IDE, Spring Initializr Website
5Spring Boot
6. www.spiraltrain.nl
Spring Boot Flavors
• Two flavors of Spring-Based Applications using Spring Boot :
• Java-Based Applications and Groovy Applications
• Spring Boot Groovy Applications :
• Spring Boot CLI or Spring STS IDE or Spring Initializr Website
• Spring Initializr Website is found at: http://start.spring.io/
• Spring Boot Java Applications :
• Spring STS IDE or Spring Initializr Website
• Groovy is also JVM language almost similar to Java Language :
• Can combine both Groovy and Java into one Project
• Groovy files are finally compiled into *.class files with same byte code format
6
Java
*.java
Compiler
javac of groovyc
*.class files
Byte Code
Groovy
*.groovy
Spring Boot
7. www.spiraltrain.nl
Key Spring Boot Components
• Spring Boot Framework has mainly four major Components :
• Spring Boot Starters
• Spring Boot AutoConfigurator
• Spring Boot CLI
• Spring Boot Actuator
• In addition there are two more Spring Boot components :
• Spring Initilizr and Spring Boot IDEs
• Use Spring Initializr web interface at http://start.spring.io :
• To quick start new Spring Boot projects
7
CLI
Actuator
Autoconfigurator
Starter
Spring Boot
Components
Spring Boot
8. www.spiraltrain.nl
Spring Boot Starter
• Combines group of related dependencies in single dependency
• Example for Spring WebApplication with Tomcat WebServer :
• Need minimal jar dependencies in your Maven’s pom.xml :
• Spring core Jar file spring-core-xx.jar, Spring Web Jar file spring-web-xx.jar
• Spring Web MVC Jar file spring-webmvc-xx.jar ,Servlet Jar file servlet-xx.jar
• Need to add database related jars to add some database stuff :
• Spring JDBC Jar file spring-jdbc-xx.jar, Spring ORM Jar files spring-orm-xx.jar
• Spring Transaction Jar file spring-transaction-xx.jar
• Need to define lot of dependencies in build files :
• Very tedious and cumbersome tasks and also it increases our build file size
• Solution is to use Spring Boot Starter component :
• Spring Boot Starter component combines all related jars into single jar file
• We need to add one and only one jar file spring-boot-starter-web.jar file
• When adding spring-boot-starter-web.jar file dependency :
• Spring Boot downloads all required jars and adds them to project classpath
8Spring Boot
9. www.spiraltrain.nl
Starter Dependencies
• spring-boot-starter-logging jar file :
• Loads all it’s dependency jars to project classpath like :
• jcl-over-slf4j, jul-to-slf4j,log4j-over-slf4j, logback-classic
9
spring-boot-starter-web
spring-boot-starter
spring-web
spring-webmvc
spring-boot-starter-tomcat
spring-boot
spring-boot-autoconfigure
spring-boot-starter-logging
Tomcat-embed-core
Tomcat-embed-logging- juli
Spring Boot
10. www.spiraltrain.nl
Spring Boot AutoConfigurator
• Most of the Spring IO Platform Critics :
• Spring-based application requires lot of configuration either with XML or annotations
• Solution to this problem is Spring Boot AutoConfigurator :
• Don’t need to define XML configuration and almost no Annotation configuration
• Spring Boot AutoConfigurator component will take care of providing this information
• Spring MVC application using Spring IO Platform :
• Define lot of XML Configuration like views, view resolvers etc
• If using spring-boot-starter-web jar file in project build file :
• Spring Boot AutoConfigurator will resolve views, view resolvers etc. automatically
• Also Spring Boot reduces Annotation configuration :
• If we use @SpringBootApplication annotation at class level
• Spring Boot AutoConfigurator automatically adds required annotations to Java ByteCode
• Spring Boot Starter reduces build’s dependencies :
• Spring Boot AutoConfigurator reduces the Spring Configuration
• Spring Boot Starter triggers Spring Boot AutoConfigurator automatically
10Spring Boot
11. www.spiraltrain.nl
@SpringBootApplication
• Definition for @SpringBootApplication :
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication
11
SpringBootApplication @Configuration @ComponentScan @EnableAutoConfiguration= + +
Spring Boot
12. www.spiraltrain.nl
Spring Boot CLI
• Spring Boot Command Line Interface :
• Spring Boot software to run and test Spring Boot applications from command line
• When running Spring Boot Applications through CLI :
• Internally uses Spring Boot Starter and Spring Boot AutoConfigurate components
• Spring Boot CLI introduced spring command :
• Execute Groovy Scripts from command prompt
spring run HelloWorld.groovy
• Can run Spring Web Applications with simple Spring Boot CLI Commands
• Spring Boot Framework internally uses Groovy :
• Add some defaults like Default import statements, Application main() method etc.
• Groovy language contains JAR Dependency Resolver :
• Resolves and add all required jar files to Groovy Project classpath
• Groovy Compiler groovyc :
• Will automatically add all default import statements then compile it
12Spring Boot
13. www.spiraltrain.nl
Spring Boot Internals
• Spring Boot Framework Programming model :
• Mainly inspired by Groovy Programming model
• Spring Boot Framework depends on two major components :
• Groovy and Grape
• Grape is an Embedded Dependency Resolution engine :
• JAR Dependency Manager embedded into Groovy
• Adds maven dependencies to project classpath to reduce build file definitions
13
Spring Boot
Groovy
Groovy’s Grape
Spring Boot
14. www.spiraltrain.nl
Spring Boot Actuator
• Spring Boot Actuator components major features are :
• Providing Management EndPoints to Spring Boot Applications.
• Spring Boot Applications Metrics
• For Spring Boot Web Application using CLI :
• Spring Boot Actuator provides hostname localhost and default port number 8080
• Application is accessed with http://localhost:8080/ end point
• Most common endpoints Boot provides out of the box :
• /health – Shows application health information
• /info – Displays arbitrary application info
• /metrics – Shows ‘metrics’ information for the current application
• /trace – Displays trace information by default the last few HTTP requests
• In order to get it working :
• Actuator requires Spring MVC to expose its endpoints through HTTP
• No other technology is supported
14Spring Boot