Skip to content

Resources and sample projects for a lightning talk about spring-boot.

License

Notifications You must be signed in to change notification settings

msg-DAVID-GmbH/SpringBootLightningTalk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot lightning talk

This repository hosts several spring-boot projects and resources for a lightning talk on spring-boot and how spring-boot's starters and autoconfigure works under the hoood.

Table of Contents

Resources

Spring Boot Reference Guide

Create a Custom Auto-Configuration with Spring Boot

Spring Initializr

GitHub repository containing a custom starter

Introduction

From https://projects.spring.io/spring-boot/:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features

Create stand-alone Spring applications

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

Provide opinionated 'starter' POMs to simplify your Maven configuration

Automatically configure Spring whenever possible

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

Absolutely no code generation and no requirement for XML configuration

Tiny sample project

The code for this project is here.

Note the following things:

  • the easiest way to get the maven references is to have spring-boot-starter-parent as parent pom
  • our only dependency is spring-boot-starter-web (ignore the test)
  • there a main class annotated with @SpringBootApplication
  • there is a small rest controller
  • the controller's end point can be accessed at http://localhost:8080/helloworld

Questions:

  • Where does the tomcat come from?
  • Where does the tomcat's port come from?
  • How come there is a working DispatcherServlet?
  • How come there is a WebApplicationContext?

How AutoConfiguration works

Take a look at the pains you usually have to go through to configure and understand Spring web MVC.

Here's the source of spring-boot's 1.5.9 WebMvcAutoConfiguration.java. That's the main configuration class orchestrating the AutoConfiguration aspect of spring boot's web starter. That configuration does the heavy lifting to configure most of what you need to integrate web-mvc into your project.

Spring does this via condition annotations (documentation). They will only be loaded into your application context if their condition evaluates to true. That makes it clear how you can provide your own properties and beans to change default behavior and customize autoconfigurable features.

F.e. you can have your own InternalResourceViewResolver bean. If you don't, Spring adds this one for you:

	@Bean
	@ConditionalOnMissingBean
	public InternalResourceViewResolver defaultViewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix(this.mvcProperties.getView().getPrefix());
		resolver.setSuffix(this.mvcProperties.getView().getSuffix());
		return resolver;
	}

Here's a short list of possible condition annotations:

They can be useful even in regular spring applications to turn on or off specific beans and configurations.

Properties for almost everything

Spring boot promotes convention over configuration. If something doesn't work quite as you want, you could define your own beans and configurations. But before doing that, you should check whether there isn't a property you can set instead. The convention is to have an application.properties or applications.yml in your project root. You get placeholder replacement and profile-dependent property loading out of the box, f.e. application-production.properties are only loaded when profile "production" is set.

The versions of entire features can be set as property in your pom.xml:

	<properties>
	    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
	</properties>

There's a property discovery mechanism that allows for a multitude of ways to configure your system. Some IDEs even have a property completion (similar to code completion) feature for spring boot property files with documentation on top:

To give you an idea what can be configured, here are some examples (in .yml format) :

# the application's name. useful when using discovery
spring:
  application:
    name: vehicle-data

# pick a specific port (default is 8080) or use 0 for a random port
server:
  port: 8888

# instead of configuring an explicit logging framework, for small usecases you can use the generic spring boot logging configuration options
logging:
  level:
    org.springframework.cloud: 'DEBUG'
  pattern:
    console: "%d{HH:mm:ss} %clr(%.-1level) %msg%n"

Delivery as executable jar

Spring Initializr adds the spring-boot-maven-plugin to your pom.xml. This plugin can package your application into an executable jar when you run "mvn package". The executable jar is much bigger than the original jar because it contains all dependencies. This is a major selling point of Spring Boot because it facilitates rapid prototyping and developing microservices.

Other noteworthy features

  • developer tools
    • automatic restart
    • remote update
  • YAML support
  • type-safe configuration properties
  • support (starters) for many typical components
    • sql databses
    • nosql databases
    • messaging
    • logging
    • caching
  • auto-configured tests (slices)
  • production-ready features (means actuator)
  • cloud foundry (and other cloud infrastructures) support

Many more can be found in the Spring Boot reference.

Releases

No releases published

Packages

No packages published

Languages