Introduction to Spring Boot



Introduction to Spring Boot

0 0


spring-boot-intro

Spring Boot introduction talk

On Github JanAhrens / spring-boot-intro

Introduction to Spring Boot

Raise your hands

Java or JVM-based language (Groovy, Clojure, Scala, Kotlin)

Gradle or Maven

Spring or Spring Boot

Recap: patterns and principles

Dependency injection (DI)

Also known as inversion of control (IoC)

Hollywood principle: "Don't call us, we call you"

Dependency injection (DI)

class Demo {
    private final Foobar foobar;

    public Demo() {
        foobar = new Foobar(); // dependency NOT injected
    }

    public void doStuff() {
        foobar.doStuff();
    }
}
    

Dependency injection (DI)

class Demo {
    private final Foobar foobar;

    public Demo(Foobar foobar) {  // dependency injection
        this.foobar = foobar;
    }

    public void doStuff() {
        foobar.doStuff();
    }
}
    

Benefits of DI

Modularization

Testable code

Decrease coupling

Behavior can be changed

Aspect oriented programming (AOP)

Separate cross-cutting concerns

In plain English: reduce duplications

Aspect = concern

Examples for cross-cutting concerns

Logging

Transaction management

Authentication

AOP

class BookController {

    private final UserService userService;

    // ...

    public Response index(Request request) {
        if (userService.isAuthenticated(request) {
            // ...
        } else {
            return Response.unauthorized();
        }
    }

    public Response get(Request request) {
        if (userService.isAuthenticated(request) {
            // ...
        } else {
            return Response.unauthorized();
        }
    }

}
    

AOP

class BookController {

    @Authenticated
    public Response index(Request request) {
        // ...
    }

    @Authenticated
    public Response get(Request request) {
        // ...
    }

}
    

The Spring Framework

spring.io

The Spring Framework

IoC container

Spring AOP framework

The Spring Framework

XML based configuration

Lots of things to configure

Spring Boot

projects.spring.io/spring-boot

Spring Boot

Convention over configuration

Easily extensible through "Starter POMs"

Micro-framework paradigm

https://twitter.com/rob_winch/status/364871658483351552

$ sdk install springboot

sdkman.io

$ spring run hello-spring.grooy

http://localhost:8080

Spring isn't only about web applications

But a lot of people use it for

Definition: Spring beans and JavaBean

Not the same!

JavaBean: A plain Java class

JavaBean: Getter and setter, public constructor, serializable

JavaBean

class JavaBean {
  private final String name;

  public void setName(String name) {
      this.name = name;
  }

  public void getName() {
    return name;
  }
}
  

Sprint bean

Object instance managed by the Spring IoC container

Example: DI with Spring Boot

@Bean, @Autowired and @PostConstruct

example00/

Dependency graph

Example: Simple Web App

ratpack-spring-boot (unofficial starter)

spring-boot-starter-jdbc

example01/

Example: REST API

spring-boot-starter-web

spring-boot-starter-data-jpa

example02/

Spring Boot Actuator

localhost:7002/mappings

localhost:7002/env

localhost:7002/metrics

Not covered

HTML templates (and LiveReload)

Externalized configuration

Spring security

.. and a lot more

Further reading

start.spring.io

spring.io/guides

docs.spring.io/spring-boot/docs/current/reference/html

ratpack.io

Questions

Slides: blog.jan-ahrens.eu/spring-boot-intro

Introduction to Spring Boot