36
www.aurorasolutions.io www.aurorasolutions.io Spring Boot Introduction Presenter: Rasheed Amir

Spring boot introduction

Embed Size (px)

Citation preview

Page 1: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Spring Boot Introduction

Presenter: Rasheed Amir

Page 2: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Who is Rasheed?

❏ Programmer (Java, Groovy, C#, JavaScript). Architect. Agile Coach.❏ Co-founder Aurora Solutions, FixTelligent❏ Serial Entrepreneur❏ Certified Instructor for Spring Courses (Core, Web & Integration)❏ You can find me on LinkedIn

Page 6: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Spring Boot Goals

➔ Introduce developers to Spring Boot, an opinionated way to rapidly build production grade Spring applications quickly and with minimal fuss.

➔ Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults

➔ Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration)

➔ Absolutely no code generation and no requirement for XML configuration!

Page 7: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Spring Boot Goals...

➔ Single point of focus (as opposed to large collection of spring-* projects)

➔ A tool for getting started very quickly with Spring

➔ Common non-functional requirements for a "real" application

➔ Exposes a lot of useful features by default

➔ Gets out of the way quickly if you want to change defaults

Page 8: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Installation

Spring Boot CLI

Page 9: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Installation - Spring CLI

Spring CLI Installer -- Installer for the spring CLI command on Un*x-like system (should work on Linux, Mac or Cygwin).

You can curl http://start.spring.io/install.sh | sh, or download the script and run it.

Page 10: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Getting Started Quickly!

in Groovy!

Page 11: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Getting Started Quickly in Groovy!

Here’s a really simple web application that you can use to test your installation. Create a file called Welcome.groovy:

@RestController

class WelcomeToSpringBootMeetup {

@RequestMapping("/")

String home() {

"Welcome Everyone!"

}

}

$ spring run --watch Welcome.groovy

... application is running at http://localhost:8080

Page 12: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

How did it work?

// import org.springframework.web.bind.annotation.RestController// other imports …

@RestController

class WelcomeToSpringBootMeetup {

@RequestMapping("/")

String home() {

"Welcome Everyone!"

}

}

Page 13: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

How did it work?

// import org.springframework.web.bind.annotation.RestController// other imports …

// @Grab("org.springframework.boot:spring-boot-web-starter:0.5.0")

@RestController

class WelcomeToSpringBootMeetup {

@RequestMapping("/")

String home() {

"Welcome Everyone!"

}

}

Page 14: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

How did it work?

// import org.springframework.web.bind.annotation.RestController// other imports …

// @Grab("org.springframework.boot:spring-boot-web-starter:0.5.0")

// @EnableAutoConfiguration

@RestController

class WelcomeToSpringBootMeetup {

@RequestMapping("/")

String home() {

"Welcome Everyone!"

}

}

Page 15: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

How did it work?

// import org.springframework.web.bind.annotation.RestController// other imports …

// @Grab("org.springframework.boot:spring-boot-web-starter:0.5.0")

// @EnableAutoConfiguration

@RestController

class WelcomeToSpringBootMeetup {

@RequestMapping("/")

String home() {

"Welcome Everyone!"

}

// public static void main(String[] args) {

// SpringApplication.run(Example.class, args);

// }

}

Page 16: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Getting Started Quickly!

in Java!

Page 17: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Getting Started Quickly in Java!

Step 1: Create a folder; name it “helloworld”. Create an empty file called “pom.xml”. Copy the content given below...

Step 2: Run mvn package

Step 3: Run mvn dependency:tree

Step 4: Add spring-boot-starter-web dependency in pom

Step 5: Run mvn dependency:tree

Step 6: Create directory structure (src/main/java/com/helloworld) and file named “HelloWorld.java”. Copy the code give below...

Step 7: Running the HelloWorld: mvn spring-boot:run

Step 8: Open the browser: If you open a web browser to http://localhost:8080 and you will see something…Step 9: Create executable jar....

Page 18: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Starter POM’s

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>

➔ Standard Maven POMs➔ Define dependencies that we recommend➔ Parent optional➔ Available for web, batch, integration, data, amqp, aop, jdbc, ...➔ e.g. data = hibernate + spring-data + JSR 303

Page 19: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

SpringApplication

SpringApplication app = new SpringApplication(MyApplication.class);app.setShowBanner(false);app.run(args);

➔ Gets a running Spring ApplicationContext➔ Uses EmbeddedWebApplicationContext for web apps➔ Can be a single line: SpringApplication.run(MyApplication.class, args)

Page 20: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

SpringApplication

@Configuration@EnableAutoConfigurationpublic class Welcome {}

➔ Attempts to auto-configure your application➔ Backs off as you define your own beans➔ Regular @Configuration classes➔ Usually with @ConditionalOnClass and @ConditionalOnMissingBean

Page 21: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Production Packaging

Maven plugin (using spring-boot-starter-parent):

<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId></plugin>

$ mvn package or mvn clean install

$ java -jar <file-name>.jar

Page 22: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Production Packaging

$ java -jar <file-name.jar>

➔ Easy to understand structure➔ No unpacking or start scripts required➔ Typical REST app ~10Mb

Page 23: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Command Line Arguments

@Value("${name}")private String name;

SpringApplication adds command line arguments to the Spring Environment so you can refer inject them into beans:

$ java -jar <file-name>.jar --name=Rasheed

You can also configure many aspects of Spring Boot itself:

$ java -jar <file-name>.jar --server.port=9999

Page 24: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Externalizing Configuration to Properties

server.port: 9000

Just put application.properties in your classpath or next to you jar, e.g. application.properties

Properties can be overridden (command line arg > file > classpath)

Page 25: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Using YAML

server: port: 9000

Just include snake-yaml.jar and put application.yml in your classpath e.g. application.yml

Both properties and YAML add entries with period-separated paths to the Spring Environment.

Page 26: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Binding Configuration To Beans

@ConfigurationProperties(prefix="mine")public class MyPoperties { private Resource location; private boolean skip = true; // ... getters and setters}

MyProperties.java

application.properties

mine.location: classpath:mine.xmlmine.skip: false

Explore ServerProperties.java

Page 27: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Customizing Configuration Location

Set

➔ spring.config.name - default application, can be comma-separated list➔ spring.config.location - a Resource path, overrides name

$ java -jar <file-name>.jar --spring.config.name=production

Page 28: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Spring Profiles

Activate external configuration with a Spring profile file name convention e.g. application-development.properties

or nested documents in YAML:

defaults: etc… --- spring: profiles: development, qa other: stuff: more stuff...

application.yml

Set the default spring profile in external configuration, e.g: application.properties

spring.profiles.active: default, qa

Page 29: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Logging➔ Spring Boot provides default configuration files for 3 common logging frameworks:

logback, log4j and java.util.logging➔ Starters (and Samples) use logback with colour output➔ External configuration and classpath influence runtime behavior➔ LoggingApplicationContextInitializer sets it all up

Page 30: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Autoconfigured Behavior<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId></dependency><dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId></dependency>

Let’s extend the demo and see what we can get by just modifying the classpath, e.g.

● Add an in memory database

Page 31: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

Available Autoconfigured Behaviour...

● Embedded servlet container (Tomcat or Jetty)● JDBC: DataSource and JdbcTemplate● JPA, JMS, AMQP (Rabbit), AOP● Websocket● Spring Data JPA (scan for repositories) and Mongodb● Thymeleaf● Mobile● Batch processing● Reactor for events and async processing● Actuator features (Security, Audit, Metrics, Trace)

Page 32: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

The Actuator

Monitoring Endpoints

Page 33: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

The Actuator

Adds common non-functional features to your application and exposes MVC endpoints to interact with them.

Provides following endpoints: /metrics, /health, /trace, /dump, /shutdown, /beans, /env, /info

Page 35: Spring boot introduction

www.aurorasolutions.io www.aurorasolutions.io

● Spring Boot - main library supporting the other parts of Spring Boot● Spring Boot Autoconfigure - single @EnableAutoConfiguration annotation creates a

whole Spring context● Spring Boot Starters - a set of convenient dependency descriptors that you can include in

your application.● Spring Boot CLI - compiles and runs Groovy source as a Spring application● Spring Boot Actuator - common non-functional features that make an app instantly

deployable and supportable in production● Spring Boot Tools - for building and executing self-contained JAR and WAR archives● Spring Boot Samples - a wide range of sample apps